def __save(self, private_key, file_name):
if not isinstance(private_key, RSAPrivateKey):
raise TypeError("private_key must be an instance of RSAPrivateKey, "
"actual: {}".format(type(private_key).__name__))
pem = private_key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.PKCS8,
serialization.BestAvailableEncryption(self.__key_store_passphrase))
file_path = os.path.join(self.key_store_path, file_name)
if not os.path.isdir(self.key_store_path):
os.makedirs(self.key_store_path)
if os.path.isfile(file_path):
msg = "Save failed: A key with name [{}] already exists".format(
file_name)
raise IOError(msg)
with open(file_path, 'w') as f:
f.write(pem.decode(encoding='utf8'))
评论列表
文章目录