Are the first 32 bits of a 160-bit SHA1 hash an acceptable CRC32 hash replacement?

I am working on a .NET 3.5 project and I need a 32-bit hash value. There are no methods in the .NET Cryptography classes that return a 32-bit hash (MD5 - 128 bits, SHA1 - 160 bits, etc.). I implemented the CRC32 class, but found that the existing SHA1 and MD5 hash functions are much faster.

Will there be any problem (i.e. increased chance of collisions) with me using the SHA1 hash function and just catch the first 32 bits to store as my hash value?

+3
source share
5 answers

CRC32 ( ), , 32 .

- - ( "" , ). NIST (, NSA) , SHA-224 SHA-256 (. SHA wikipedia).

EDIT: CRC32 (, , ) , - , , .

" " (. )? 32- (.. ), 2 ^ 16 , . ( , .)

+6

, - , , . "" 32- -, , . , - , :)

+2

string.GetHashCode(). 32- - . , , .

+1

32- , . 32 , .

, ?

0

CRC32, , . .

In terms of hash primitive truncation, the only widely used application is the SSL / TLS (PRF) pseudo-random function , which is used to generate keys. It uses HMAC, seeds, and tags to generate as many bytes as you need, hashing several times and then trimming to the required number of bytes.

As for your specific question, you can read the hash output in Int32, and then combine them together if you are paranoid:

static void Main()
{
    int xorCrc = GetHashedCrc(new SHA1Cng(), new byte[] {0xDE, 0xAD, 0xBE, 0xEF});
}

private static int GetHashedCrc(HashAlgorithm algorithm, byte[] bytesToHash)
{
    byte[] hash = algorithm.ComputeHash(bytesToHash);
    int totalInt32s = hash.Length/sizeof(int);
    int result = 0;
    for(int i = 0; i < totalInt32s; i++)
    {
        int currentInt = BitConverter.ToInt32(hash, sizeof(int)*i);
        result = result ^ currentInt;
    }

    return result;
}
0
source

Source: https://habr.com/ru/post/1709196/


All Articles