本文整理汇总了Python中Crypto.Hash.SHA256属性的典型用法代码示例。如果您正苦于以下问题:Python Hash.SHA256属性的具体用法?Python Hash.SHA256怎么用?Python Hash.SHA256使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类Crypto.Hash
的用法示例。
在下文中一共展示了Hash.SHA256属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def create(vek, keySizeBytes, certificatePath):
#print("VEK: " + str(binascii.hexlify(vek)))
publicKeyPem = open(certificatePath).read()
publicKey = RSA.importKey(publicKeyPem)
# Convert from PEM to DER
lines = publicKeyPem.replace(" ", '').split()
publicKeyDer = binascii.a2b_base64(''.join(lines[1:-1]))
cert = x509.load_pem_x509_certificate(SmartStr(publicKeyPem), default_backend())
subjectName = cert.subject.rfc4514_string()
serial = cert.serial_number
cipher = PKCS1_OAEP.new(key=publicKey, hashAlgo=SHA256, mgfunc=lambda x, y: pss.MGF1(x, y, SHA1))
wrapped_key = cipher.encrypt(vek)
#print("WrappedKey: " + str(binascii.hexlify(wrapped_key)))
return CertEncryptedKeyBag(subjectName, serial, keySizeBytes, wrapped_key)
示例2: testEncryptDecrypt1
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def testEncryptDecrypt1(self):
# Helper function to monitor what's requested from RNG
global asked
def localRng(N):
global asked
asked += N
return self.rng(N)
# Verify that OAEP is friendly to all hashes
for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
self.key1024._randfunc = localRng
cipher = PKCS.new(self.key1024, hashmod)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.failUnless(asked > hashmod.digest_size)
示例3: expand
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def expand( self ):
"""
Return the expanded output key material.
The output key material is calculated based on the given PRK, info and
L.
"""
tmp = ""
# Prevent the accidental re-use of output keying material.
if len(self.T) > 0:
raise base.PluggableTransportError("HKDF-SHA256 OKM must not "
"be re-used by application.")
while self.length > len(self.T):
tmp = Crypto.Hash.HMAC.new(self.prk, tmp + self.info +
chr(self.ctr),
Crypto.Hash.SHA256).digest()
self.T += tmp
self.ctr += 1
return self.T[:self.length]
示例4: auth_digital
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def auth_digital(self, title_id, title_version, device_token, ticket):
self.verify_ticket(ticket, title_id)
plain_key = get_random_bytes(16)
aes = AES.new(plain_key, AES.MODE_CBC, iv=bytes(16))
encrypted_ticket = aes.encrypt(pad(ticket, 16))
rsa_key = RSA.construct((RSA_MODULUS, RSA_EXPONENT))
rsa = PKCS1_OAEP.new(rsa_key, SHA256)
encrypted_key = rsa.encrypt(plain_key)
req = HTTPRequest.post("/v3/application_auth_token")
req.form["application_id"] = "%016x" %title_id
req.form["application_version"] = "%08x" %title_version
req.form["device_auth_token"] = device_token
req.form["media_type"] = "DIGITAL"
req.form["cert"] = b64encode(encrypted_ticket)
req.form["cert_key"] = b64encode(encrypted_key)
response = self.request(req, True)
return response.json
示例5: sha2_256
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def sha2_256(self):
"""Get SHA2-256 hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_256().output
"559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd"
"""
self.state = hashlib.sha256(self._convert_to_bytes()).hexdigest()
return self
示例6: sha2_512
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def sha2_512(self):
"""Get SHA2-512 hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_512().out()
21b4f4bd9e64ed355c3eb676a28ebedaf6d8f17bdc365995b319097153044080516bd083bfcce66121a3072646994c8430cc382b8dc543e84880183bf856cff5
"""
self.state = hashlib.sha512(self._convert_to_bytes()).hexdigest()
return self
示例7: sha2_512_truncate
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def sha2_512_truncate(self, truncate: int = 256):
"""Get SHA2-512/bits hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Args:
truncate (int, optional): The bits to truncate by. Defaults to 256
Returns:
Chepy: The Chepy object.
"""
assert truncate in [256, 224], "Valid truncates are 256, 224"
h = SHA512.new(self._convert_to_bytes(), truncate=str(truncate))
self.state = h.hexdigest()
return self
示例8: sha2_384
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def sha2_384(self):
"""Get SHA2-384 hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Returns:
Chepy: The Chepy object.
"""
self.state = hashlib.sha384(self._convert_to_bytes()).hexdigest()
return self
示例9: sha2_224
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def sha2_224(self):
"""Get SHA2-224 hash
The SHA-2 (Secure Hash Algorithm 2) hash functions were designed by the NSA. SHA-2
includes significant changes from its predecessor, SHA-1. The SHA-2 family consists of
hash functions with digests (hash values) that are 224, 256, 384 or 512 bits: SHA224,
SHA256, SHA384, SHA512. SHA-512 operates on 64-bit words. SHA-256 operates on 32-bit
words. SHA-384 is largely identical to SHA-512 but is truncated to 384 bytes. SHA-224
is largely identical to SHA-256 but is truncated to 224 bytes. SHA-512/224 and SHA-512/256
are truncated versions of SHA-512, but the initial values are generated using the method
described in Federal Information Processing Standards (FIPS) PUB 180-4.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_224().out()
"5cfe2cddbb9940fb4d8505e25ea77e763a0077693dbb01b1a6aa94f2"
"""
self.state = hashlib.sha224(self._convert_to_bytes()).hexdigest()
return self
示例10: unwrap
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def unwrap(data, authKey, keyWrapKey):
iv = data[-16:]
cipher = AES.new(keyWrapKey, AES.MODE_CBC, iv)
unwrapped = cipher.decrypt(data[:-16])
unwrapped = unpad(unwrapped)
kwa = unwrapped[-8:]
unwrapped = unwrapped[:-8]
hmac = HMAC(authKey, digestmod=SHA256)
hmac.update(unwrapped)
local_kwa = hmac.digest()[:8]
if kwa != local_kwa:
print("Unwrapped kwa does not match")
return unwrapped
示例11: testEncryptDecrypt2
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def testEncryptDecrypt2(self):
# Helper function to monitor what's requested from RNG
global asked
def localRng(N):
global asked
asked += N
return self.rng(N)
# Verify that OAEP is friendly to all hashes
for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD160):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.assertEqual(asked, hashmod.digest_size)
示例12: gen_random_key
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def gen_random_key(size=32):
"""
Generate a cryptographically-secure random key. This is done by using
Python 2.4's os.urandom, or PyCrypto.
"""
import os
if hasattr(os, "urandom"): # Python 2.4+
return os.urandom(size)
# Try using PyCrypto if available
try:
from Crypto.Util.randpool import RandomPool
from Crypto.Hash import SHA256
return RandomPool(hash=SHA256).get_bytes(size)
except ImportError:
print >>sys.stderr, "WARNING: The generated key will not be cryptographically-secure key. Consider using Python 2.4+ to generate the key, or install PyCrypto."
# Stupid random generation
import random
L = []
for i in range(size):
L.append(chr(random.randint(0, 255)))
return "".join(L)
示例13: test_hdkf
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def test_hdkf(self):
derived = HKDF(b"secret", 32, b"", SHA256).hex()
self.assertEqual(
derived, "2f34e5ff91ec85d53ca9b543683174d0cf550b60d5f52b24c97b386cfcf6cbbf"
)
k1 = PrivateKey(secret=bytes([2]))
self.assertEqual(k1.to_int(), 2)
k2 = PrivateKey(secret=bytes([3]))
self.assertEqual(k2.to_int(), 3)
self.assertEqual(encapsulate(k1, k2.public_key), decapsulate(k1.public_key, k2))
self.assertEqual(
encapsulate(k1, k2.public_key).hex(),
"6f982d63e8590c9d9b5b4c1959ff80315d772edd8f60287c9361d548d5200f82",
)
示例14: derive_key
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def derive_key(salt: str, passphrase: str, hd: bool = True) -> \
Union[int, Tuple[int, bytes]]:
key_length = 64 if hd else 32 # type: int
t1 = and_split(bytes(salt, "utf-8")) # type: Tuple[bytes, bytes]
salt1, salt2 = t1
t2 = and_split(bytes(passphrase, "utf-8")) # type: Tuple[bytes, bytes]
pass1, pass2 = t2
scrypt_key = scrypt.hash(
pass1, salt1,
N=1 << 18, buflen=key_length) # type: bytes
pbkdf2_key = pbkdf2.PBKDF2(
pass2, salt2,
iterations=1 << 16,
digestmodule=SHA256).read(key_length) # type: bytes
merged = xor_merge(scrypt_key, pbkdf2_key) # type: bytes
if hd:
secret_exp = int(merged[0:32].hex(), 16) # type: int
chain_code = merged[32:] # type: bytes
return secret_exp, chain_code
return int(merged.hex(), 16)
示例15: runTest
# 需要导入模块: from Crypto import Hash [as 别名]
# 或者: from Crypto.Hash import SHA256 [as 别名]
def runTest(self):
key = RSA.generate(1024)
signer = pkcs1_15.new(key)
hash_names = ("MD2", "MD4", "MD5", "RIPEMD160", "SHA1",
"SHA224", "SHA256", "SHA384", "SHA512",
"SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512")
for name in hash_names:
hashed = load_hash_by_name(name).new(b"Test")
signer.sign(hashed)
from Crypto.Hash import BLAKE2b, BLAKE2s
for hash_size in (20, 32, 48, 64):
hashed_b = BLAKE2b.new(digest_bytes=hash_size, data=b"Test")
signer.sign(hashed_b)
for hash_size in (16, 20, 28, 32):
hashed_s = BLAKE2s.new(digest_bytes=hash_size, data=b"Test")
signer.sign(hashed_s)