本文整理汇总了Python中Crypto.Hash.SHA1.new方法的典型用法代码示例。如果您正苦于以下问题:Python SHA1.new方法的具体用法?Python SHA1.new怎么用?Python SHA1.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Hash.SHA1
的用法示例。
在下文中一共展示了SHA1.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addHmac
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def addHmac(string, Version):
isHmacVersion = False
# "Encrypt-then-MAC" feature introduced in R1 2017
# Required for >= "2017.1.118" (e.g. "2017.1.118", "2017.1.228", "2017.2.503" etc.)
if int(Version[:4]) >= 2017:
isHmacVersion = True
hmac = HMAC.new(
bytes(RAUCipher.HASHKEY.encode()),
string.encode(),
SHA256.new()
)
hmac = base64.b64encode(hmac.digest()).decode()
return string + hmac if isHmacVersion else string
示例2: test_verify
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def test_verify(self, tv):
self._id = "Wycheproof ECDSA Test #%d (%s, %s)" % (tv.id, tv.comment, tv.filename)
hashed_msg = tv.hash_module.new(tv.msg)
signer = DSS.new(tv.key, 'fips-186-3', encoding=tv.encoding)
try:
signature = signer.verify(hashed_msg, tv.sig)
except ValueError as e:
if tv.warning:
return
if tv.comment == "k*G has a large x-coordinate":
return
assert not tv.valid
else:
assert tv.valid
self.warn(tv)
示例3: runTest
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def runTest(self):
key = RSA.generate(1280)
signer = pss.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)
示例4: get_hash_module
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def get_hash_module(hash_name):
if hash_name == "SHA-512":
hash_module = SHA512
elif hash_name == "SHA-512/224":
hash_module = SHA512.new(truncate="224")
elif hash_name == "SHA-512/256":
hash_module = SHA512.new(truncate="256")
elif hash_name == "SHA-384":
hash_module = SHA384
elif hash_name == "SHA-256":
hash_module = SHA256
elif hash_name == "SHA-224":
hash_module = SHA224
elif hash_name == "SHA-1":
hash_module = SHA1
else:
raise ValueError("Unknown hash algorithm: " + hash_name)
return hash_module
示例5: test_streaming
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def test_streaming(self):
"""Verify that an arbitrary number of bytes can be encrypted/decrypted"""
from Crypto.Hash import SHA1
segments = (1, 3, 5, 7, 11, 17, 23)
total = sum(segments)
pt = b("")
while len(pt) < total:
pt += SHA1.new(pt).digest()
cipher1 = ChaCha20.new(key=b("7") * 32, nonce=b("t") * 8)
ct = cipher1.encrypt(pt)
cipher2 = ChaCha20.new(key=b("7") * 32, nonce=b("t") * 8)
cipher3 = ChaCha20.new(key=b("7") * 32, nonce=b("t") * 8)
idx = 0
for segment in segments:
self.assertEqual(cipher2.decrypt(ct[idx:idx+segment]), pt[idx:idx+segment])
self.assertEqual(cipher3.encrypt(pt[idx:idx+segment]), ct[idx:idx+segment])
idx += segment
示例6: test_verify
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def test_verify(self, tv):
self._id = "Wycheproof ECDSA Test #%d (%s)" % (tv.id, tv.comment)
hashed_msg = tv.hash_module.new(tv.msg)
signer = DSS.new(tv.key, 'fips-186-3', encoding='der')
try:
signature = signer.verify(hashed_msg, tv.sig)
except ValueError as e:
if tv.warning:
return
if tv.comment == "k*G has a large x-coordinate":
return
assert not tv.valid
else:
assert tv.valid
self.warn(tv)
示例7: runTest
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [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)
示例8: verify_incoming_request
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def verify_incoming_request(form_data: dict) -> bool:
"""verify the incoming form_data"""
# copy form data
input_data = form_data.copy()
signature = input_data["p_signature"]
# Remove the p_signature parameter
del input_data["p_signature"]
# Ensure all the data fields are strings
for field in input_data:
input_data[field] = str(input_data[field])
# Sort the data
sorted_data = collections.OrderedDict(sorted(input_data.items()))
# and serialize the fields
serialized_data = phpserialize.dumps(sorted_data)
# verify the data
key = RSA.importKey(public_key_der)
digest = SHA1.new()
digest.update(serialized_data)
verifier = PKCS1_v1_5.new(key)
signature = base64.b64decode(signature)
if verifier.verify(digest, signature):
return True
return False
示例9: hash_SHA512
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def hash_SHA512(data) -> str:
if isinstance(data, str):
hash_digest = SHA512.new(data=str.encode(data))
else:
hash_digest = SHA512.new(data=data)
return hash_digest.hexdigest()
示例10: hash_SHA256
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def hash_SHA256(data) -> str:
if isinstance(data, str):
hash_digest = SHA256.new(data=str.encode(data))
else:
hash_digest = SHA256.new(data=data)
return hash_digest.hexdigest()
示例11: hash_SHA1
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def hash_SHA1(data) -> str:
if isinstance(data, str):
hash_digest = SHA1.new(data=str.encode(data))
else:
hash_digest = SHA1.new(data=data)
return hash_digest.hexdigest()
示例12: hash_MD5
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def hash_MD5(data) -> str:
if isinstance(data, str):
hash_digest = MD5.new(data=str.encode(data))
else:
hash_digest = MD5.new(data=data)
return hash_digest.hexdigest()
# https://pycryptodome.readthedocs.io/en/latest/src/examples.html
示例13: decrypt_AES256
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def decrypt_AES256(data: bytes, key: bytes):
# hmac should include IV
mac = data[-32:] # sha256 hmac at the end
iv = data[:16] # 16 Bytes for IV at the beginning
message = data[16:-32] # the rest is the message
h = HMAC.new(key=key, msg=iv + message, digestmod=SHA256)
h.verify(mac)
decryption_cipher = AES.new(key, AES.MODE_CBC, iv=iv)
decrypted_message = decryption_cipher.decrypt(message)
# print(decrypted_message)
# now to remove any padding that was added on to make it the right block size of 16
return unpad(decrypted_message, 16)
示例14: encrypt_pub_key
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def encrypt_pub_key(data: bytes, key: bytes):
recipient_key = RSA.import_key(key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted_data = cipher_rsa.encrypt(data)
return encrypted_data
示例15: decrypt_pub_key
# 需要导入模块: from Crypto.Hash import SHA1 [as 别名]
# 或者: from Crypto.Hash.SHA1 import new [as 别名]
def decrypt_pub_key(data: bytes, key: bytes):
recipient_key = RSA.import_key(key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
decrypted_data = cipher_rsa.decrypt(data)
return decrypted_data