Python cryptography.hazmat.primitives.hashes 模块,HashContext() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用cryptography.hazmat.primitives.hashes.HashContext()

项目:python-otr    作者:AGProjects    | 项目源码 | 文件源码
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)
项目:python-otr    作者:AGProjects    | 项目源码 | 文件源码
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")