0

In the "Small Collision Probabilities" section of https://preshing.com/20110504/hash-collision-probabilities/ you can see the trade-offs depending on how big the hash value is. Rather a 32-bit or 160-bit hash depends on your use-case and limitations.

How can I construct a checksum/hash of n-bits given the integer n?

The idea is that the caller of the hash function can determine the trade off that fit their need (only want to store integers and dont care about collisions, use 32 bit, etc).

The probability of collisions should be on-par with typical hash functions of the same bit-length (Given n = 160, the probability of collisions is close to sha-1. Given n = 32 the probability of collisions is close to crc-32)

My attempt:

if n < 160, sha-1(input) truncate first bits to bit-size n.
if n = 160, sha-1(input) 
if n > 160, sha-1(input) & sha-1(input) [Repeat concatenation (n % 160) times and then truncate bits to n)

I realize that for n > 160 my collision probability isn't getting any lower because its performing the same hash. Also sha-1 isn't needed because my purpose is not cryptographic in nature and more of a checksum

ParoX
  • 5,685
  • 23
  • 81
  • 152

1 Answers1

0

For hashes of n*160 bits you could pick n prefixes (different but of the same length, to avoid collisions) and hash the input data appended to a prefix to give you n different 160-bit hashes. If you can create a collision while doing this you should be able to track this back to a collision in the underlying hash function, because different input data values never give rise to the same inputs for the underlying hash function.

The paper "Computational complexity of universal hashing" e.g. at https://core.ac.uk/download/pdf/82448366.pdf shows that binary convolution of bit-vectors is a (non-secure) universal hash function. To hash a bit-vector of n bits and produce a bit-vector of m bits as output you convolve with a bit-vector of n+m-1 bits and xor with a bit-vector of m-bits. (Claim 2.2 P 5 marked 125)

mcdowella
  • 19,301
  • 2
  • 19
  • 25