PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms 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
 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)
 7use strict;
 8use warnings;
 9
10# *TODO: specify test count here
11use Test::More qw(no_plan);
12
13use Crypt::CBC;
14use MIME::Base64;
15
16my $init_vector = "\x00" x 8;
17# my $key = pack("H*", "ef5a8376eb0c99fe0dafa487d15bec19cae63d1e25fe31d8d92f7ab0398246d70ee733108e47360e16359654571cf5bab6c3375b42cee4fa");
18# my $key = "d263eb8a78034e40";
19         #"8d082918aa369174";
20my $key = "\x00" x 16;
21
22my $cipher = Crypt::CBC->new( { cipher => 'Blowfish',
23								regenerate_key => 0,
24							  	key => $key,
25							  	iv => $init_vector,
26							  	header => 'none',
27								prepend_iv => 0,
28							  	keysize => 16 } );
29
30#my $blocks = $cipher->blocksize();
31#print "blocksize $blocks\n";
32
33my $len;
34my $input = "01234567890123456789012345678901234\n";
35#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";
36$len = length($input);
37is ($len, 36, "input length");
38
39$len = length($key);
40is ($len, 16, "key length");
41
42
43my $encrypted = $cipher->encrypt($input);
44is (length($encrypted), 40, "encrypted length");
45
46open(FH, "blowfish.1.bin");
47my $bin = scalar <FH>;
48is ($encrypted, $bin, "matches openssl");
49close(FH);
50
51my $base64 = encode_base64($encrypted);
52is ($base64, "LkGExDOMTNxFIGBg8gP43UvbQLz7xztNWwYF2kLrtwT4hD7LykOXJw==\n",
53	"base64 output");
54
55my $unbase64 = decode_base64($base64);
56is( $encrypted, $unbase64, "reverse base64" );
57
58my $output = $cipher->decrypt($unbase64);
59is ($input, $output, "reverse encrypt");
60
61$key = pack("H[32]", "526a1e07a19dbaed84c4ff08a488d15e");
62$cipher = Crypt::CBC->new( { cipher => 'Blowfish',
63								regenerate_key => 0,
64							  	key => $key,
65							  	iv => $init_vector,
66							  	header => 'none',
67								prepend_iv => 0,
68							  	keysize => 16 } );
69$encrypted = $cipher->encrypt($input);
70is (length($encrypted), 40, "uuid encrypted length");
71$output = $cipher->decrypt($encrypted);
72is ($input, $output, "uuid reverse encrypt");
73
74open(FH, "blowfish.2.bin");
75$bin = scalar <FH>;
76close(FH);
77is( $encrypted, $bin, "uuid matches openssl" );
78
79print encode_base64($encrypted);