Accept passphrases as bytes

The OpenPGP specifications are ambiguous about the encoding of
passwords.

In practice, we expect most passphrases to be UTF-8-encoded, but if
the incoming passphrase is in fact a bytestring, we ought to be able
to handle it correctly.

Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
This commit is contained in:
Daniel Kahn Gillmor
2022-02-01 23:05:54 -05:00
parent 955d166947
commit 412540ad72

View File

@@ -1023,8 +1023,10 @@ class String2Key(Field):
# Simple S2K - always done
hsalt = b''
##TODO: we could accept a passphrase that is optionally already `bytes`
hpass = passphrase.encode('utf-8')
if isinstance(passphrase, bytes):
hpass = passphrase
else:
hpass = passphrase.encode('utf-8')
# salted, iterated S2K
if self.specifier >= String2KeyType.Salted: