1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import random, re, time |
---|
4 | import unittest |
---|
5 | |
---|
6 | from binascii import a2b_hex, b2a_hex |
---|
7 | from pycryptopp.hash import hmac, sha256 |
---|
8 | |
---|
9 | def randstr(n): |
---|
10 | return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n))) |
---|
11 | |
---|
12 | #multiple times hmac using different key and different messages |
---|
13 | def HMAC_Bench1(): |
---|
14 | print "HMAC_Bench1 starting\n" |
---|
15 | start_time = time.clock() |
---|
16 | # i=1 |
---|
17 | for keylen in xrange(100): |
---|
18 | for msglen in xrange(100): |
---|
19 | # for times in xrange(10): |
---|
20 | # print i, '\n' |
---|
21 | # i+=1 |
---|
22 | key = randstr(keylen) |
---|
23 | msg = randstr(msglen) |
---|
24 | h = hmac.new(key, msg, sha256.SHA256) |
---|
25 | h.digest() |
---|
26 | |
---|
27 | stop_time = time.clock() |
---|
28 | print "multiple times hmac using different random key and different random messages, 100 random keys * 100 messages with lenth from 0 to 99, Bench1: ", stop_time-start_time,'sec \nHMAC_Bench1 ending\n' |
---|
29 | |
---|
30 | #using a key hmac a short message |
---|
31 | def HMAC_Bench2(): |
---|
32 | print "HMAC_Bench2 starting\n" |
---|
33 | start_time = time.clock() |
---|
34 | key = randstr(100) |
---|
35 | msg = randstr(10) |
---|
36 | h = hmac.new(key, msg, sha256.SHA256) |
---|
37 | h.digest() |
---|
38 | stop_time = time.clock() |
---|
39 | print "using a key hmac a short message, Bench2: ", stop_time-start_time,"sec \n" |
---|
40 | print "HMAC_Bench2 ending\n\n\n" |
---|
41 | |
---|
42 | def HMAC_Bench3(): |
---|
43 | print "hmac bench3 starting\n" |
---|
44 | start_time = time.clock() |
---|
45 | k1 = "k"*100 |
---|
46 | m1 = "a"*10000000 |
---|
47 | h1 = hmac.new(k1, m1, sha256.SHA256) |
---|
48 | h1.digest() |
---|
49 | stop_time = time.clock() |
---|
50 | print "using a fixed 100 bytes key hmac a 10^7 bytes long fixed long message:\n", stop_time-start_time, 'sec \n' |
---|
51 | print "another one using a random string" |
---|
52 | start_time = time.clock() |
---|
53 | k2=randstr(100) |
---|
54 | m2=randstr(10000000) |
---|
55 | h2 = hmac.new(k2, m2, sha256.SHA256) |
---|
56 | h2.digest() |
---|
57 | stop_time = time.clock() |
---|
58 | print "using a random 100 bytes key hmac a 10^7 bytes long random message, Bench3: ", stop_time-start_time,"sec \n" |
---|
59 | print "As we can see, most of time is spent on random string generation \nhmac bench3 ending\n\n\n" |
---|
60 | |
---|
61 | def HMAC_Bench4(): |
---|
62 | print "hmac bench4 starting\n" |
---|
63 | k = "k"*100 |
---|
64 | m = "b"*10000000 |
---|
65 | start_time = time.clock() |
---|
66 | for times in xrange(1000): |
---|
67 | h = hmac.new(k, m , sha256.SHA256) |
---|
68 | h.digest() |
---|
69 | stop_time = time.clock() |
---|
70 | print "hmac a 10^7 bytes long message 1000 times, Bench4: ", stop_time-start_time, "sec \n" |
---|
71 | print "hmac bench4 ending\n\n\n" |
---|
72 | |
---|
73 | |
---|
74 | def main(): |
---|
75 | HMAC_Bench4() |
---|
76 | HMAC_Bench3() |
---|
77 | HMAC_Bench2() |
---|
78 | HMAC_Bench1() |
---|
79 | |
---|
80 | if __name__=="__main__": |
---|
81 | main() |
---|
82 | |
---|