Skip to content

Commit d9c4a7d

Browse files
committed
Merge remote-tracking branch 'origin/fix/make-hash-py3' into experimental
2 parents 02d7105 + ceddffb commit d9c4a7d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

mig/shared/pwcrypto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ def best_crypt_salt(configuration):
114114
return salt_data
115115

116116

117-
def make_hash(password):
117+
def make_hash(password, _urandom=urandom):
118118
"""Generate a random salt and return a new hash for the password."""
119119
# NOTE: urandom already returns bytes as required for base64 encode
120-
salt = b64encode(urandom(SALT_LENGTH))
120+
salt = b64encode(_urandom(SALT_LENGTH))
121121
# NOTE: hashlib functions require bytes, and post string format needs
122122
# native strings to avoid actually inserting string type markers.
123123
derived = b64encode(hashlib.pbkdf2_hmac(HASH_FUNCTION,

tests/test_mig_shared_pwcrypto.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# --- BEGIN_HEADER ---
4+
#
5+
# test_mig_shared_pwcrypto - unit test of the corresponding mig shared module
6+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
7+
#
8+
# This file is part of MiG.
9+
#
10+
# MiG is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# MiG is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23+
# USA.
24+
#
25+
# --- END_HEADER ---
26+
#
27+
28+
"""Unit test pwcrypto functions"""
29+
30+
import os
31+
import sys
32+
33+
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
34+
35+
from support import MigTestCase, temppath, testmain
36+
37+
from mig.shared.pwcrypto import *
38+
39+
class MigSharedPwcrypto_make_hash(MigTestCase):
40+
def test_pickle_string(self):
41+
expected = "PBKDF2$sha256$10000$MDAwMDAwMDAwMDAw$epib2rEg/HYTQZFnCp7hmIGZ6rzHnViy"
42+
43+
actual = make_hash('foobar', _urandom=lambda vlen: b'0' * vlen)
44+
45+
self.assertEqual(actual, expected, "mismatch pickling string")
46+
47+
48+
if __name__ == '__main__':
49+
testmain()

0 commit comments

Comments
 (0)