X-Git-Url: https://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Frand.py;h=d9e966d67c67f6635751ce54338990ae788915eb;hb=1c45d3f211f72ce19c9e92be868c15afe5e6ec62;hp=92e67e96b0dbbe303e663d077362c904656a7852;hpb=954b6dc9e3312f8d8b49f20f8466e6d2a8342f35;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/rand.py b/ctypescrypto/rand.py index 92e67e9..d9e966d 100644 --- a/ctypescrypto/rand.py +++ b/ctypescrypto/rand.py @@ -6,40 +6,41 @@ from ctypes import create_string_buffer, c_char_p, c_int, c_double from ctypescrypto import libcrypto from ctypescrypto.exception import LibCryptoError -__all__ = ['RandError','bytes','pseudo_bytes','seed','status'] +__all__ = ['RandError', 'bytes', 'pseudo_bytes', 'seed', 'status'] class RandError(LibCryptoError): + """ Exception raised when openssl function return error """ pass -def bytes( num, check_result=False): +def bytes(num, check_result=False): """ - Returns num bytes of cryptographically strong pseudo-random - bytes. If checkc_result is True, raises error if PRNG is not - seeded enough + Returns num bytes of cryptographically strong pseudo-random + bytes. If checkc_result is True, raises error if PRNG is not + seeded enough """ - if num <= 0 : + if num <= 0: raise ValueError("'num' should be > 0") - buffer = create_string_buffer(num) - result = libcrypto.RAND_bytes(buffer, num) + buf = create_string_buffer(num) + result = libcrypto.RAND_bytes(buf, num) if check_result and result == 0: raise RandError("Random Number Generator not seeded sufficiently") - return buffer.raw[:num] + return buf.raw[:num] def pseudo_bytes(num): """ - Returns num bytes of pseudo random data. Pseudo- random byte - sequences generated by pseudo_bytes() will be unique if - they are of sufficient length, but are not necessarily - unpredictable. They can be used for non-cryptographic purposes - and for certain purposes in cryptographic protocols, but usually - not for key generation etc. + Returns num bytes of pseudo random data. Pseudo- random byte + sequences generated by pseudo_bytes() will be unique if + they are of sufficient length, but are not necessarily + unpredictable. They can be used for non-cryptographic purposes + and for certain purposes in cryptographic protocols, but usually + not for key generation etc. """ - if num <= 0 : + if num <= 0: raise ValueError("'num' should be > 0") - buffer = create_string_buffer(num) - libcrypto.RAND_pseudo_bytes(buffer, num) - return buffer.raw[:num] + buf = create_string_buffer(num) + libcrypto.RAND_pseudo_bytes(buf, num) + return buf.raw[:num] def seed(data, entropy=None): """ @@ -47,24 +48,24 @@ def seed(data, entropy=None): If entropy is not None, it should be floating point(double) value estimating amount of entropy in the data (in bytes). """ - if not isinstance(data,str): + if not isinstance(data, str): raise TypeError("A string is expected") ptr = c_char_p(data) size = len(data) if entropy is None: libcrypto.RAND_seed(ptr, size) - else : + else: libcrypto.RAND_add(ptr, size, entropy) def status(): """ - Returns 1 if random generator is sufficiently seeded and 0 - otherwise + Returns 1 if random generator is sufficiently seeded and 0 + otherwise """ return libcrypto.RAND_status() - -libcrypto.RAND_add.argtypes=(c_char_p,c_int,c_double) -libcrypto.RAND_seed.argtypes=(c_char_p,c_int) -libcrypto.RAND_pseudo_bytes.argtypes=(c_char_p,c_int) -libcrypto.RAND_bytes.argtypes=(c_char_p,c_int) + +libcrypto.RAND_add.argtypes = (c_char_p, c_int, c_double) +libcrypto.RAND_seed.argtypes = (c_char_p, c_int) +libcrypto.RAND_pseudo_bytes.argtypes = (c_char_p, c_int) +libcrypto.RAND_bytes.argtypes = (c_char_p, c_int)