本文整理汇总了Python中Crypto.Util.winrandom.new方法的典型用法代码示例。如果您正苦于以下问题:Python winrandom.new方法的具体用法?Python winrandom.new怎么用?Python winrandom.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.winrandom
的用法示例。
在下文中一共展示了winrandom.new方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runTest
# 需要导入模块: from Crypto.Util import winrandom [as 别名]
# 或者: from Crypto.Util.winrandom import new [as 别名]
def runTest(self):
"""winrandom: simple test"""
# Import the winrandom module and try to use it
from Crypto.Util import winrandom
randobj = winrandom.new()
x = randobj.get_bytes(16)
y = randobj.get_bytes(16)
self.assertNotEqual(x, y)
示例2: __init__
# 需要导入模块: from Crypto.Util import winrandom [as 别名]
# 或者: from Crypto.Util.winrandom import new [as 别名]
def __init__(self, numbytes = 160, cipher=None, hash=None):
if hash is None:
from hashlib import sha1 as hash
# The cipher argument is vestigial; it was removed from
# version 1.1 so RandomPool would work even in the limited
# exportable subset of the code
if cipher is not None:
warnings.warn("'cipher' parameter is no longer used")
if isinstance(hash, types.StringType):
# ugly hack to force __import__ to give us the end-path module
hash = __import__('Crypto.Hash.'+hash,
None, None, ['new'])
warnings.warn("'hash' parameter should now be a hashing module")
self.bytes = numbytes
self.bits = self.bytes*8
self.entropy = 0
self._hash = hash
# Construct an array to hold the random pool,
# initializing it to 0.
self._randpool = array.array('B', [0]*self.bytes)
self._event1 = self._event2 = 0
self._addPos = 0
self._getPos = hash().digest_size
self._lastcounter=time.time()
self.__counter = 0
self._measureTickSize() # Estimate timer resolution
self._randomize()
示例3: _randomize
# 需要导入模块: from Crypto.Util import winrandom [as 别名]
# 或者: from Crypto.Util.winrandom import new [as 别名]
def _randomize(self, N = 0, devname = '/dev/urandom'):
"""_randomize(N, DEVNAME:device-filepath)
collects N bits of randomness from some entropy source (e.g.,
/dev/urandom on Unixes that have it, Windows CryptoAPI
CryptGenRandom, etc)
DEVNAME is optional, defaults to /dev/urandom. You can change it
to /dev/random if you want to block till you get enough
entropy.
"""
data = ''
if N <= 0:
nbytes = int((self.bits - self.entropy)/8+0.5)
else:
nbytes = int(N/8+0.5)
if winrandom:
# Windows CryptGenRandom provides random data.
data = winrandom.new().get_bytes(nbytes)
# GAE fix, benadida
#elif os.path.exists(devname):
# # Many OSes support a /dev/urandom device
# try:
# f=open(devname)
# data=f.read(nbytes)
# f.close()
# except IOError, (num, msg):
# if num!=2: raise IOError, (num, msg)
# # If the file wasn't found, ignore the error
if data:
self._addBytes(data)
# Entropy estimate: The number of bits of
# data obtained from the random source.
self._updateEntropyEstimate(8*len(data))
self.stir_n() # Wash the random pool
示例4: randomize
# 需要导入模块: from Crypto.Util import winrandom [as 别名]
# 或者: from Crypto.Util.winrandom import new [as 别名]
def randomize(self, N = 0):
"Adds N bits of entropy to random pool. If N is 0, fill up pool."
import os, string, time
if N <= 0:
bits = self.bits - self.entropy
else:
bits = N*8
if bits == 0:
return
print bits,'bits of entropy are now required. Please type on the keyboard'
print 'until enough randomness has been accumulated.'
kb = KeyboardEntry()
s='' # We'll save the characters typed and add them to the pool.
hash = self._hash
e = 0
try:
while e < bits:
temp=str(bits-e).rjust(6)
os.write(1, temp)
s=s+kb.getch()
e += self.add_event(s)
os.write(1, 6*chr(8))
self.add_event(s+hash.new(s).digest() )
finally:
kb.close()
print '\n\007 Enough. Please wait a moment.\n'
self.stir_n() # wash the random pool.
kb.close(4)