PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/test/blowfish.pl

https://bitbucket.org/lindenlab/viewer-beta/
Perl | 79 lines | 50 code | 16 blank | 13 comment | 0 complexity | 69febc40dfe57488c081a9fd5499f1f9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/perl
  2. #
  3. # Test Perl Crypt::CBC Blowfish algorithm and initial parameter settings
  4. # for compatibility with OpenSSL's Blowfish algorithm/settings.
  5. #
  6. # Used by outbound LSL email (openssl C library) and mailglue (Perl library)
  7. use strict;
  8. use warnings;
  9. # *TODO: specify test count here
  10. use Test::More qw(no_plan);
  11. use Crypt::CBC;
  12. use MIME::Base64;
  13. my $init_vector = "\x00" x 8;
  14. # my $key = pack("H*", "ef5a8376eb0c99fe0dafa487d15bec19cae63d1e25fe31d8d92f7ab0398246d70ee733108e47360e16359654571cf5bab6c3375b42cee4fa");
  15. # my $key = "d263eb8a78034e40";
  16. #"8d082918aa369174";
  17. my $key = "\x00" x 16;
  18. my $cipher = Crypt::CBC->new( { cipher => 'Blowfish',
  19. regenerate_key => 0,
  20. key => $key,
  21. iv => $init_vector,
  22. header => 'none',
  23. prepend_iv => 0,
  24. keysize => 16 } );
  25. #my $blocks = $cipher->blocksize();
  26. #print "blocksize $blocks\n";
  27. my $len;
  28. my $input = "01234567890123456789012345678901234\n";
  29. #my $input = "a whale of a tale I tell you lad, a whale of a tale for me, a whale of a tale and the fiddlers three";
  30. $len = length($input);
  31. is ($len, 36, "input length");
  32. $len = length($key);
  33. is ($len, 16, "key length");
  34. my $encrypted = $cipher->encrypt($input);
  35. is (length($encrypted), 40, "encrypted length");
  36. open(FH, "blowfish.1.bin");
  37. my $bin = scalar <FH>;
  38. is ($encrypted, $bin, "matches openssl");
  39. close(FH);
  40. my $base64 = encode_base64($encrypted);
  41. is ($base64, "LkGExDOMTNxFIGBg8gP43UvbQLz7xztNWwYF2kLrtwT4hD7LykOXJw==\n",
  42. "base64 output");
  43. my $unbase64 = decode_base64($base64);
  44. is( $encrypted, $unbase64, "reverse base64" );
  45. my $output = $cipher->decrypt($unbase64);
  46. is ($input, $output, "reverse encrypt");
  47. $key = pack("H[32]", "526a1e07a19dbaed84c4ff08a488d15e");
  48. $cipher = Crypt::CBC->new( { cipher => 'Blowfish',
  49. regenerate_key => 0,
  50. key => $key,
  51. iv => $init_vector,
  52. header => 'none',
  53. prepend_iv => 0,
  54. keysize => 16 } );
  55. $encrypted = $cipher->encrypt($input);
  56. is (length($encrypted), 40, "uuid encrypted length");
  57. $output = $cipher->decrypt($encrypted);
  58. is ($input, $output, "uuid reverse encrypt");
  59. open(FH, "blowfish.2.bin");
  60. $bin = scalar <FH>;
  61. close(FH);
  62. is( $encrypted, $bin, "uuid matches openssl" );
  63. print encode_base64($encrypted);