• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7from cryptography import utils
8from cryptography.hazmat._der import (
9    DERReader,
10    INTEGER,
11    SEQUENCE,
12    encode_der,
13    encode_der_integer,
14)
15from cryptography.hazmat.primitives import hashes
16
17
18def decode_dss_signature(signature):
19    with DERReader(signature).read_single_element(SEQUENCE) as seq:
20        r = seq.read_element(INTEGER).as_integer()
21        s = seq.read_element(INTEGER).as_integer()
22        return r, s
23
24
25def encode_dss_signature(r, s):
26    return encode_der(
27        SEQUENCE,
28        encode_der(INTEGER, encode_der_integer(r)),
29        encode_der(INTEGER, encode_der_integer(s)),
30    )
31
32
33class Prehashed(object):
34    def __init__(self, algorithm):
35        if not isinstance(algorithm, hashes.HashAlgorithm):
36            raise TypeError("Expected instance of HashAlgorithm.")
37
38        self._algorithm = algorithm
39        self._digest_size = algorithm.digest_size
40
41    digest_size = utils.read_only_property("_digest_size")
42