[3.10] bpo-44309: Add support for yescrypt in crypt. (GH-26526) by besser82 · Pull Request #26527 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Doc/library/crypt.rst
13 changes: 12 additions & 1 deletion Lib/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def mksalt(method=None, *, rounds=None):
else: # modular
s = f'${method.ident}$'

if method.ident and method.ident[0] == '2': # Blowfish variants
if method.ident and method.ident == 'y': # yescrypt
if rounds is not None:
if not 1 <= rounds <= 11:
raise ValueError('rounds out of the range 1 to 11')
else:
rounds = 5
s += 'j' + ('75', '85', '7T', '8T', '9T', 'AT', 'BT', 'CT', 'DT', 'ET', 'FT')[rounds - 1] + '$'
elif method.ident and method.ident[0] == '2': # Blowfish variants
if rounds is None:
log_rounds = 12
else:
Expand Down Expand Up @@ -102,6 +109,10 @@ def _add_method(name, *args, rounds=None):
return True
return False

# Supported by libxcrypt. Strongest hashing method currently supported.
_add_method('YESCRYPT', 'y', 24, 75, rounds=1)

# SHA-2 based methods.
_add_method('SHA512', '6', 16, 106)
_add_method('SHA256', '5', 16, 63)

Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,34 @@ def test_blowfish_rounds(self):
cr2 = crypt.crypt('mypassword', cr)
self.assertEqual(cr2, cr)

@unittest.skipUnless(
crypt and crypt.METHOD_YESCRYPT in crypt.methods, 'requires support of yescrypt'
)
def test_yescrypt_rounds(self):
for rounds in range(1, 11):
if rounds < 3:
enc_rounds = 'j' + chr(54 + rounds) + '5'
elif rounds < 6:
enc_rounds = 'j' + chr(52 + rounds) + 'T'
elif rounds < 12:
enc_rounds = 'j' + chr(59 + rounds) + 'T'
salt = crypt.mksalt(crypt.METHOD_YESCRYPT, rounds=rounds)
self.assertIn('$%s$' % enc_rounds, salt)
self.assertEqual(len(salt) - crypt.METHOD_YESCRYPT.salt_chars, 7)
cr = crypt.crypt('mypassword', salt)
self.assertTrue(cr)
cr2 = crypt.crypt('mypassword', cr)
self.assertEqual(cr2, cr)

def test_invalid_rounds(self):
if crypt.METHOD_YESCRYPT in crypt.methods:
with self.assertRaises(TypeError):
crypt.mksalt(crypt.METHOD_YESCRYPT, rounds='4096')
with self.assertRaises(TypeError):
crypt.mksalt(crypt.METHOD_YESCRYPT, rounds=4096.0)
for rounds in (0, -1, 1000, 1<<999):
with self.assertRaises(ValueError):
crypt.mksalt(crypt.METHOD_YESCRYPT, rounds=rounds)
for method in (crypt.METHOD_SHA256, crypt.METHOD_SHA512,
crypt.METHOD_BLOWFISH):
with self.assertRaises(TypeError):
Expand Down