# @file slh_sha256.py
# @version 1.1.0 (2026-02-15T08:23Z)
# @author David Ireland <https://di-mgt.com.au/contact>
# @copyright 2023-26 DI Management Services Pty Ltd
# @license Apache-2.0

"""SHL-DSA-SHA-256 crypto functions."""

# Either use cryptosyspki or pure hashlib functions
#from hashlib_pki import *
from hashlib_pure import *

# All values passed are hex-encoded

# SLH SHA-256 FUNCTIONS...
def BlockPad(PKseed):
    # Pad PK.seed to 64 bytes with zeros (NB in hex)
    return PKseed + "0" * (128 - len(PKseed))

def F(PKseed, adrs, M):
    return SHA256(BlockPad(PKseed) + adrs + M)[:32]

def H(PKseed, adrs, M1, M2):
    return SHA256(BlockPad(PKseed) + adrs + M1 + M2)[:32]

def PRF(PKseed, SKseed, adrs):
    return SHA256(BlockPad(PKseed) + adrs + SKseed)[:32]

def T_len(PKseed, adrs, M):
    return F(PKseed, adrs, M)

def PRF_msg(SKprf, optrand, M):
    return HMAC_SHA256(SKprf, optrand + M)[:32]

def H_msg(R, PKseed, PKroot, M, m):
    return MGF1_SHA256(R + PKseed + SHA256(R + PKseed + PKroot + M), m)


if __name__ == '__main__':
    R = 'bd40e6d66893f38d5c5fad99e4885329'
    PKseed = 'FA495FB834DEFEA7CC96A81309479135'
    PKroot = 'A67029E90668C5A58B96E60111491F3D'
    msg= \
    '00003F'
    h_msg = H_msg(R, PKseed, PKroot, msg, 34)
    print(f"H_msg={h_msg}")
    # cafc639d5e550807f84bae9f7eb8da2077cd579fd484522072fcdcef4b8fdb03b028
    print(f"OK   =cafc639d5e550807f84bae9f7eb8da2077cd579fd484522072fcdcef4b8fdb03b028")