/hphp/test/zend/good/ext/mcrypt/tests/blowfish.php
PHP | 43 lines | 35 code | 7 blank | 1 comment | 1 complexity | 8393a8f21c84a9e635af5ffa8f1c66fd MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
- <?hh
- <<__EntryPoint>> function main(): void {
- print "key plain crypt guess stat\n";
- $null = "\0\0\0\0\0\0\0\0";
- $vectors = file(dirname(__FILE__) . "/vectors.txt");
- $td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_ECB, "");
- foreach($vectors as $data) {
- $data = trim($data);
- if ($data) {
- list($key,$plain,$crypt) = preg_split("/[[:space:]]+/",$data);
- printf("%s %s ",
- $key,
- $plain
- );
- $key = hex2bin(trim($key));
- $plain = hex2bin(($plain));
- $crypt = strtolower(trim($crypt));
- mcrypt_generic_init ($td, $key, $null);
- $guess = mcrypt_generic ($td, $plain);
- $guess = bin2hex($guess);
- printf("%s %s %s\n",
- $crypt,
- $guess,
- ($crypt==$guess ? "OK" : "BAD")
- );
- }
- }
- // Longer test case from http://www.schneier.com/code/vectors.txt
- $td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_CBC, "");
- $key = hex2bin( "0123456789ABCDEFF0E1D2C3B4A59687" );
- $iv = hex2bin( "FEDCBA9876543210" );
- $plain = hex2bin( "37363534333231204E6F77206973207468652074696D6520666F722000" );
- mcrypt_generic_init( $td, $key, $iv );
- $guess = bin2hex( mcrypt_generic( $td, $plain ) );
- echo "\n", $guess, "\n";
- }