Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add salt parameter to hash generation for sha256 plugins #631

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix python 2.7 compatibility
Aohzan committed Jun 10, 2024

Verified

This commit was signed with the committer’s verified signature.
Aaron1011 Aaron Hill
commit c9c9f7317a29606c4557085eb9f1cdf662240ca4
18 changes: 9 additions & 9 deletions plugins/module_utils/implementations/mysql/hash.py
Original file line number Diff line number Diff line change
@@ -10,33 +10,33 @@
import hashlib


def _to64(v: int, n: int) -> str:
def _to64(v, n):
"""Convert a 32-bit integer to a base-64 string"""
i64 = (
[".", "/"]
+ [chr(x) for x in range(48, 58)]
+ [chr(x) for x in range(65, 91)]
+ [chr(x) for x in range(97, 123)]
)
result: str = ""
result = ""
while n > 0:
n -= 1
result += i64[v & 0x3F]
v >>= 6
return result


def _hashlib_sha256(data: bytes) -> bytes:
def _hashlib_sha256(data):
"""Return SHA-256 digest from hashlib ."""
return hashlib.sha256(data).digest()


def _sha256_digest(key: str, salt: str, loops: int) -> str:
def _sha256_digest(key, salt, loops):
"""Return a SHA-256 digest of the concatenation of the key, the salt, and the key, repeated as necessary."""
# https://www.akkadia.org/drepper/SHA-crypt.txt
num_bytes: bytes = 32
bytes_key: bytes = key.encode()
bytes_salt: bytes = salt.encode()
num_bytes = 32
bytes_key = key.encode()
bytes_salt = salt.encode()
digest_b = _hashlib_sha256(bytes_key + bytes_salt + bytes_key)

tmp = bytes_key + bytes_salt
@@ -104,12 +104,12 @@ def _sha256_digest(key: str, salt: str, loops: int) -> str:
return tmp


def mysql_sha256_password_hash_hex(password: str, salt: str) -> str:
def mysql_sha256_password_hash_hex(password, salt):
"""Return a MySQL compatible caching_sha2_password hash in hex format."""
assert len(salt) == 20, "Salt must be 20 characters long."

count = 5
iteration = 1000 * count

digest = _sha256_digest(password, salt, iteration)
return f"$A${count:>03}${salt}{digest}".encode().hex().upper()
return "$A${0:>03}{1}{2}".format(count, salt, digest).encode().hex().upper()