Skip to content

Commit

Permalink
Added shuffleString and shuffleArray to the CryptLib class
Browse files Browse the repository at this point in the history
  • Loading branch information
ircmaxell committed Aug 23, 2011
1 parent 98ac98b commit b9e4d6a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/CryptLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@

printf("\nHere's a random array element: %s\n", $element);

/**
* And we can randomize an array
*/
$array = array('a', 'b', 'c', 'd', 'e', 'f');
$newArray = $cryptLib->shuffleArray($array);

printf("\nHere's a randomized array: \n");

print_r($newArray);

printf("\nAnd here's the same arrays with incremental keys:\n");

print_r(array_values($newArray));

/**
* And we can randomize a string
*/

$string = 'abcdef';
$newString = $cryptLib->shuffleString($string);

printf("\nHere's our randomized string: %s\n", $newString);

/**
* Now, lets do some password hashing.
*/
Expand Down
42 changes: 42 additions & 0 deletions lib/CryptLib/CryptLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,46 @@ public function getRandomToken($size) {
return $generator->generateString($size);
}

/**
* Shuffle an array. This will preserve key => value relationships, and return
* a new array that has been randomized in order.
*
* To get keys randomized, simply pass the result through array_values()...
*
* @param array $array The input array to randomize
*
* @return array The suffled array
*/
public function shuffleArray(array $array) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
$result = array();
$values = array_values($array);
$keys = array_keys($array);
$max = count($array);
for ($i = $max - 1; $i >= 0; $i--) {
$int = $generator->generateInt(0, $i);
$result[$keys[$int]] = $values[$int];
unset($keys[$int], $values[$int]);
$keys = array_values($keys);
$values = array_values($values);
}
return $result;
}

/**
* Shuffle a string and return the randomized string
*
* @param string $string The string to randomize
*
* @return string The shuffled string
*/
public function shuffleString($string) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
$array = str_split($string);
$result = $this->shuffleArray($array);
return implode('', $result);
}

}
18 changes: 18 additions & 0 deletions test/Unit/CryptLibTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ public function testGetRandomToken() {
$string = $crypt->getRandomToken(10);
$this->assertEquals(10, strlen($string));
}

public function testShuffleArray() {
$crypt = new CryptLib;
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$newArray = $crypt->shuffleArray($array);
$this->assertNotEquals($array, array_values($newArray));
$this->assertEquals($array, $newArray);
}

public function testShuffleString() {
$crypt = new CryptLib;
$string = 'abcdefghijklmnopqrstuvwxyz';
$newString = $crypt->shuffleString($string);
$this->assertNotEquals($string, $newString);
$cnt = count_chars($string, 1);
$cnt2 = count_chars($newString, 1);
$this->assertEquals($cnt, $cnt2);
}
}

0 comments on commit b9e4d6a

Please sign in to comment.