File: rsa_speed.py

package info (click to toggle)
python-crypto 2.0.1%2Bdfsg1-1.2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 736 kB
  • ctags: 932
  • sloc: ansic: 6,591; python: 3,585; makefile: 13; sh: 10
file content (48 lines) | stat: -rw-r--r-- 1,328 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

# Script to time fast and slow RSA operations
# Contributed by Joris Bontje.

import time, pprint
from Crypto.PublicKey import *
from Crypto.Util.randpool import RandomPool
from Crypto.Util import number

pool = RandomPool()
pool.stir()

KEYSIZE=2048
COUNT=5
fasttime=0
slowtime=0
for x in range(COUNT):
    begintime=time.time()
    rsa=RSA.generate(KEYSIZE, pool.get_bytes)
    endtime=time.time()
    print "Server: Generating %d bit RSA key: %f s" % (KEYSIZE, endtime-begintime)
    rsa_slow=RSA.construct((rsa.n,rsa.e,rsa.d))

    code=number.getRandomNumber(256, pool.get_bytes)
    begintime=time.time()
    signature=rsa.sign(code,None)[0]
    endtime=time.time()
    fast=(endtime-begintime)
    fasttime=fasttime+fast
    print "Fast signing took %f s" % fast

    begintime=time.time()
    signature_slow=rsa_slow.sign(code,None)[0]
    endtime=time.time()
    slow=(endtime-begintime)
    slowtime=slowtime+slow
    print "Slow signing took %f s" % slow

    if rsa.verify(code,(signature,)) and signature==signature_slow:
        print "Signature okay"
    else:
        print "Signature WRONG"

    print "faster: %f" % (slow/fast)

print "Based on %d signatures with %d bits keys the optimized\n RSA decryption/signing algorithm is %f times faster" % (COUNT, KEYSIZE, (slowtime/fasttime))