本文整理汇总了Python中cryptography.hazmat.primitives.hashes.SHA512属性的典型用法代码示例。如果您正苦于以下问题:Python hashes.SHA512属性的具体用法?Python hashes.SHA512怎么用?Python hashes.SHA512使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cryptography.hazmat.primitives.hashes
的用法示例。
在下文中一共展示了hashes.SHA512属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_key
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def _get_key(self, alg, key, p2s, p2c):
if isinstance(key, bytes):
plain = key
else:
plain = key.encode('utf8')
salt = bytes(self.name.encode('utf8')) + b'\x00' + p2s
if self.hashsize == 256:
hashalg = hashes.SHA256()
elif self.hashsize == 384:
hashalg = hashes.SHA384()
elif self.hashsize == 512:
hashalg = hashes.SHA512()
else:
raise ValueError('Unknown Hash Size')
kdf = PBKDF2HMAC(algorithm=hashalg, length=_inbytes(self.keysize),
salt=salt, iterations=p2c, backend=self.backend)
rk = kdf.derive(plain)
if _bitsize(rk) != self.keysize:
raise InvalidJWEKeyLength(self.keysize, len(rk))
return JWK(kty="oct", use="enc", k=base64url_encode(rk))
示例2: __init__
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def __init__(self, key, length, algorithm, backend):
if not isinstance(backend, HMACBackend):
raise UnsupportedAlgorithm(
"Backend object does not implement HMACBackend.",
_Reasons.BACKEND_MISSING_INTERFACE
)
if len(key) < 16:
raise ValueError("Key length has to be at least 128 bits.")
if not isinstance(length, six.integer_types):
raise TypeError("Length parameter must be an integer type.")
if length < 6 or length > 8:
raise ValueError("Length of HOTP has to be between 6 to 8.")
if not isinstance(algorithm, (SHA1, SHA256, SHA512)):
raise TypeError("Algorithm must be SHA1, SHA256 or SHA512.")
self._key = key
self._length = length
self._algorithm = algorithm
self._backend = backend
示例3: __init__
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def __init__(self, key, length, algorithm, backend,
enforce_key_length=True):
if not isinstance(backend, HMACBackend):
raise UnsupportedAlgorithm(
"Backend object does not implement HMACBackend.",
_Reasons.BACKEND_MISSING_INTERFACE
)
if len(key) < 16 and enforce_key_length is True:
raise ValueError("Key length has to be at least 128 bits.")
if not isinstance(length, six.integer_types):
raise TypeError("Length parameter must be an integer type.")
if length < 6 or length > 8:
raise ValueError("Length of HOTP has to be between 6 to 8.")
if not isinstance(algorithm, (SHA1, SHA256, SHA512)):
raise TypeError("Algorithm must be SHA1, SHA256 or SHA512.")
self._key = key
self._length = length
self._algorithm = algorithm
self._backend = backend
示例4: insert_encrypted_value
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def insert_encrypted_value(self, element, encryption_algorithm, encrypted_string):
"""
Add an encrypted value (key) to the document.
"""
encrypted_value = element_tree.SubElement(element, "{urn:ietf:params:xml:ns:keyprov:pskc}EncryptedValue")
encryption_method = element_tree.SubElement(encrypted_value, "{http://www.w3.org/2001/04/xmlenc#}EncryptionMethod")
encryption_method.set("Algorithm", encryption_algorithm)
cipher_data = element_tree.SubElement(encrypted_value, "{http://www.w3.org/2001/04/xmlenc#}CipherData")
cipher_value = element_tree.SubElement(cipher_data, "{http://www.w3.org/2001/04/xmlenc#}CipherValue")
cipher_value.text = encrypted_string
# calculate and set MAC using HMAC-SHA512 over data in CipherValue
if not self.hmac_key:
raise Exception("Missing HMAC key")
value_mac = element_tree.SubElement(element, "{urn:ietf:params:xml:ns:keyprov:pskc}ValueMAC")
hmac_instance = hmac.HMAC(self.hmac_key, hashes.SHA512(), backend=default_backend())
hmac_instance.update(base64.b64decode(encrypted_string))
value_mac.text = base64.b64encode(hmac_instance.finalize()).decode('utf-8')
示例5: _cryptography_hash_function
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def _cryptography_hash_function(algorithm: DigestAlgorithm) -> Union[None, Type[hashes.SHA1], Type[hashes.SHA256], Type[hashes.SHA512]]:
"""Find the cryptography hash function given the string output from asn1crypto SignedDigestAlgorithm.
Todo: There should be a better way to do this?
Args:
algorithm (DigestAlgorithm): The asn1crypto Signed Digest Algorithm
Returns:
Union[Type[hashes.SHA1], Type[hashes.SHA256], Type[hashes.SHA512]] A cryptography hash function for use with
signature verification.
"""
hash_algo = algorithm['algorithm'].native
if hash_algo == "sha1":
return hashes.SHA1
elif hash_algo == "sha256":
return hashes.SHA256
elif hash_algo == "sha512":
return hashes.SHA512
else:
return None
示例6: test_basic
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def test_basic(self):
hash_cls = hashes.SHA512
enc_cls = Encoding.DER
stdout, stderr = self.cmd('cache_crls')
self.assertEqual(stdout, '')
self.assertEqual(stderr, '')
for name, ca in self.cas.items():
key = get_crl_cache_key(ca.serial, hash_cls, enc_cls, 'ca')
crl = x509.load_der_x509_crl(cache.get(key), default_backend())
self.assertIsNotNone(crl)
self.assertIsInstance(crl.signature_hash_algorithm, hash_cls)
key = get_crl_cache_key(ca.serial, hash_cls, enc_cls, 'user')
crl = x509.load_der_x509_crl(cache.get(key), default_backend())
self.assertIsNotNone(crl)
示例7: test_serial
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def test_serial(self):
ca = self.cas['root']
hash_cls = hashes.SHA512
enc_cls = Encoding.DER
stdout, stderr = self.cmd('cache_crls', ca.serial)
self.assertEqual(stdout, '')
self.assertEqual(stderr, '')
key = get_crl_cache_key(ca.serial, hash_cls, enc_cls, 'ca')
crl = x509.load_der_x509_crl(cache.get(key), default_backend())
self.assertIsNotNone(crl)
self.assertIsInstance(crl.signature_hash_algorithm, hash_cls)
key = get_crl_cache_key(ca.serial, hash_cls, enc_cls, 'user')
crl = x509.load_der_x509_crl(cache.get(key), default_backend())
self.assertIsNotNone(crl)
示例8: test_file
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def test_file(self):
path = os.path.join(ca_settings.CA_DIR, 'crl-test.crl')
stdout, stderr = self.cmd('dump_crl', path, ca=self.ca, scope='user',
stdout=BytesIO(), stderr=BytesIO())
self.assertEqual(stdout, b'')
self.assertEqual(stderr, b'')
with open(path, 'rb') as stream:
crl = x509.load_pem_x509_crl(stream.read(), default_backend())
self.assertIsInstance(crl.signature_hash_algorithm, hashes.SHA512)
self.assertEqual(list(crl), [])
# test an output path that doesn't exist
path = os.path.join(ca_settings.CA_DIR, 'test', 'crl-test.crl')
msg = r"^\[Errno 2\] No such file or directory: '%s'$" % re.escape(path)
with self.assertCommandError(msg):
self.cmd('dump_crl', path, ca=self.ca, scope='user', stdout=BytesIO(), stderr=BytesIO())
示例9: test_password
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def test_password(self):
ca = self.cas['pwd']
# Giving no password raises a CommandError
with self.assertCommandError('^Password was not given but private key is encrypted$'):
self.cmd('dump_crl', ca=ca, scope='user')
# False password
ca = CertificateAuthority.objects.get(pk=ca.pk)
with self.assertCommandError(self.re_false_password):
self.cmd('dump_crl', ca=ca, scope='user', password=b'wrong')
stdout, stderr = self.cmd('dump_crl', ca=ca, scope='user', password=certs['pwd']['password'],
stdout=BytesIO(), stderr=BytesIO())
self.assertEqual(stderr, b'')
crl = x509.load_pem_x509_crl(stdout, default_backend())
self.assertIsInstance(crl.signature_hash_algorithm, hashes.SHA512)
self.assertEqual(list(crl), [])
示例10: test_ca_crl
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def test_ca_crl(self):
ca = self.cas['root']
child = self.cas['child']
self.assertIsNotNone(child.key(password=None))
self.assertNotRevoked(child)
stdout, stderr = self.cmd('dump_crl', ca=ca, scope='ca',
stdout=BytesIO(), stderr=BytesIO())
self.assertEqual(stderr, b'')
crl = x509.load_pem_x509_crl(stdout, default_backend())
self.assertIsInstance(crl.signature_hash_algorithm, hashes.SHA512)
self.assertEqual(list(crl), [])
# revoke the CA and see if it's there
child.revoke()
self.assertRevoked(child)
stdout, stderr = self.cmd('dump_crl', ca=ca, scope='ca', stdout=BytesIO(), stderr=BytesIO())
self.assertEqual(stderr, b'')
crl = x509.load_pem_x509_crl(stdout, default_backend())
self.assertIsInstance(crl.signature_hash_algorithm, hashes.SHA512)
self.assertEqual(len(list(crl)), 1)
self.assertEqual(crl[0].serial_number, child.x509.serial_number)
self.assertEqual(len(crl[0].extensions), 0)
示例11: __init__
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def __init__(self, curve_class, nist_name):
self.nist_name = nist_name
self.key_length = curve_class.key_size
# Defined in RFC 5656 6.2
self.key_format_identifier = "ecdsa-sha2-" + self.nist_name
# Defined in RFC 5656 6.2.1
if self.key_length <= 256:
self.hash_object = hashes.SHA256
elif self.key_length <= 384:
self.hash_object = hashes.SHA384
else:
self.hash_object = hashes.SHA512
self.curve_class = curve_class
示例12: _sign
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def _sign(self, message):
""" Uses the decrypted private key to sign the message. """
if self.private_key:
keydata = self.private_key
elif os.path.exists(self.private_key_file):
with open(self.private_key_file) as private_key:
keydata = private_key.read()
else:
raise RuntimeError('The private key does not exist.')
private_key = serialization.load_pem_private_key(
str.encode(keydata),
password=None,
backend=default_backend()
)
signature = private_key.sign(
str.encode(message),
padding.PKCS1v15(),
hashes.SHA512(),
)
signature = base64.b64encode(signature)
signature = quote_plus(signature)
return signature
示例13: get_default_algorithms
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def get_default_algorithms():
"""
Returns the algorithms that are implemented by the library.
"""
default_algorithms = {
'none': NoneAlgorithm(),
'HS256': HMACAlgorithm(HMACAlgorithm.SHA256),
'HS384': HMACAlgorithm(HMACAlgorithm.SHA384),
'HS512': HMACAlgorithm(HMACAlgorithm.SHA512)
}
if has_crypto:
default_algorithms.update({
'RS256': RSAAlgorithm(RSAAlgorithm.SHA256),
'RS384': RSAAlgorithm(RSAAlgorithm.SHA384),
'RS512': RSAAlgorithm(RSAAlgorithm.SHA512),
'ES256': ECAlgorithm(ECAlgorithm.SHA256),
'ES384': ECAlgorithm(ECAlgorithm.SHA384),
'ES512': ECAlgorithm(ECAlgorithm.SHA512),
'PS256': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256),
'PS384': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA384),
'PS512': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA512)
})
return default_algorithms
示例14: get_default_algorithms
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def get_default_algorithms():
"""
Returns the algorithms that are implemented by the library.
"""
default_algorithms = {
'none': NoneAlgorithm(),
'HS256': HMACAlgorithm(HMACAlgorithm.SHA256),
'HS384': HMACAlgorithm(HMACAlgorithm.SHA384),
'HS512': HMACAlgorithm(HMACAlgorithm.SHA512)
}
if has_crypto:
default_algorithms.update({
'RS256': RSAAlgorithm(RSAAlgorithm.SHA256),
'RS384': RSAAlgorithm(RSAAlgorithm.SHA384),
'RS512': RSAAlgorithm(RSAAlgorithm.SHA512),
'ES256': ECAlgorithm(ECAlgorithm.SHA256),
'ES384': ECAlgorithm(ECAlgorithm.SHA384),
'ES521': ECAlgorithm(ECAlgorithm.SHA512),
'ES512': ECAlgorithm(ECAlgorithm.SHA512), # Backward compat for #219 fix
'PS256': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256),
'PS384': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA384),
'PS512': RSAPSSAlgorithm(RSAPSSAlgorithm.SHA512)
})
return default_algorithms
示例15: __init__
# 需要导入模块: from cryptography.hazmat.primitives import hashes [as 别名]
# 或者: from cryptography.hazmat.primitives.hashes import SHA512 [as 别名]
def __init__(self):
super(_HS512, self).__init__(hashes.SHA512())