class TestSHA512224(object): test_sha512_224 = generate_hash_test( load_hash_vectors, os.path.join("hashes", "SHA2"), ["SHA512_224LongMsg.rsp", "SHA512_224ShortMsg.rsp"], hashes.SHA512_224(), )
async def SHA512_224(message): message = message.encode() kdf = PBKDF2HMAC( algorithm=hashes.SHA512_224(), length=hashLen, salt= b'\xe7\xde\xc1\xf0\x81\x99\xde}\xa4\xb50u;&\x06\xe7\xa4\xbfn\xbc', iterations=hashIter, backend=default_backend()) output = base64.urlsafe_b64encode(kdf.derive(message)) return (output)
def _get_hasher(T: int): hash_algo = None if T == Dss1Verifier.TRAILER_IMPLICIT or T == Dss1Verifier.TRAILER_SHA1: hash_algo = hashes.SHA1() elif T == Dss1Verifier.TRAILER_SHA224: hash_algo = hashes.SHA224() elif T == Dss1Verifier.TRAILER_SHA256: hash_algo = hashes.SHA256() elif T == Dss1Verifier.TRAILER_SHA384: hash_algo = hashes.SHA384() elif T == Dss1Verifier.TRAILER_SHA512: hash_algo = hashes.SHA512() elif T == Dss1Verifier.TRAILER_SHA512_224: hash_algo = hashes.SHA512_224() elif T == Dss1Verifier.TRAILER_SHA512_256: hash_algo = hashes.SHA512_256() if hash_algo is None: raise Dss1VerifierError("Unrecognized hash algorithm in signature") return hashes.Hash(hash_algo, backend=default_backend())
) @pytest.mark.requires_backend_interface(interface=HashBackend) class TestSHA512(object): test_sha512 = generate_hash_test( load_hash_vectors, os.path.join("hashes", "SHA2"), [ "SHA512LongMsg.rsp", "SHA512ShortMsg.rsp", ], hashes.SHA512(), ) @pytest.mark.supported( only_if=lambda backend: backend.hash_supported(hashes.SHA512_224()), skip_message="Does not support SHA512/224", ) @pytest.mark.requires_backend_interface(interface=HashBackend) class TestSHA512224(object): test_sha512_224 = generate_hash_test( load_hash_vectors, os.path.join("hashes", "SHA2"), [ "SHA512_224LongMsg.rsp", "SHA512_224ShortMsg.rsp", ], hashes.SHA512_224(), )
from asn1crypto.algos import SignedDigestAlgorithm from cryptography.hazmat.primitives import hashes _STR_TO_HASH_ALGO = { 'md5' : hashes.MD5(), 'sha1' : hashes.SHA1(), 'sha224' : hashes.SHA224(), 'sha256' : hashes.SHA256(), 'sha384' : hashes.SHA384(), 'sha512' : hashes.SHA512(), 'sha512_224' : hashes.SHA512_224(), 'sha512_256' : hashes.SHA512_256(), 'sha3_224' : hashes.SHA3_224(), 'sha3_256' : hashes.SHA3_256(), 'sha3_384' : hashes.SHA3_384(), 'sha3_512' : hashes.SHA3_512(), } def get_hash_algo_by_name(hash_algo: str): hash_algo = hash_algo.lower() if hash_algo not in _STR_TO_HASH_ALGO: raise ValueError("Invalid hash algorithm '{}'".format(hash_algo)) return _STR_TO_HASH_ALGO[hash_algo] def update_sig_algo_if_no_hash_algo(sig_algo: SignedDigestAlgorithm, hash_algo: str): n_sig_algo = sig_algo['algorithm'].native if n_sig_algo == 'rsassa_pkcs1v15' or n_sig_algo == 'ecdsa' or n_sig_algo == 'dsa': if n_sig_algo == 'rsassa_pkcs1v15': n_sig_algo = 'rsa' if hash_algo == 'md5':
def _publish_certificate(req: object, issuer: dict): logger.info('Create a certificate builder...') crt_builder = x509.CertificateBuilder() crt_builder = crt_builder.subject_name(name=req._subject()) crt_builder = crt_builder.public_key(key=req._public_key()) for extension in req._extensions(): crt_builder = crt_builder.add_extension(extension=extension.value, critical=extension.critical) logger.info('Ready to sign the certificate request...') if not isinstance(issuer['valid_year'], int): issuer['valid_year'] = int(issuer['valid_year']) if issuer['is_ca'] == 'true': issuer['is_ca'] = True if issuer['is_ca'] == 'false': issuer['is_ca'] = False crt_builder = crt_builder.not_valid_before(time=datetime.datetime.today()) crt_builder = crt_builder.not_valid_after( time=datetime.datetime.today() + datetime.timedelta(days=issuer['valid_year'] * 365)) crt_builder = crt_builder.serial_number(number=x509.random_serial_number()) crt_builder = crt_builder.add_extension(extension=x509.BasicConstraints( ca=issuer['is_ca'], path_length=None), critical=True) logger.info( 'Select CA to sign the certificate signing request and output certificate' ) if issuer['ca'] == 'SelfSign': logger.debug('CA is self-signed') crt_builder = crt_builder.issuer_name(name=req._subject()) ca_key = req.private_key() else: ca_dir = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'common_static/ca') ca_file = "{}/{}.pfx".format(ca_dir, issuer['ca']) logger.debug("CA file is located in {}".format(ca_file)) with open(file=ca_file, mode='rb') as f: ca_bytes = f.read() logger.debug('CA file content is \n{}'.format(ca_bytes)) crt_chain = ReadCertificateChain({ 'bytes': ca_bytes, 'password': b'Cisco123!' }) ca_crt = crt_chain.certificate(data_type='object') ca_key = crt_chain.private_key(data_type='object') crt_builder = crt_builder.issuer_name(name=ca_crt.subject) hash_obj_list = { hashes.MD5(), hashes.SHA1(), hashes.SHA224(), hashes.SHA256(), hashes.SHA384(), hashes.SHA512(), hashes.SHA512_224(), hashes.SHA512_256(), hashes.SHA3_224(), hashes.SHA3_256(), hashes.SHA3_384(), hashes.SHA3_512() } for hash_obj in hash_obj_list: if issuer['hash_alg'] == hash_obj.name: hash_algor = hash_obj break else: hash_algor = hashes.MD5() return crt_builder.sign(private_key=ca_key, algorithm=hash_algor, backend=default_backend())