-3

So for a project that I am working on, I am trying to get a hashing algorithm but I don't know anything about hashing algorithms. The final outcome I would like to archive is inputing a 6 byte value and get 3 unique bytes as my output.

My other alternative is one algorithm that inputs a 2 byte value and outputs 1 unique byte.

Is this possible?

** Edit: I would need this in C language if possible or pseudo code.

  • Only if those 2 (or 6) input bytes are limited to 256 (or 2^24, for 3 output bytes) different values, can you make a guarantee like that? What can you say about the input? – harold Aug 19 '21 at 20:22
  • So the input would be 6 bytes (each byte is 8 bits) (each byte is 0xFF in hex or 255 max in decimal). Did i understand your question right? @harold – Zachary Denny Aug 19 '21 at 20:26
  • 2
    Well what I meant is, I hope there is more detail to it. If the input is *any 6 bytes* (so 2^48 different possible inputs), then 3 byte outputs couldn't possibly be unique, because there are just fewer possible outputs than there are inputs (by a large margin). Compare: if there are 20 people in a room, they cannot all have a unique number between 1 and 10, because there are more people than numbers. If there is some additional condition on the input that reduces the number of possibilities, then maybe it's possible. – harold Aug 19 '21 at 20:32
  • 1
    No, it's not possible, unless you have some unexpected definition of *unique*. – President James K. Polk Aug 20 '21 at 02:00
  • The question does not meet the standards of stack overflow, please look at https://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites – cards Aug 21 '21 at 12:16

1 Answers1

0

Most hash functions can take arbitrary numbers of bytes since they are by nature compression functions. As for the output, you can just take the first 3 bytes of the output. Any cryptographically safe hash function will output bytes that are suitable for this.

For example, in Python it would be:

from hashlib import sha256

s = sha256(<your bytes>)
output = s.digest()[:3]
hangonstack
  • 162
  • 5
  • Would you be able to achieve this in C language or pseudo code please? sorry i edited my original post afterwords. Also would the first 3 bytes always be unique between all other possibilities? – Zachary Denny Aug 19 '21 at 20:30
  • I haven't written C since my college days over a decade ago so can't help you there. The basic idea is that any commonly used cryptographic hash function like SHA256 can accept any number of bytes and output psuedo-random bytes. And all bits/bytes in the output is as random as any other bytes/bits. So just take any 3. – hangonstack Aug 19 '21 at 20:36