Existing method

Have been using the following function to create password for years.

function randpass($passlength = 10) 
{
  $i = 0;
  while($i < $passlength){
      $foo = chr(rand(33,122));
      if(strpos("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_?!.", $foo) === FALSE) continue;
      if(strpos($pass, $foo) !== FALSE ) continue;
      $pass .= $foo;
      $i++;
  }
  return $pass;
}

New method

Recently found that this method is quite slow. A much simpler and faster method.

function random_pass($i=10){
  for($j=33; $j<128; $j++) { $letters[] = chr($j); }  
  shuffle($letters);
  $subset = array_slice($letters, 0, $i);
  return implode('',$subset);
}  

Test output

Here are the results for some kind of benchmark tests:
1. Produce 1M random strings with both methods.
2. Run the test for string length from 8 to 20.

String length

Time of method 1

Time of method 2

8

15.185411930084

2.1637690067291

9

17.162335157394

2.9013810157776

10

19.071127176285

2.9580578804016

11

21.024034976959

2.9672908782959

12

22.92680311203

2.973995923996

13

24.843096017838

3.0049130916595

14

26.952475070953

3.0360279083252

15

28.952363967896

3.0420889854431

16

31.086222887039

3.0596430301666

17

33.311107873917

3.0881309509277

18

35.279084920883

3.142566204071

19

37.746668100357

3.1463310718536

20

40.050843000412

3.1662061214447

Conclusions:

1. The first method used more time in any case.
2. The longer of the string, the more time the first method used.
3. The time used by the second method to produce 1M strings was almost unchanged for different string lengths.

A faster method to create complicate password

Leave a Reply

Your email address will not be published. Required fields are marked *