chiark / gitweb /
cleanup: All the whitespace fixes, all at once.
[mLib] / unihash-check.pl
CommitLineData
573eadb5 1#! /usr/bin/perl
2
3my $MOD = 0x04c11db7;
4
5sub 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
18sub 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
28print <<EOF;
29# test vectors for unihash
30
31hash {
32EOF
33hash(0x00000000, "anything you like");
34hash(0x12345678, "an exaple test string");
35hash(0xb8a171f0, "The quick brown fox jumps over the lazy dog.");
36hash(0x2940521b, "A man, a plan, a canal: Panama!");
37
38my $k = 0x94b22a73;
39my $m = 0xbb7b1fef;
40for (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
45print <<EOF;
46}
47EOF