1

I'm looking for a way to hash a string to a dynamic number of characters. I don't want to trim an existing hash (such as SHA) but generate a hash that you can specify the number of output characters for. It should also work if the input is less than the number of characters. It doesn't need to be cryptographic, it only needs to guarantee the same hash for the same input. I've been going through the hash functions on wiki but they all seem to have a fixed length of a dynamic length depending on the input.

The Cookies Dog
  • 1,915
  • 2
  • 22
  • 38
  • 1
    For what purpose do you need this kind of thing? – deceze Mar 12 '19 at 09:07
  • I want to create an identifier that depends on an unspecified number of inputs, but the output length must always be the same. I can't combine the inputs as-is as I need a way to check whether there are still parts of the original hashes in the identifier. Say the identifier is ABCDEF, where A, B, C, D, E and F are the hashes of dynamic length. I need to be able to recalculate A, B, C, etc). Hope that helps! – The Cookies Dog Mar 12 '19 at 09:15
  • So, `hashA + hashB + hashC + ...` must always equal a given length, so you want to make the length of each hash dynamic so as to always reach a total length? … I'd first go further back and questions what exactly you need *that* for. Make sure you don't have an [XY Problem](http://xyproblem.info). – deceze Mar 12 '19 at 09:20
  • This requirement is OK for what I need it for. – The Cookies Dog Mar 12 '19 at 09:28

1 Answers1

0

What you are looking for might be Extendable Output Functions (XOF's)!

Those hash functions don't have predefined output length and might use sponge functions for construction.

The SHA-3 family consists of four cryptographic hash functions, [...], and two extendable-output functions (XOFs), called SHAKE128 and SHAKE256.

You can try out both under https://emn178.github.io/online-tools/. For the output bits choose your desired number or characters.

For a Java implementation see the Bouncy Castle Crypto Library which supports both algorithms https://www.bouncycastle.org/specifications.html

But be aware of collisions if the hash length is to small.

Hans Sperker
  • 1,347
  • 2
  • 15
  • 37