def digest(self): """Return the digest of the strings passed to the update() method so far.""" digest = ffi.new("unsigned char[]", self.hash_size // 8) shc = ffi.new("SHA2_CTX *") # Create a temporary ctx that copies self.ctx because SHA512_t_Final # will zeroize the context passed in. lib.memcpy(shc, self.ctx, ffi.sizeof("SHA2_CTX")) lib.SHA2Final(digest, shc) return b"".join(six.int2byte(i) for i in digest)
def __init__(self, message=None, t=256): self.ctx = ffi.new("SHA2_CTX *") if t == 256: lib.SHA2Init(lib.SHA512_256, self.ctx) elif t == 224: lib.SHA2Init(lib.SHA512_224, self.ctx) else: raise ValueError("The module only supports " "SHA512/256 or SHA512/224.") self.hash_size = t if message: self.update(message)