本文整理汇总了Python中Crypto.Signature.PKCS1_v1_5.new方法的典型用法代码示例。如果您正苦于以下问题:Python PKCS1_v1_5.new方法的具体用法?Python PKCS1_v1_5.new怎么用?Python PKCS1_v1_5.new使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Signature.PKCS1_v1_5
的用法示例。
在下文中一共展示了PKCS1_v1_5.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def verify(self, message, signature):
"""Verifies a message against a signature.
Args:
message: string, The message to verify.
signature: string, The signature on the message.
Returns:
True if message was signed by the private key associated with the public
key that this object was constructed with.
"""
try:
return PKCS1_v1_5.new(self._pubkey).verify(
SHA256.new(message), signature)
except:
return False
示例2: testSign1
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def testSign1(self):
for i in range(len(self._testData)):
row = self._testData[i]
# Build the key
if isStr(row[0]):
key = RSA.importKey(row[0])
else:
comps = [ long(rws(row[0][x]),16) for x in ('n','e','d') ]
key = RSA.construct(comps)
h = row[3].new()
# Data to sign can either be in hex form or not
try:
h.update(t2b(row[1]))
except:
h.update(b(row[1]))
# The real test
signer = PKCS.new(key)
self.failUnless(signer.can_sign())
s = signer.sign(h)
self.assertEqual(s, t2b(row[2]))
示例3: testVerify1
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def testVerify1(self):
for i in range(len(self._testData)):
row = self._testData[i]
# Build the key
if isStr(row[0]):
key = RSA.importKey(row[0]).publickey()
else:
comps = [ long(rws(row[0][x]),16) for x in ('n','e') ]
key = RSA.construct(comps)
h = row[3].new()
# Data to sign can either be in hex form or not
try:
h.update(t2b(row[1]))
except:
h.update(b(row[1]))
# The real test
verifier = PKCS.new(key)
self.failIf(verifier.can_sign())
result = verifier.verify(h, t2b(row[2]))
self.failUnless(result)
示例4: _build_auth_token_data
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def _build_auth_token_data(
self,
auth_token_ticket,
authenticator,
private_key,
**kwargs
):
auth_token = dict(
authenticator=authenticator,
ticket=auth_token_ticket,
**kwargs
)
auth_token = json.dumps(auth_token, sort_keys=True)
if six.PY3:
auth_token = auth_token.encode('utf-8')
digest = SHA256.new()
digest.update(auth_token)
auth_token = base64.b64encode(auth_token)
rsa_key = RSA.importKey(private_key)
signer = PKCS1_v1_5.new(rsa_key)
auth_token_signature = signer.sign(digest)
auth_token_signature = base64.b64encode(auth_token_signature)
return auth_token, auth_token_signature
示例5: __init__
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def __init__(self, privatekey_text = None, pass_phrase= None):
print('Initializing KeyManager...')
if privatekey_text:
self.import_key_pair(privatekey_text, pass_phrase)
else:
random_gen = Crypto.Random.new().read
self._private_key = RSA.generate(2048, random_gen)
self._public_key = self._private_key.publickey()
self._signer = PKCS1_v1_5.new(self._private_key)
if pass_phrase is not None:
my_pem = self.export_key_pair(pass_phrase)
my_pem_hex = binascii.hexlify(my_pem).decode('ascii')
# とりあえずファイル名は固定
path = 'my_server_key_pair.pem'
f1 = open(path,'a')
f1.write(my_pem_hex)
f1.close()
示例6: test_encrypt
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def test_encrypt(self, sign_message, encrypt_message, encrypt_key, get_key, get_iv):
message = Random.new().read(2000)
encryption = Encryption(self.sender_key, self.recipient_key)
sender_key_bytes = int(self.sender_key.publickey().n.bit_length() / 8)
encrypt_message.return_value = Random.new().read(len(message))
encrypt_key.return_value = Random.new().read(sender_key_bytes)
get_iv.return_value = Random.new().read(16)
encrypted_message = encryption.encrypt(message)
data = base64.b64decode(encrypted_message)
iv = data[0:16]
encrypted_key = data[16:(16 + sender_key_bytes)]
encrypted_message = data[(16 + sender_key_bytes):]
self.assertEqual(encrypt_message.return_value, encrypted_message)
self.assertEqual(encrypt_key.return_value, encrypted_key)
self.assertEqual(get_iv.return_value, iv)
encrypt_key.assert_called_once_with(get_key.return_value)
encrypt_message.assert_called_once_with(
sign_message.return_value, message, get_key.return_value, get_iv.return_value
)
sign_message.assert_called_once_with(message)
示例7: test_decrypt
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def test_decrypt(self, verify, get_message, get_signature, get_decrypted_message, get_key, get_iv, hexlify):
decryption = Decryption(self.recipient_key, self.sender_key)
raw = Random.new().read(2000)
encrypted = base64.b64encode(raw)
message = get_message.return_value
signature = get_signature.return_value
hexlify_signature = hexlify.return_value
decrypted_message = get_decrypted_message.return_value
iv = get_iv.return_value
key = get_key.return_value
verify.return_value = True
returned_message, returned_signature = decryption.decrypt(encrypted)
self.assertEqual(message, returned_message)
self.assertEqual(hexlify_signature, returned_signature)
get_message.assert_called_once_with(decrypted_message)
get_decrypted_message.assert_called_once_with(iv, key, raw)
get_iv.assert_called_once_with(raw)
get_key.assert_called_once_with(raw)
get_signature.assert_called_once_with(decrypted_message)
verify.assert_called_once_with(signature, message)
hexlify.assert_called_once_with(signature)
示例8: sign
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def sign(self, msg, key):
return PKCS1_v1_5.new(key).sign(self.hash_alg.new(msg))
示例9: verify
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def verify(self, msg, key, sig):
return PKCS1_v1_5.new(key).verify(self.hash_alg.new(msg), sig)
示例10: sign_data
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def sign_data(data: bytes, private_key: RSA):
"""
Signs message
:param data: message to sign
:param private_key: private key to use
:return:
"""
signer = PKCS1_v1_5.new(private_key)
digest = SHA256.new()
digest.update(data)
return signer.sign(digest)
示例11: verify_data
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def verify_data(data: bytes, signature: bytes, public_key: RSA) -> bool:
"""
Verifies message
:param data: Message to verify
:param signature: signature of message to verify
:param public_key: public key to use
:return:
"""
signer = PKCS1_v1_5.new(public_key)
digest = SHA256.new()
digest.update(data)
return signer.verify(digest, signature)
示例12: calculate_fingerprint
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def calculate_fingerprint(pkey: paramiko.pkey.PKey, algorithm: str='md5') -> bytes:
"""
Calculates fingerprint for PKey
:param pkey: pkey
:param algorithm: algoright to use
:return: fingerprint
"""
h = hashlib.new(algorithm)
h.update(pkey.asbytes())
return h.digest()
示例13: verify_incoming_request
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 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
示例14: verify
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def verify(self, message, signature):
hash = SHA256.new(message)
signer = PKCS1_v1_5.new(self._value)
return signer.verify(hash, signature)
示例15: client_id
# 需要导入模块: from Crypto.Signature import PKCS1_v1_5 [as 别名]
# 或者: from Crypto.Signature.PKCS1_v1_5 import new [as 别名]
def client_id(self):
return "C.%s" % (SHA256.new(self._value.publickey().exportKey(
"PEM")).hexdigest()[:16])