Ticket #42: hkdf_bench.py

File hkdf_bench.py, 1.9 KB (added by dragonxue, at 2010-07-03T01:53:49Z)
Line 
1#!/usr/bin/env python
2
3import random, re, time
4import unittest
5import hashlib
6
7from binascii import a2b_hex, b2a_hex
8from pycryptopp.hash import hkdf, sha256
9
10def randstr(n):
11        return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n)))
12
13def HKDF_Bench1():
14    print "HKDF_Bench1 starting\n"
15    ctxinfo = "hkdf bench test"
16    salt = ""
17    hash = sha256.SHA256
18    l = 20
19#    hk = hkdf.new(ikm, l, salt, ctxinfo, hash)
20    start_time = time.clock()
21    for times in xrange(1000):
22        ikm = randstr(100)
23        hk = hkdf.new(ikm, l, salt, ctxinfo, hash)
24        prk = hk.extract()
25        okm = hk.expand()
26
27    stop_time = time.clock()
28    print "hkdf using a 100 bytes ikm without salt, test 1000 times, Bench1: ", stop_time-start_time, "sec \n"
29    print "HKDF_Bench1 ending\n\n"
30
31def HKDF_Bench2():
32    print "HKDF_Bench2 starting\n"
33    ctxinfo = "hkdf bench test"
34    hash = sha256.SHA256
35    l = 20
36    salt = ""
37    start_time = time.clock()
38    for times in xrange(1000):
39        ikm = randstr(1000)
40        hk = hkdf.new(ikm, l, salt, ctxinfo, hash)
41        prk = hk.extract()
42        okm = hk.expand()
43
44    stop_time = time.clock()
45    print "hkdf using a 1000 bytes ikm without salt, test 1000 times, Bench2: ",stop_time-start_time, "sec \n"
46    print "HKDF_Bench2 ending\n\n"
47
48def HKDF_Bench3():
49    print "HKDF_Bench3 starting\n"
50    ctxinfo = "hkdf bench test"
51    hash = sha256.SHA256
52    l = 20
53    start_time = time.clock()
54    for times in xrange(1000):
55        ikm = randstr(100)
56        salt = randstr(64)
57        hk = hkdf.new(ikm, l, salt, ctxinfo, hash)
58        prk = hk.extract()
59        okm = hk.expand()
60
61    stop_time = time.clock()
62    print "hkdf using a 100 bytes ikm with 64 bytes salt, test 1000 times, Bench3: ", stop_time-start_time, "sec \n"
63    print "HKDF_Bench3 ending\n\n"
64
65def main():
66    HKDF_Bench1()
67    HKDF_Bench2()
68    HKDF_Bench3()
69
70if __name__ == "__main__":
71    main()
72
73
74