How does BitConverter.ToInt64 handles a byte arry with 32 bytes (a 256 bit hash from SHA256Managed)

StackOverflow https://stackoverflow.com/questions/9804265

  •  25-05-2021
  •  | 
  •  

Question

I'm using SHA256Managed to generate 256 bit hashs and I would like to store them on sql server. How good is BitConverter.ToInt64 for this? Does it ignore the extra bytes? Should I do some processing to reduce the byte array before converting to int64?

Was it helpful?

Solution

The BitConverter.ToInt64 only uses 8 bytes to produce the value, so any extra bytes in the array are ignored.

If you want to store the complete 256 bit hash, you need to use a different data type.

If you just want to use only the first 64 bits of the hash, that is usually just fine. The entire hash code should be affected by each byte that you calculated the hash for, so only a part of the hash still produces a good hash in itself. There will naturally be a larger risk for a hash collision, but 64 bits is usually enough to make them rare enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top