Crypt-Tea_JS

 view release on metacpan or  search on metacpan

test.pl  view on Meta::CPAN

@ary = &Crypt::Tea_JS::oldtea_code((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [034504733200, 1589210186]), "oldtea_code");

@ary = &Crypt::Tea_JS::oldtea_decode((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [036053034552, 023357663604]), "oldtea_decode");

@ary = &Crypt::Tea_JS::pp_oldtea_code((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [034504733200, 1589210186]), "pp_oldtea_code");

@ary = &Crypt::Tea_JS::pp_oldtea_decode((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [036053034552, 023357663604]), "pp_oldtea_decode");

@ary = &Crypt::Tea_JS::tea_code((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [023450705615, 826873245]), "tea_code");

@ary = &Crypt::Tea_JS::tea_decode((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [021317044354, 034350376361]), "tea_decode");

@ary = &Crypt::Tea_JS::pp_tea_code((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [023450705615, 826873245]), "pp_tea_code");

@ary = &Crypt::Tea_JS::pp_tea_decode((2048299521,595110280),
  (032234174231,554905533,637549562,035705455446));
ok (&equal(\@ary, [021317044354, 034350376361]), "pp_tea_decode");

ok (&asciidigest($text) eq "7IGNTaSe2ch6WTwcz6c1eA", "asciidigest");

my $key1 = &asciidigest ("G $$ ". time);
my $c = &encrypt ($text, $key1);
my $p = &decrypt ($c, $key1);
ok (($p eq $text), "encrypt and decrypt");

{
	no integer;
	if ($] > 5.007) {
		require Encode;
		$x = chr(400);
		$c = &encrypt ($x, $key1);
		$p = Encode::decode_utf8(&decrypt ($c, $key1));
		ok (($p eq $x), "encrypt and decrypt utf8");
	} else {
		ok (1, "skipping utf8 test for perl version < 5.007");
	}
}

&generate_test_html();

exit;
# --------------------------- infrastructure ----------------
sub equal { my ($xref, $yref) = @_;
	my $eps = .000000001;
	my @x = @$xref; my @y = @$yref;
	if (scalar @x != scalar @y) { return 0; }
	my $i; for ($i=$[; $i<=$#x; $i++) {
		if (abs($x[$i]-$y[$i]) > $eps) { return 0; }
	}
	return 1;
}

sub generate_test_html {
$key1 = &asciidigest ("G $$ ". time);
my $key2 = &asciidigest ("Arghhh... " . time ."Xgloopiegleep $$");

my $p1 = <<EOT;
If you are reading this paragraph, it has been successfully
encrypted by <I>Perl</I> and decrypted by <I>JavaScript</I>.
The password used was "$key1".
A localised error in the cyphertext will cause about
16 bytes of binary garbage to appear in the plaintext output.
EOT
my $p2 = <<EOT;
And if you are reading this one, it has been successfully
encrypted by <I>Perl</I>, decrypted by <I>JavaScript</I>,
and then, using a different password "$key2",
re-encrypted and re-decrypted by <I>JavaScript</I>.
This means that <B>everyting&nbsp;works</B>&nbsp;:-)
EOT

if (! open (F, '>test.html')) { die "# sorry, can't write to test.html: $!\n"; }
$ENV{REMOTE_ADDR} = '123.321.123.321';  # simulate CGI context
my $c1 = &encrypt ($p1, $key1); 
my $c2 = &encrypt ($p2, $key1); 
print F "<HTML><HEAD><TITLE>test.html</TITLE>\n",&tea_in_javascript(),<<'EOT';
</HEAD><BODY BGCOLOR="#FFFFFF">
<P><H2>
This page is a test of the JavaScript side of
<A HREF="http://search.cpan.org/~pjb">Crypt::Tea_JS.pm</A>
</H2></P>

<HR>
<H3>First a quick check of the various JavaScript functions . . .</H3>
<P>If any of these functions do not return what they should,
please use your mouse to cut-and-paste all the bit in
<CODE>constant-width</CODE> font, and paste it into an email to
<A HREF="http://www.pjb.com.au/comp/contact.html">Peter&nbsp;Billam</A>
</P>
<PRE>
<SCRIPT LANGUAGE="JavaScript"> <!--
EOT
print F <<EOT;
document.write('Crypt::Tea_JS ${Crypt::Tea_JS::VERSION} on ' + navigator.appName
EOT
print F <<'EOT';
 + ' ' + navigator.appVersion);
// -->
</SCRIPT>

binary2ascii(1234567,7654321,9182736,8273645)
<SCRIPT LANGUAGE="JavaScript"> <!--
var blocks = new Array();
blocks[0]=1234567; blocks[1]=7654321; blocks[2]=9182736; blocks[3]=8273645;
document.write('   returns ' + binary2ascii(blocks));
// -->
</SCRIPT>



( run in 0.990 second using v1.01-cache-2.11-cpan-5735350b133 )