Debian Codegraph @

bindings/perl/t/1test_hash_calculation.t

raw
1 use Test::More tests => 25;
2 BEGIN { use_ok('Crypt::Rhash') };
3
4 #########################
5 # test script
6
7 $msg = "message digest";
8
9 ok( $r = new Crypt::Rhash(RHASH_MD5, RHASH_TTH));
10 ok( $r->update($msg) );
11
12 is( $r->hash(), "f96b697d7cb7938d525a2f31aaf161d0"); # prints the first hash by default
13 is( $r->hash(RHASH_TTH), "ym432msox5qilih2l4tno62e3o35wygwsbsjoba");
14 is( $r->hash_hex(RHASH_TTH), "c339bd324ebf6085a0fa5f26d77b44dbb7db60d690649704");
15 is( $r->hash_hex(RHASH_MD5), "f96b697d7cb7938d525a2f31aaf161d0");
16 is( $r->hash_rhex(RHASH_MD5), "d061f1aa312f5a528d93b77c7d696bf9");
17 is( $r->hash_base32(RHASH_MD5), "7fvws7l4w6jy2us2f4y2v4lb2a");
18 is( $r->hash_base64(RHASH_MD5), "+WtpfXy3k41SWi8xqvFh0A==");
19 is( $r->hash_raw(RHASH_MD5), "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0");
20 is( $r->hashed_length(), length($msg));
21 is( $r->reset()->hash(), "d41d8cd98f00b204e9800998ecf8427e");
22
23 is( $r->hashed_length(), 0);
24 is( $r->hash_id(), (RHASH_MD5 | RHASH_TTH));
25 $r = undef; # destruct the Rhash object
26
27 #########################
28 # test hashing a file
29
30 $file = "msg.txt";
31 open FILE, ">$file" or die $!;
32 binmode FILE;
33 print FILE $msg;
34 close FILE;
35
36 $r = new Crypt::Rhash(RHASH_MD5);
37 is( $r->update_file($file), 14);
38 is( $r->hash(), "f96b697d7cb7938d525a2f31aaf161d0");
39
40 is( $r->reset()->update_file($file, 4, 1), 1);
41 is( $r->hash(), "0cc175b9c0f1b6a831c399e269772661");
42
43 open $fd, "<$file" or die $!;
44 binmode $fd;
45 is( $r->reset()->update_fd($fd), 14);
46 is( $r->hash(), "f96b697d7cb7938d525a2f31aaf161d0");
47 close $fd;
48 unlink($file);
49
50 #########################
51 # test magnet_link() method
52 $r = new Crypt::Rhash(RHASH_ALL);
53 $r->update("a")->final();
54 is( $r->magnet_link("test.txt", RHASH_MD5, RHASH_SHA1), "magnet:?xl=1&dn=test.txt&xt=urn:md5:0cc175b9c0f1b6a831c399e269772661&xt=urn:sha1:q336in72uwt7zyk5dxolt2xk5i3xmz5y");
55 is( $r->magnet_link(undef, RHASH_ED2K, RHASH_AICH, RHASH_TTH), "magnet:?xl=1&xt=urn:ed2k:bde52cb31de33e46245e05fbdbd6fb24&xt=urn:aich:q336in72uwt7zyk5dxolt2xk5i3xmz5y&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y");
56
57 $r = new Crypt::Rhash(RHASH_CRC32, RHASH_MD4);
58 $r->update("abc")->final();
59 is( $r->magnet_link(), "magnet:?xl=3&xt=urn:crc32:352441c2&xt=urn:md4:a448017aaf21d8525fc10ae87aa6729d");
60 is( $r->magnet_link("t"), "magnet:?xl=3&dn=t&xt=urn:crc32:352441c2&xt=urn:md4:a448017aaf21d8525fc10ae87aa6729d");
61
62 __END__