Skip to content

Commit 64f8fe5

Browse files
committed
Cover make_hash.
1 parent 4055f1d commit 64f8fe5

File tree

2 files changed

+50
-2
lines changed

2 files changed

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

0 commit comments

Comments
 (0)