Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
dsa.py180 linesDownload Raw Back to asymmetric
1# This file is dual licensed under the terms of the Apache License, Version2# 2.0, and the BSD License. See the LICENSE file in the root of this repository3# for complete details.4 5from __future__ import annotations6 7import abc8import typing9 10from cryptography.hazmat.bindings._rust import openssl as rust_openssl11from cryptography.hazmat.primitives import _serialization, hashes12from cryptography.hazmat.primitives.asymmetric import utils as asym_utils13from cryptography.utils import Buffer14 15 16class DSAParameters(metaclass=abc.ABCMeta):17    @abc.abstractmethod18    def generate_private_key(self) -> DSAPrivateKey:19        """20        Generates and returns a DSAPrivateKey.21        """22 23    @abc.abstractmethod24    def parameter_numbers(self) -> DSAParameterNumbers:25        """26        Returns a DSAParameterNumbers.27        """28 29 30DSAParametersWithNumbers = DSAParameters31DSAParameters.register(rust_openssl.dsa.DSAParameters)32 33 34class DSAPrivateKey(metaclass=abc.ABCMeta):35    @property36    @abc.abstractmethod37    def key_size(self) -> int:38        """39        The bit length of the prime modulus.40        """41 42    @abc.abstractmethod43    def public_key(self) -> DSAPublicKey:44        """45        The DSAPublicKey associated with this private key.46        """47 48    @abc.abstractmethod49    def parameters(self) -> DSAParameters:50        """51        The DSAParameters object associated with this private key.52        """53 54    @abc.abstractmethod55    def sign(56        self,57        data: Buffer,58        algorithm: asym_utils.Prehashed | hashes.HashAlgorithm,59    ) -> bytes:60        """61        Signs the data62        """63 64    @abc.abstractmethod65    def private_numbers(self) -> DSAPrivateNumbers:66        """67        Returns a DSAPrivateNumbers.68        """69 70    @abc.abstractmethod71    def private_bytes(72        self,73        encoding: _serialization.Encoding,74        format: _serialization.PrivateFormat,75        encryption_algorithm: _serialization.KeySerializationEncryption,76    ) -> bytes:77        """78        Returns the key serialized as bytes.79        """80 81    @abc.abstractmethod82    def __copy__(self) -> DSAPrivateKey:83        """84        Returns a copy.85        """86 87    @abc.abstractmethod88    def __deepcopy__(self, memo: dict) -> DSAPrivateKey:89        """90        Returns a deep copy.91        """92 93 94DSAPrivateKeyWithSerialization = DSAPrivateKey95DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey)96 97 98class DSAPublicKey(metaclass=abc.ABCMeta):99    @property100    @abc.abstractmethod101    def key_size(self) -> int:102        """103        The bit length of the prime modulus.104        """105 106    @abc.abstractmethod107    def parameters(self) -> DSAParameters:108        """109        The DSAParameters object associated with this public key.110        """111 112    @abc.abstractmethod113    def public_numbers(self) -> DSAPublicNumbers:114        """115        Returns a DSAPublicNumbers.116        """117 118    @abc.abstractmethod119    def public_bytes(120        self,121        encoding: _serialization.Encoding,122        format: _serialization.PublicFormat,123    ) -> bytes:124        """125        Returns the key serialized as bytes.126        """127 128    @abc.abstractmethod129    def verify(130        self,131        signature: Buffer,132        data: Buffer,133        algorithm: asym_utils.Prehashed | hashes.HashAlgorithm,134    ) -> None:135        """136        Verifies the signature of the data.137        """138 139    @abc.abstractmethod140    def __eq__(self, other: object) -> bool:141        """142        Checks equality.143        """144 145    @abc.abstractmethod146    def __copy__(self) -> DSAPublicKey:147        """148        Returns a copy.149        """150 151    @abc.abstractmethod152    def __deepcopy__(self, memo: dict) -> DSAPublicKey:153        """154        Returns a deep copy.155        """156 157 158DSAPublicKeyWithSerialization = DSAPublicKey159DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey)160 161DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers162DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers163DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers164 165 166def generate_parameters(167    key_size: int, backend: typing.Any = None168) -> DSAParameters:169    if key_size not in (1024, 2048, 3072, 4096):170        raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.")171 172    return rust_openssl.dsa.generate_parameters(key_size)173 174 175def generate_private_key(176    key_size: int, backend: typing.Any = None177) -> DSAPrivateKey:178    parameters = generate_parameters(key_size)179    return parameters.generate_private_key()180 
codekingpro/portable-devtools · Team Ai