I need to produce a Hash value based off of a variable length string that I can store within a field no longer than 16 (due to vendor requirements).
I am concatenating together several strings that are being passed through a C# script transformation in order to calculate the Hash. I am constrained by the vendor's file specification in that the output of the hash cannot be any longer than 16.
Does anyone have any suggestions? As an example the string conversion of the MD5 algorith has a length of 32.
The cryptographic has functions are designed such that you may truncate the output to some size and the truncated hash function remains a secure cryptographic hash function. For example, if you take the first 128 bits (16 bytes) of the output of SHA-512 applied to some input, then the first 128 bits are a cryptographic hash as strong as any other 128-bits cryptographic hash.
The solution is to choose some cryptographic hash function - SHA-256, SHA-384, and SHA-512 are good choices - and truncate the output to 128 bits (16 bytes).
--EDIT--
Based on the comment that the hash value must, when encoded to ASCII, fit within 16 ASCI characters, the solution is
If you have 16 bytes storing a 128bit number is not an issue. Store the 128bit value as a 16byte value instead of a 32 character string that stored the 16 byte value as HEX.
As a note I have used GUID/UUID fields in databases to store MD5 hashes. While no longer cryptographically secure, 128bit MD5 hashes are fine for Checksums (and is much better than 64 bits.)
Note that I don't show the file content because there is a good chance that it will contain "unprintable" characters.
I've noticed this question is relatively old, but I'm sure someone will find this answer to it valuable.
My suggestion would be to use Blake2b which has the ability to use 8 bit through 512 bits. If no key size is used, the default value is used "512" in this case. Blake2s default value 256 bits.
Hope this helps!
You can easily use an MD5 hash for this, but you will have to alter the way it is stored. An MD5 is 128-bits, which is typically displayed as 32 4-bit (hexadecimal) values. A standard char is 8 bits, however, so 16 characters is exactly enough to store the value of an MD5 hash.
To convert it, try the following:
Any comments about this code? Seems works well...