Show
Ignore:
Timestamp:
06/01/09 20:52:46 (3 years ago)
Author:
ocjhb
Message:

strenthening the random numbers; ordering coins for inspection; changing wording

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/oc2/rsa.py

    r272 r322  
    11"""RSA module 
    22 
    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. 
     3This is a module based on the works by Sybren Stuvel, Marloes de Boer and Ivo Tamboer, 
     4tlslite, helped by Nils Toedtmann, done wrong by Joerg Baach ;-) 
     5 
     6This file still needs serious audit before you can trust it for anything productive 
     7 
    108""" 
    11  
    12 __author__ = "Sybren Stuvel, Marloes de Boer and Ivo Tamboer" 
    13 __date__ = "2004-11-17" 
    149 
    1510# NOTE: Python's modulo can return negative numbers. We compensate for 
     
    2015import random    # For picking semi-random numbers 
    2116import types 
     17from hashlib import sha256 
    2218 
    2319# Get os.urandom PRNG 
    2420import os 
    2521def 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)) 
    2726    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         
    2937   
    3038