问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9804265/how-does-bitconverter-toint64-handles-a-byte-arry-with-32-bytes-a-256-bit-hash