%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from hashlib import sha1
import os
N=10**6 # number of random hashes generated
M=4 # number of bits of the identifier
lsb = [0.]*(2**M)
msb = [0.]*(2**M)
for h in [sha1(bytes(os.urandom(4))).hexdigest() for x in range(N)]:
lsb[int(h[-M//4:],base=16)] += 1
msb[int(h[:M//4],base=16)] += 1
lsb = [h/N for h in lsb]
msb = [h/N for h in msb]
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.plot(range(2**M), lsb, '-')
plt.axis([0, 2**M-1, 0, max(lsb)*1.5])
plt.title("SHA1, {0} LSB, {1} random hashes".format(M, N))
plt.subplot(122)
plt.plot(range(2**M), msb, '-')
plt.axis([0, 2**M-1, 0, max(msb)*1.5])
plt.title("SHA1, {0} MSB, {1} random hashes".format(M, N))
Text(0.5,1,'SHA1, 4 MSB, 1000000 random hashes')