我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用cryptography.hazmat.primitives.hashes.HashContext()。
def sign(self, data, hash_context): if not isinstance(hash_context, hashes.HashContext): raise TypeError("hash_context must be an instance of hashes.HashContext.") signer = self._key.signer(hashes.SHA256()) signer._hash_ctx = hash_context signer.update(data) r, s = decode_dss_signature(signer.finalize()) # return long_to_bytes(r, 20) + long_to_bytes(s, 20) size = self.private_numbers.public_numbers.parameter_numbers.q.bit_length() // 8 return long_to_bytes(r, size) + long_to_bytes(s, size)
def verify(self, signature, data, hash_context): if not isinstance(hash_context, hashes.HashContext): raise TypeError("hash_context must be an instance of hashes.HashContext.") size = self.public_numbers.parameter_numbers.q.bit_length() // 8 r, s = (bytes_to_long(value) for value in read_content(signature, '{0}s{0}s'.format(size))) # r, s = (bytes_to_long(value) for value in read_content(signature, '20s20s')) verifier = self._key.verifier(encode_dss_signature(r, s), hashes.SHA256()) verifier._hash_ctx = hash_context verifier.update(data) try: verifier.verify() except InvalidSignature: raise ValueError("invalid signature")