adds random and hashes docstrings.

gh-pages
Moisés Guimarães 2016-05-01 11:17:37 -03:00
parent 2287ebad58
commit 9af1f0d034
3 changed files with 46 additions and 2 deletions

View File

@ -33,7 +33,12 @@ class _Hash(object):
@classmethod
def new(cls, string=None):
# PEP 247 -- API for Cryptographic Hash Functions
"""
Create a new hashing object and returns it. The optional
'string' parameter, if supplied, will be immediately hashed
into the object's starting state, as if obj.update(string)
was called.
"""
self = cls(string)
if (string):
@ -44,6 +49,10 @@ class _Hash(object):
def copy(self):
"""
Return a separate copy of this hashing object. An update to
this copy won't affect the original object.
"""
copy = self.new("")
_ffi.memmove(copy._native_object,
@ -54,6 +63,11 @@ class _Hash(object):
def update(self, string):
"""
Hash 'string' into the current state of the hashing object.
update() can be called any number of times during a hashing
object's lifetime.
"""
string = t2b(string)
ret = self._update(string)
@ -62,6 +76,12 @@ class _Hash(object):
def digest(self):
"""
Return the hash value of this hashing object as a string
containing 8-bit data. The object is not altered in any
way by this function; you can continue updating the object
after calling this function.
"""
result = t2b("\0" * self.digest_size)
if self._native_object:
@ -77,6 +97,12 @@ class _Hash(object):
def hexdigest(self):
"""
Return the hash value of this hashing object as a string
containing hexadecimal digits. Lowercase letters are used
for the digits 'a' through 'f'. Like the .digest() method,
this method doesn't alter the object.
"""
return b2h(self.digest())
@ -178,7 +204,13 @@ class _Hmac(_Hash):
@classmethod
def new(cls, key, string=None):
# PEP 247 -- API for Cryptographic Hash Functions
"""
Create a new hashing object and returns it. 'key' is a
required parameter containing a string giving the key
to use. The optional 'string' parameter, if supplied, will
be immediately hashed into the object's starting state, as
if obj.update(string) was called.
"""
self = cls(key)
if (string):

View File

@ -25,6 +25,9 @@ from wolfcrypt.exceptions import *
class Random(object):
"""
A Cryptographically Secure Pseudo Random Number Generator - CSPRNG
"""
def __init__(self):
self.native_object = _ffi.new("WC_RNG *")
@ -40,6 +43,9 @@ class Random(object):
def byte(self):
"""
Generate and return a random byte.
"""
result = t2b("\0")
ret = _lib.wc_RNG_GenerateByte(self.native_object, result)
@ -50,6 +56,9 @@ class Random(object):
def bytes(self, length):
"""
Generate and return a random sequence of length bytes.
"""
result = t2b("\0" * length)
ret = _lib.wc_RNG_GenerateBlock(self.native_object, result, length)

View File

@ -30,6 +30,9 @@ else:
def t2b(s):
"""
Converts text to bynary.
"""
if isinstance(s, _binary_type):
return s
return _text_type(s).encode("utf-8")