diff --git a/_modules/rabbitmq.py b/_modules/rabbitmq.py new file mode 100644 index 0000000..9a57042 --- /dev/null +++ b/_modules/rabbitmq.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +# ------------------------------------------------------------- +# Salt — RabbitMQ execution module +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Project: Nasqueron +# Description: Allow to use RabbitMQ management plugin HTTP API +# License: BSD-2-Clause +# ------------------------------------------------------------- + + +import base64 +import hashlib +import secrets + + +# ------------------------------------------------------------- +# Credentials +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + +def compute_password_hash(password): + salt = secrets.randbits(32) + return _compute_password_hash_with_salt(salt, password) + + +def _compute_password_hash_with_salt(salt, password): + """Reference: https://rabbitmq.com/passwords.html#computing-password-hash""" + salt = salt.to_bytes(4, "big") # salt is a 32 bits (4 bytes) value + + m = hashlib.sha256() + m.update(salt) + m.update(password.encode("utf-8")) + result = salt + m.digest() + + return base64.b64encode(result).decode("utf-8") diff --git a/_tests/modules/test_rabbitmq.py b/_tests/modules/test_rabbitmq.py new file mode 100755 index 0000000..b57c849 --- /dev/null +++ b/_tests/modules/test_rabbitmq.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +from importlib.machinery import SourceFileLoader +import unittest + + +salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module() +rabbitmq = SourceFileLoader("rabbitmq", "../_modules/rabbitmq.py").load_module() + + +class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase): + def test_compute_password_hash_with_salt(self): + self.assertEqual( + "kI3GCqW5JLMJa4iX1lo7X4D6XbYqlLgxIs30+P6tENUV2POR", + rabbitmq._compute_password_hash_with_salt(0x908DC60A, "test12"), + )