How to Generate an Image Hash Value in Node.JS

Cloudmersive
2 min readApr 21, 2021

--

Due to the increase of digital forgeries and online threats, multimedia authentication and security have become important and demanding facets of content protection. These threats have driven further research into algorithms and safety features in an effort to improve the confidentiality and integrity of images, videos, and other multimedia. Perceptual image hashing is the use of an algorithm that produces a unique snippet of an image; from this, we can determine the hash value. To avoid the standard and time-consuming method of perceptual hashing, the following API can be used in Node.JS to generate a hash value for an image.

To begin, we can run this command to install the client:

npm install cloudmersive-image-api-client –-save

Or, add this snippet to your package.json:

"dependencies": {
"cloudmersive-image-api-client": "^1.3.4"
}

Now we are ready to input our image path and API key into the following code to call the function:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveImageApiClient.RecognizeApi();var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.var opts = {
'recognitionMode': "recognitionMode_example" // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.recognizeSimilarityHash(imageFile, opts, callback);

That’s it! Your hash value between 0 and 1 will be ready for you at the end of this quick and easy process.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.