From 8f31ee05cd247dfdb8d7fd59ffe7ac965aecf952 Mon Sep 17 00:00:00 2001 From: Alex Burke Date: Wed, 29 Jan 2025 12:23:18 +0100 Subject: [PATCH 1/2] Cover make_hash. --- mig/shared/pwcrypto.py | 4 +-- tests/test_mig_shared_pwcrypto.py | 48 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/test_mig_shared_pwcrypto.py diff --git a/mig/shared/pwcrypto.py b/mig/shared/pwcrypto.py index abb1a6a1a..f30dad457 100644 --- a/mig/shared/pwcrypto.py +++ b/mig/shared/pwcrypto.py @@ -114,10 +114,10 @@ def best_crypt_salt(configuration): return salt_data -def make_hash(password): +def make_hash(password, _urandom=urandom): """Generate a random salt and return a new hash for the password.""" # NOTE: urandom already returns bytes as required for base64 encode - salt = b64encode(urandom(SALT_LENGTH)) + salt = b64encode(_urandom(SALT_LENGTH)) # NOTE: hashlib functions require bytes, and post string format needs # native strings to avoid actually inserting string type markers. derived = b64encode(hashlib.pbkdf2_hmac(HASH_FUNCTION, diff --git a/tests/test_mig_shared_pwcrypto.py b/tests/test_mig_shared_pwcrypto.py new file mode 100644 index 000000000..db85740f2 --- /dev/null +++ b/tests/test_mig_shared_pwcrypto.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# +# --- BEGIN_HEADER --- +# +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH +# +# This file is part of MiG. +# +# MiG is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# MiG is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. +# +# --- END_HEADER --- +# + +"""Unit test pwcrypto functions""" + +import os +import sys + +sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), "."))) + +from support import MigTestCase, temppath, testmain + +from mig.shared.pwcrypto import * + +class MigSharedPwcrypto_make_hash(MigTestCase): + def test_pickle_string(self): + expected = "PBKDF2$sha256$10000$MDAwMDAwMDAwMDAw$epib2rEg/HYTQZFnCp7hmIGZ6rzHnViy" + + actual = make_hash('foobar', _urandom=lambda vlen: b'0' * vlen) + + self.assertEqual(actual, expected, "mismatch pickling string") + + +if __name__ == '__main__': + testmain() From ceddffbc8984f193d10ed320e4ac90dc61a86dd3 Mon Sep 17 00:00:00 2001 From: Jonas Bardino Date: Sat, 8 Feb 2025 19:15:08 +0100 Subject: [PATCH 2/2] Update test_mig_shared_pwcrypto.py Comment-only change: add missing brief module description one-liner and bump copyright year while at it. Signed-off-by: Jonas Bardino --- tests/test_mig_shared_pwcrypto.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_mig_shared_pwcrypto.py b/tests/test_mig_shared_pwcrypto.py index db85740f2..d81dbbe4a 100644 --- a/tests/test_mig_shared_pwcrypto.py +++ b/tests/test_mig_shared_pwcrypto.py @@ -2,7 +2,8 @@ # # --- BEGIN_HEADER --- # -# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH +# test_mig_shared_pwcrypto - unit test of the corresponding mig shared module +# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH # # This file is part of MiG. #