MCPcopy Create free account
hub / github.com/SecurityInnovation/PGPy / encrypt_keyblob

Method encrypt_keyblob

pgpy/packet/fields.py:1210–1237  ·  view source on GitHub ↗
(self, passphrase, enc_alg, hash_alg)

Source from the content-addressed store, hash-verified

1208 return super(PrivKey, self).__len__()
1209
1210 def encrypt_keyblob(self, passphrase, enc_alg, hash_alg):
1211 # PGPy will only ever use iterated and salted S2k mode
1212 self.s2k.usage = 254
1213 self.s2k.encalg = enc_alg
1214 self.s2k.specifier = String2KeyType.Iterated
1215 self.s2k.iv = enc_alg.gen_iv()
1216 self.s2k.halg = hash_alg
1217 self.s2k.salt = bytearray(os.urandom(8))
1218 self.s2k.count = hash_alg.tuned_count
1219
1220 # now that String-to-Key is ready to go, derive sessionkey from passphrase
1221 # and then unreference passphrase
1222 sessionkey = self.s2k.derive_key(passphrase)
1223 del passphrase
1224
1225 pt = bytearray()
1226 for pf in self.__privfields__:
1227 pt += getattr(self, pf).to_mpibytes()
1228
1229 # append a SHA-1 hash of the plaintext so far to the plaintext
1230 pt += hashlib.new('sha1', pt).digest()
1231
1232 # encrypt
1233 self.encbytes = bytearray(_encrypt(bytes(pt), bytes(sessionkey), enc_alg, bytes(self.s2k.iv)))
1234
1235 # delete pt and clear self
1236 del pt
1237 self.clear()
1238
1239 @abc.abstractmethod
1240 def decrypt_keyblob(self, passphrase):

Callers 1

protectMethod · 0.80

Calls 6

clearMethod · 0.95
_encryptFunction · 0.85
gen_ivMethod · 0.80
derive_keyMethod · 0.45
to_mpibytesMethod · 0.45
newMethod · 0.45

Tested by

no test coverage detected