Changeset 322 for trunk/sandbox/jhb/oc2/rsa.py
- Timestamp:
- 06/01/09 20:52:46 (3 years ago)
- Files:
-
- 1 modified
-
trunk/sandbox/jhb/oc2/rsa.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sandbox/jhb/oc2/rsa.py
r272 r322 1 1 """RSA module 2 2 3 !!! This is just a playground, for understanding some bits and pieces, 4 this is not at all serious crypto production code!!! 5 6 7 8 Module for calculating large primes, and RSA encryption, decryption, 9 signing and verification. Includes generating public and private keys. 3 This is a module based on the works by Sybren Stuvel, Marloes de Boer and Ivo Tamboer, 4 tlslite, helped by Nils Toedtmann, done wrong by Joerg Baach ;-) 5 6 This file still needs serious audit before you can trust it for anything productive 7 10 8 """ 11 12 __author__ = "Sybren Stuvel, Marloes de Boer and Ivo Tamboer"13 __date__ = "2004-11-17"14 9 15 10 # NOTE: Python's modulo can return negative numbers. We compensate for … … 20 15 import random # For picking semi-random numbers 21 16 import types 17 from hashlib import sha256 22 18 23 19 # Get os.urandom PRNG 24 20 import os 25 21 def getRandomBytes(howMany): 26 bits = howMany * 8 22 factor = 8 * 8 #bytesize time security factor 23 bits = howMany * factor 24 if bits % factor: 25 bits = bits+(16-(bits % factor)) 27 26 number = random.getrandbits(bits) 28 return numberToBytes(number) 27 bytes = numberToBytes(number) 28 out = '' 29 30 #Assuming we haven't used a good source of randomness, but the 31 #Mersenne twister, we hash a bit to make it secure 32 while bytes: 33 out += sha256(bytes[:factor]).digest() 34 bytes = bytes[factor:] 35 return stringToBytes(out[:howMany]) 36 29 37 30 38
