Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a hash algorithm that produces a hash size of 64 bits in C#?

Tags:

c#

hash

ssis

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.

like image 551
Matt Avatar asked Dec 02 '10 22:12

Matt


1 Answers

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

  • first, to choose some cryptographic hash function (the SHA-2 family includes SHA-256, SHA-384, and SHA-512)
  • then, to truncate the output of the chosen hash function to 96 bits (12 bytes) - that is, keep the first 12 bytes of the hash function output and discard the remaining bytes
  • then, to base-64-encode the truncated output to 16 ASCII characters (128 bits)
  • yielding effectively a 96-bit-strong cryptographic hash.
like image 63
yfeldblum Avatar answered Sep 19 '22 02:09

yfeldblum