1# This file is dual licensed under the terms of the Apache License, Version 2# 2.0, and the BSD License. See the LICENSE file in the root of this repository 3# for complete details. 4 5from __future__ import absolute_import, division, print_function 6 7import warnings 8 9from asn1crypto.algos import DSASignature 10 11import six 12 13from cryptography import utils 14from cryptography.hazmat.primitives import hashes 15 16 17def decode_rfc6979_signature(signature): 18 warnings.warn( 19 "decode_rfc6979_signature is deprecated and will " 20 "be removed in a future version, use decode_dss_signature instead.", 21 utils.PersistentlyDeprecated, 22 stacklevel=2 23 ) 24 return decode_dss_signature(signature) 25 26 27def decode_dss_signature(signature): 28 data = DSASignature.load(signature, strict=True).native 29 return data['r'], data['s'] 30 31 32def encode_rfc6979_signature(r, s): 33 warnings.warn( 34 "encode_rfc6979_signature is deprecated and will " 35 "be removed in a future version, use encode_dss_signature instead.", 36 utils.PersistentlyDeprecated, 37 stacklevel=2 38 ) 39 return encode_dss_signature(r, s) 40 41 42def encode_dss_signature(r, s): 43 if ( 44 not isinstance(r, six.integer_types) or 45 not isinstance(s, six.integer_types) 46 ): 47 raise ValueError("Both r and s must be integers") 48 49 return DSASignature({'r': r, 's': s}).dump() 50 51 52class Prehashed(object): 53 def __init__(self, algorithm): 54 if not isinstance(algorithm, hashes.HashAlgorithm): 55 raise TypeError("Expected instance of HashAlgorithm.") 56 57 self._algorithm = algorithm 58 self._digest_size = algorithm.digest_size 59 60 digest_size = utils.read_only_property("_digest_size") 61