Skip to content

Instantly share code, notes, and snippets.

@Jim-Holmstroem
Created February 21, 2012 10:51
Show Gist options
  • Save Jim-Holmstroem/1875766 to your computer and use it in GitHub Desktop.
Save Jim-Holmstroem/1875766 to your computer and use it in GitHub Desktop.
Hash numpy.array
from hashlib import sha1
import numpy
arr=numpy.zeros((256,256,4))
sha1(arr)
@sentientmachine
Copy link

sentientmachine commented Sep 28, 2017

Warning, do not use sha1 with password or anything where security relies on it

Sha1 cryptographic hash is dead because thanks to the submission of the first known instance of a fatal exploit known as a "hash collision brute force attack" users can find multiple hash inputs that will generate identical outputs. Do not use sha1 for passwords. No hash function from this library is safe in a security minded area without salts and multiple rounds of hashing.

Documentation: https://docs.python.org/2/library/hashlib.html

But if you can be sure the hashes can't be poisoned, and maliciously crafted collisions don't matter, then you can get the hash object's digest using:

foo = sha1(arr)
print(foo.hexdigest())   #prints something like b443a7f1039283e91d16cb122494b70d3f3ff431

Make sure to take into account the remote possibility that two different inputs will generate identical hash outputs.

@alex-petrenko
Copy link

Also, make sure your array is C-contigious, otherwise hashing will not work.
Simple fix:

    if not x.flags['C_CONTIGUOUS']:
        x = np.ascontiguousarray(x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment