2
$\begingroup$

I am implementing certificateless cryptography from this research paper in python language. Essentially, I want to have the following hash function mapping. This hash function is mentioned in the setup algorithm in section 2.4 of the paper. Here n is the bit-length of plaintexts. $$\begin{align} H_4: \{0, 1\}^n\to\{0, 1\}^n\\ \end{align}$$ Is there any inbuilt hash function in hashlib python library? Or how can I construct this hash function in python?

$\endgroup$
3
  • 1
    $\begingroup$ This question contains two separate parts: which (kind of) hash algorithms can I use and which ones are available in the hashlib python library. It seems to focus on the later one, and that's off topic on this site, strictly speaking. $\endgroup$
    – Maarten Bodewes
    Commented Dec 5, 2021 at 12:49
  • 2
    $\begingroup$ It's specified here - there since 3.6, which is the oldest still supported lib version. The API is so simple that there is really not much to it. It would maybe require a bit more work if your n is not a multiple of 8, but that's about it. Try it and ask on Stack Overflow when stuck. $\endgroup$
    – Maarten Bodewes
    Commented Dec 5, 2021 at 12:55
  • 1
    $\begingroup$ I didn't answer because it is probably a somewhat better fit on SO, but can honestly not be buggered to convert that formula to an image. ashizz, please do react on answers you're getting here and on Stack Overflow. $\endgroup$
    – Maarten Bodewes
    Commented Dec 6, 2021 at 10:34

1 Answer 1

1
$\begingroup$

Probably the best algorithm to use is SHAKE specified for Keccak, which is the winner of the SHA-3 competition. You can use either SHAKE128 or SHAKE256 depending on the security and performance balance you wish to achieve.

For Python, the algorithm is specified here and should be available since 3.6, i.e. all supported versions. You can just create the hash (or rather XOF) algorithm, call update as many times you want on the binary input and then retrieve the result using digest(digestSize) call, where digestSize is in bytes.


If you require an $n$ that is not a multiple of 8 (i.e. the input / output is not byte aligned) you may need to perform some tricks, e.g. prefixing zeros to the input and removing the leftmost bits from the output afterwards. Of course, that assumes that $n$ doesn't vary, or you may get input message collisions. Most practical protocols have been designed with byte-aligned input/output parameters though.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.