1

Need some help. I am not versed in hashing and just started programming with PHP and currently need to hash some strings but required it to be unique. Found that OpenSSL can hash with SHAKE128 but without the option to select the lengths of the output. Notice that every openssl_digest function with shake128 hash output is 256 character by default, can I just take the first 20 character to use since I notice only the last few characters are appended with each length change? shake128 hash "hello" example:

5 byte - 8eb4b6a932

6 byte - 8eb4b6a932f2

7 byte - 8eb4b6a932f280

8 byte - 8eb4b6a932f28033

16 byte - 8eb4b6a932f280335ee1a279f8c208a3

Appreciate your insight. Thank you.

  • NVM. I think i get it. Just read the NIST document https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf and found the following which I think answer my question. "...Consequently,when two differentoutput lengths are chosen for a common message,thetwo outputs are closely related: thelonger output is an extensionof the shorter output. For example, given anypositive integers d and e, andany message M, Truncd(SHAKE128(M,d+e))isidentical to SHAKE128(M,d). Thesame property holds for SHAKE256..." – OLDPROGRAMMER Apr 01 '20 at 04:30

1 Answers1

1

Yes, you can do that as long as you promise to not to use the hash for any other purpose that would be compromised if the shorter hash would leak. However, if you need fewer than 128 bits then you might as well use SHA-3 and truncate that; it depends on the purpose if SHAKE128 is better than, say, SHA-3-256; they both depend on the same underlying sponge function after all.

Note that neither SHAKE128 or SHA-3-256 output any characters. They generate binary bits or - for most implementations - bytes. The hexadecimal characters that you're showing are just a representation of those bytes.

Maarten Bodewes
  • 92,551
  • 13
  • 161
  • 313