Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def Sign(self, data):
return rsa.sign(data, self.priv_key, 'SHA-1-PREHASHED')
if __name__ == '__main__':
private_key_file = "rsa_4096_private.pem"
public_key_file = "rsa_4096_public.pem"
pub_key, priv_key = load_keys()
message = "hello"
crypto = rsa.encrypt(message, pub_key)
message_decrypted = rsa.decrypt(crypto, priv_key)
print(message == message_decrypted)
hash_method = 'SHA-512'
signature = rsa.sign(message, priv_key, hash_method)
print(signature.encode('hex'))
try:
rsa.verify(message, signature, pub_key)
except rsa.pkcs1.VerificationError:
print('Verification failed')
else:
print('Verification succeed')
def _sign_text(self, data, key_file=path_render(Config.ModulesConfig.yggdrasil.SignnatureKeys.Private)):
key_file = open(key_file, 'r').read()
key = rsa.PrivateKey.load_pkcs1(key_file.encode('utf-8'))
return bytes(Base64(rsa.sign(data.encode("utf-8"), key, 'SHA-1'))).decode("utf-8")
if private_key_file and private_key_string:
raise ValueError("Only specify the private_key_file or the private_key_string not both")
if not private_key_file and not private_key_string:
raise ValueError("You must specify one of private_key_file or private_key_string")
# If private_key_file is a file name, open it and read it
if private_key_string is None:
if isinstance(private_key_file, six.string_types):
with open(private_key_file, 'r') as file_handle:
private_key_string = file_handle.read()
# Otherwise, treat it like a file
else:
private_key_string = private_key_file.read()
# Sign it!
private_key = rsa.PrivateKey.load_pkcs1(private_key_string)
signature = rsa.sign(str(message), private_key, 'SHA-1')
return signature
def asymmetric_sign(plaintext, sign_key):
""" A function that takes in a plaintext and a private key and signs the
plaintext with the key to generate an RSA signature and return it.
Args:
plaintext (bytearray): The plaintext that is to be signed.
sign_key (rsa.PrivateKey): The private key that is to be used to
sign the plaintext.
Returns:
bytearray: A bytearray representing the RSA signature of the
plaintext generated with the given private key.
"""
return sign(plaintext, sign_key, 'SHA-512')
def sign(self,data):
return rsa.sign(data,self.key)
def make_sign(message):
"""
签名
:param message:
:return:
"""
private_key = rsa.PrivateKey._load_pkcs1_pem(alipay_config.RSA_PRIVATE)
sign = rsa.sign(message, private_key, SIGN_TYPE)
b64sing = base64.b64encode(sign)
return b64sing
def encrypt(cls, message):
public_key_data, private_key_data = cls.get_keys_raw()
private_rsa_key = rsa.PrivateKey.load_pkcs1(private_key_data)
signature = rsa.sign(message, private_rsa_key, 'SHA-512')
iv, aes_key, encrypted_message = aes_cbc_encrypt(signature + message)
public_key = RSA.importKey(public_key_data.strip())
public_cipher = PKCS1_OAEP.new(public_key)
encrypted_key = public_cipher.encrypt(aes_key)
return base64.b64encode(iv + encrypted_key + encrypted_message)
def _get_token():
"""Get the authentication token for windows vm xmlrpc client."""
challenge = proxy.get_challenge()
priv_key = rsa.PrivateKey.load_pkcs1(
open(settings.WINDOWS_VM_SECRET).read())
signature = rsa.sign(challenge, priv_key, 'SHA-512')
sig_b64 = base64.b64encode(signature)
return sig_b64
msg2 = "Hello Toni, I am Jarvis!".encode('utf-8')
public, private = rsa.new_keys(key_size=2048)
print('private:', private.exportKey('PEM'))
print('public:', public.exportKey('PEM'))
print()
# Encrypt-Decrypt: private -> private
encrypted = b64encode(rsa.encrypt(msg1, private))
print("Encrypted:", encrypted)
decrypted = rsa.decrypt(b64decode(encrypted), private)
print("Decrypted:", decrypted)
print()
signature = b64encode(rsa.sign(msg1, private))
print("Signature:", signature)
print()
verify = rsa.verify(msg1, b64decode(signature), private)
print("Verify:", verify) # True
verify = rsa.verify(msg2, b64decode(signature), private)
print("Verify:", verify) # False
# Encrypt-Decrypt: private -> private
print('\n')
# Encrypt-Decrypt: public -> private
encrypted = b64encode(rsa.encrypt(msg1, public))
print("Encrypted:", encrypted)