Utilities¶
Generating Random Bytes¶
- monocypher.utils.random(n)¶
Generates exactly n random bytes. This just calls
os.urandom()
and returns the result.- Return type:
Constant-Time Comparisons¶
The following functions perform constant-time comparisons of bytes
objects of length 16, 32, and 64 respectively. They return True
if they
are equal, and False
otherwise.
- monocypher.utils.crypto_verify16(a, b)¶
- monocypher.utils.crypto_verify32(a, b)¶
- monocypher.utils.crypto_verify64(a, b)¶
from monocypher.utils import crypto_verify16
crypto_verify16(b'a....a', b'a....a')
Copying Contexts¶
- monocypher.utils.copy_context(ctx_ptr, type)¶
Return a copy of the struct at ctx_ptr of the given type. This can be used to copy structs, e.g.
crypto_blake2b_ctx
. This is equivalent to the following in C:type dst_ptr = malloc(sizeof(*ctx_ptr)); memcpy( (void *) dst_ptr, (void *) ctx_ptr, sizeof(*ctx_ptr) );
Example:
>>> from monocypher.bindings import crypto_blake2b_init >>> ctx = crypto_blake2b_init() >>> u = copy_context(ctx, 'crypto_blake2b_ctx *') <cdata 'crypto_blake2b_ctx *' owning ...> >>> u == ctx False
- Parameters:
ctx_ptr – CFFI pointer to some struct.
type – Type of ctx_ptr.
Memory Wipe¶
- monocypher.utils.crypto_wipe(buf)¶
Wipe the given buf by filling it with zeros. Example:
>>> buf = bytearray(b'abc') >>> crypto_wipe(buf) >>> buf bytearray(b'\x00\x00\x00')
- Parameters:
buf – Any writable object with the buffer protocol, e.g.
bytearray
orarray.array
.