573eadb5 |
1 | #! /usr/bin/perl |
2 | |
3 | my $MOD = 0x04c11db7; |
4 | |
5 | sub gfmul { |
6 | my ($x, $y) = @_; |
7 | my $a = 0; |
8 | |
9 | while ($y) { |
10 | if ($y & 1) { $a ^= $x }; |
d6cf7c44 |
11 | if ($x & 0x80000000) { $x <<= 1; $x &= 0xffffffff; $x ^= $MOD; } |
573eadb5 |
12 | else { $x <<= 1; } |
13 | $y >>= 1; |
14 | } |
15 | return $a; |
16 | } |
17 | |
18 | sub hash { |
19 | my ($k, $msg) = @_; |
20 | my $h = $k; |
21 | for (my $i = 0; $i < length $msg; $i++) { |
22 | my $m = ord(substr($msg, $i, 1)); |
23 | $h = gfmul($h ^ $m, $k); |
24 | } |
25 | printf " 0x%08x \"%s\" 0x%08x;\n", $k, $msg, $h; |
26 | } |
27 | |
28 | print <<EOF; |
29 | # test vectors for unihash |
30 | |
31 | hash { |
32 | EOF |
33 | hash(0x00000000, "anything you like"); |
34 | hash(0x12345678, "an exaple test string"); |
35 | hash(0xb8a171f0, "The quick brown fox jumps over the lazy dog."); |
36 | hash(0x2940521b, "A man, a plan, a canal: Panama!"); |
37 | |
38 | my $k = 0x94b22a73; |
39 | my $m = 0xbb7b1fef; |
40 | for (my $i = 0; $i < 48; $i++) { |
41 | hash($k, "If we don't succeed, we run the risk of failure."); |
42 | $k = gfmul($k, $m); |
43 | } |
44 | |
45 | print <<EOF; |
46 | } |
47 | EOF |