# How to have a hash function that maps any binary string of size n to binary string of size n?

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?

• 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. Commented Dec 5, 2021 at 12:49
• 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. Commented Dec 5, 2021 at 12:55
• 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. Commented Dec 6, 2021 at 10:34

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.