From 9af1f0d0345ecfe5cd05e90682c76472b1a21920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Sun, 1 May 2016 11:17:37 -0300 Subject: [PATCH] adds random and hashes docstrings. --- wolfcrypt/hashes.py | 36 ++++++++++++++++++++++++++++++++++-- wolfcrypt/random.py | 9 +++++++++ wolfcrypt/utils.py | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/hashes.py b/wolfcrypt/hashes.py index b22b04b..3ed14e1 100644 --- a/wolfcrypt/hashes.py +++ b/wolfcrypt/hashes.py @@ -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): diff --git a/wolfcrypt/random.py b/wolfcrypt/random.py index de47303..f819f51 100644 --- a/wolfcrypt/random.py +++ b/wolfcrypt/random.py @@ -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) diff --git a/wolfcrypt/utils.py b/wolfcrypt/utils.py index 43934ac..34646ff 100644 --- a/wolfcrypt/utils.py +++ b/wolfcrypt/utils.py @@ -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")