PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/mcrypt/tests/blowfish.php

http://github.com/facebook/hiphop-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
  1. <?hh
  2. <<__EntryPoint>> function main(): void {
  3. print "key plain crypt guess stat\n";
  4. $null = "\0\0\0\0\0\0\0\0";
  5. $vectors = file(dirname(__FILE__) . "/vectors.txt");
  6. $td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_ECB, "");
  7. foreach($vectors as $data) {
  8. $data = trim($data);
  9. if ($data) {
  10. list($key,$plain,$crypt) = preg_split("/[[:space:]]+/",$data);
  11. printf("%s %s ",
  12. $key,
  13. $plain
  14. );
  15. $key = hex2bin(trim($key));
  16. $plain = hex2bin(($plain));
  17. $crypt = strtolower(trim($crypt));
  18. mcrypt_generic_init ($td, $key, $null);
  19. $guess = mcrypt_generic ($td, $plain);
  20. $guess = bin2hex($guess);
  21. printf("%s %s %s\n",
  22. $crypt,
  23. $guess,
  24. ($crypt==$guess ? "OK" : "BAD")
  25. );
  26. }
  27. }
  28. // Longer test case from http://www.schneier.com/code/vectors.txt
  29. $td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_CBC, "");
  30. $key = hex2bin( "0123456789ABCDEFF0E1D2C3B4A59687" );
  31. $iv = hex2bin( "FEDCBA9876543210" );
  32. $plain = hex2bin( "37363534333231204E6F77206973207468652074696D6520666F722000" );
  33. mcrypt_generic_init( $td, $key, $iv );
  34. $guess = bin2hex( mcrypt_generic( $td, $plain ) );
  35. echo "\n", $guess, "\n";
  36. }