Score:2

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

mq flag

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?

Maarten Bodewes avatar
in flag
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.
Maarten Bodewes avatar
in flag
It's specified [here](https://docs.python.org/3.9/library/hashlib.html#shake-variable-length-digests) - 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 [so] when stuck.
Maarten Bodewes avatar
in flag
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 [so].
Score:1
in flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.