Commit | Line | Data |
---|---|---|
7cf5c72a MW |
1 | #! /usr/bin/python |
2 | ### -*-python-*- | |
3 | ### | |
4 | ### Generate test vectors for universal hashing. | |
5 | ||
37342632 MW |
6 | import sys as SYS |
7 | if SYS.version_info >= (3,): xrange = range | |
8 | ||
7cf5c72a MW |
9 | MOD = 0x104c11db7 |
10 | ||
11 | def gfmul(x, y): | |
12 | a = 0 | |
13 | while y > 0: | |
14 | if y & 1: a ^= x | |
15 | if x & 0x80000000: x = (x << 1) ^ MOD | |
16 | else: x <<= 1 | |
17 | y >>= 1 | |
18 | return a | |
19 | ||
20 | def hashtest(k, m): | |
21 | h = k | |
22 | for ch in m: h = gfmul(h ^ ord(ch), k) | |
b64eb60f MW |
23 | print('') |
24 | print('k = 0x%08x' % k) | |
25 | print('m = "%s"' % m) | |
26 | print('h = 0x%08x' % h) | |
7cf5c72a | 27 | |
37342632 | 28 | print('''\ |
b64eb60f MW |
29 | ;;; -*-conf-*- Test vectors for universal hashing |
30 | ;;; [generated] | |
7cf5c72a | 31 | |
b64eb60f | 32 | [unihash]''') |
7cf5c72a MW |
33 | |
34 | for k, m in [(0x00000000, 'anything you like'), | |
35 | (0x12345678, 'an exaple test string'), | |
36 | (0xb8a171f0, 'The quick brown fox jumps over the lazy dog.'), | |
b64eb60f MW |
37 | (0xcc825ed5, 'Jackdaws love my big sphinx of quartz.'), |
38 | (0x16e98e46, 'Waltz, bad nymph, for quick jigs vex!'), | |
7cf5c72a MW |
39 | (0x2940521b, 'A man, a plan, a canal: Panama!')]: |
40 | hashtest(k, m) | |
41 | ||
42 | k, m = 0x94b22a73, 0xbb7b1fef | |
43 | for i in xrange(48): | |
44 | hashtest(k, "If we don't succeed, we run the risk of failure.") | |
45 | k = gfmul(k, m) |