in oss2/crypto.py [0:0]
def __init__(self, dir=None, key='', passphrase=None, cipher=utils.AESCTRCipher(),
pub_key_suffix=DEFAULT_PUB_KEY_SUFFIX, private_key_suffix=DEFAULT_PRIV_KEY_SUFFIX):
super(LocalRsaProvider, self).__init__(cipher=cipher)
self.wrap_alg = headers.RSA_NONE_OAEPWithSHA1AndMGF1Padding
keys_dir = dir or os.path.join(os.path.expanduser('~'), _LOCAL_RSA_TMP_DIR)
priv_key_path = os.path.join(keys_dir, key + private_key_suffix)
pub_key_path = os.path.join(keys_dir, key + pub_key_suffix)
try:
if os.path.exists(priv_key_path) and os.path.exists(pub_key_path):
with open(priv_key_path, 'rb') as f:
self.__decrypt_obj = PKCS1_OAEP.new(RSA.importKey(f.read(), passphrase=passphrase))
with open(pub_key_path, 'rb') as f:
self.__encrypt_obj = PKCS1_OAEP.new(RSA.importKey(f.read(), passphrase=passphrase))
else:
logger.warn('The file path of private key or public key is not exist, will generate key pair')
private_key = RSA.generate(2048)
public_key = private_key.publickey()
self.__encrypt_obj = PKCS1_OAEP.new(public_key)
self.__decrypt_obj = PKCS1_OAEP.new(private_key)
utils.makedir_p(keys_dir)
with open(priv_key_path, 'wb') as f:
f.write(private_key.exportKey(passphrase=passphrase))
with open(pub_key_path, 'wb') as f:
f.write(public_key.exportKey(passphrase=passphrase))
except (ValueError, TypeError, IndexError) as e:
raise ClientError(str(e))