Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing an Image in Javascript

I have currently been trying to hash an image from my browser using javascript. However, I've been hashing a string of the dataURL or the pixel data that I've been retrieving from the canvas element in HTML. This is obviously not the same as hashing the raw data of the image which is what I would like to do.

For example the data that would be used for the same image in the php hash file function.

Does anybody know how I can access this raw image data using javascript to get a hash value that would be equivalent to the result hash I get from PHP hash_file($file)?

Thanks!

like image 597
roman canada Avatar asked Mar 04 '13 18:03

roman canada


People also ask

Can an image be hashed?

Image hashing is the process of using an algorithm to assign a unique hash value to an image. Duplicate copies of the image all have the exact same hash value. For this reason, it is sometimes referred to as a 'digital fingerprint'.

How do you hash in JavaScript?

You can implement a Hash Table in JavaScript in three steps: Create a HashTable class with table and size initial properties. Add a hash() function to transform keys into indices. Add the set() and get() methods for adding and retrieving key/value pairs from the table.

What is pHash in images?

Image similarity identification Cloudinary uses perceptual hash (pHash), which acts as an image fingerprint. This mathematical algorithm analyzes an image's content and represents it using a 64-bit number fingerprint. Two images' pHash values are “close” to one another if the images' content features are similar.

How do I view an image hash?

To check the hash of the entire image including the partition table and all partitions, select "Use entire image file". Otherwise, select the desired partition. Once the hash has been calculated, copy/paste the expected hash value into the comparison hash field. If the hash matches, a green checkmark appears.


1 Answers

You can get the raw data of an image with an XHR request to that image file location.

var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/file.png', true);
xhr.responseType = 'arraybuffer'; // this will accept the response as an ArrayBuffer
xhr.onload = function(buffer) {
    var words = new Uint32Array(buffer),
        hex = '';
    for (var i = 0; i < words.length; i++) {
      hex += words.get(i).toString(16);  // this will convert it to a 4byte hex string
    }
    console.log(hex);
};
xhr.send();

After that, you can use whatever hashing algorithm you'd like. Here's a library of them: https://code.google.com/p/crypto-js/

like image 74
MattDiamant Avatar answered Sep 21 '22 16:09

MattDiamant