The comparison between the shasum output of the SHELL program and the crypto results from the JS library are revealing discrepancies

I have successfully obtained the following result using Unix:

shasum -a 256 test.jpg
df94ac3fd72415827f345b5fa22761f57d87e99c25d5345d2e8e9f6c91ef34a3  test.jpg

However, I am facing issues with getting the same result in Javascript using crypto-browserify. Here are the results I am getting:

img.onload = function(e) {
    console.log(crypto.createHash('sha256').update(e.path[0]).digest('hex'));
    console.log(crypto.createHash('sha256').update(e.path).digest('hex'));
    console.log(crypto.createHash('sha256').update(e.path[0].src).digest('hex'));
    console.log(crypto.createHash('sha256').update(e).digest('hex'));
}

The results returned are:

da5698be17b9b46962335799779fbeca8ce5d491c0e26243bafef9ea1837a9d8
6e340b9cffb37a989ca54ee6bb780a2c78901d3fb33738768511a30617afa01d
7ce85f64d69c7a8865413deaff3d65ca0272dfbe74ad9bc07s5e28679243cb69
da5698be17b9b46962335799779fbeca8ce5d491csd26243bafef9ea1837a9d8

I am unable to achieve the desired

df94ac3fd72415827f345b5fa22761f57d87e99c25d5345d2e8e9f6c91ef34a3
as seen in the Unix command line using shasum. Can someone provide guidance on how to obtain the correct sha value?

Answer №1

After much trial and error, this is the solution that worked for me:

    const request = new XMLHttpRequest();
    request.open("GET", "../test.jpg", true);
    request.responseType = "arraybuffer";
    request.onload = function (event) {
        const arrayBuffer = request.response;
        if (arrayBuffer) {
            const byteArray = new Uint8Array(arrayBuffer);
            console.log(crypto.createHash('sha256').update(byteArray).digest('hex'));
        }
    };
    request.send(null);

Appreciate the help, Keith.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Website search bar - easily find what you're looking for

I've tested this code and it works well on the basintap page. However, I'm interested to find out how I can search for something when I'm on a different page instead? <?php $query = $_GET['query']; $min_length = 2; if(strlen($ ...

What is the best way to initiate a background process upon the startup of a Windows WinRT/C# device or application?

What is the ideal way to initiate a background task that utilizes IBackgroundTask in a universal app designed for windows/windows phone? In my development process with cordova, I am crafting an application for ios, android, wp8 and windows. Each platform ...

Finding a specific element within a Node.js application requires pinpointing and isolating

I have a dynamic table that displays user information, with each row containing an update icon and a delete icon. When the delete icon is clicked, I need to delete the corresponding user record. The challenge is identifying which user's delete icon ha ...

What is an alternative method for registering a click on a button without using the onclick event?

I currently have around 30 buttons in my react app, each with a specific function. I am looking for a more efficient way to handle click events without adding an event listener to every button individually. Is there a solution that allows me to achieve thi ...

Is it possible to conduct a feature test to verify the limitations of HTML5 audio on

Is there a way to detect if volume control of HTML5 audio elements is possible on iOS devices? I want to remove UI elements related to the volume if it's not alterable. After referring to: http://developer.apple.com/library/safari/#documentation/Aud ...

Ways to efficiently add numerous string elements to an array in JavaScript without encountering any errors

I have implemented a function to process a large array of strings (user names), checking for single quotes and pushing them into a new array before returning it. However, I recently encountered an issue as the number of users in the list has grown substan ...

Transforming a detailed JSON structure into a more simplified format with Angular 2

As a newcomer to Angular 2, I find myself encountering a hurdle in filtering unnecessary data from a JSON object that is retrieved from a REST API. Below is an example of the JSON data I am working with: { "refDataId":{ "rdk":1, "refDataTy ...

How can I verify if a date is after the current date using Node.js?

I am struggling to set up date validation that ensures the date is after the current date. This is what I have attempted so far: Router.post('/home', [ check('due_date') .isDate() .isAfter(new Date.now()) .wi ...

How can I change an array to a string in JavaScript/jQuery and add a specific

Currently, I am tasked with the challenge of converting an array into objects. Furthermore, I also need to add something before each object is displayed. var arrayList = ["image1.jpg","image2.jpg","image3.jpg"]; The desired outcome should look like this ...

Arrange a collection of words in alphabetical order based on word importance

Given the array below [ { name: '4K UHD', commentator: 'Ali' }, { name: 'English 1 HD', commentator: 'Ahmed' }, { name: 'English 3 HD', commentator: 'Ahmed' }, { name: 'Premium 1 HD&a ...

Tips for merging time-series datasets with varying time intervals into one visual representation using Google Earth Engine

Currently, I am facing a challenge in plotting monthly and yearly soil moisture time-series in the same chart. The ImageCollection contains 480 images (one per month) for the monthly data and another ImageCollection with 40 images (one per year) for the ye ...

"Using Bootstrap's toast feature repositions every element on the

I am working on a website project and encountering an issue with a bootstrap toast. The toast currently appears in the top right corner, but I would like it to display above the carousel without affecting the layout. Instead, when the toast appears, it shi ...

Issue: Unable to change onclick event using JavaScript I am facing a

Presently, I have this code snippet: function tp_visible(action){ if(action==1){ document.getElementById("tp").style.display='block'; document.getElementById("tp_action").onclick='tp_visible(0); return false;&apo ...

Tips on streamlining the deployment process for a basic Vue WebApp without the need for a server (e.g. setting up a straightforward build chain with Vue

I have developed a Vue app and I am exploring ways to automate its deployment process by implementing a CI/CD pipeline that includes linting, testing, building, and uploading the files to web servers (such as stage or production using sftp). After researc ...

Switching a material-ui input control instead of a textfield for materials-ui-datepicker: the ultimate guide

Within my React application (v16.3), I am utilizing the DatePicker component from the material-ui-pickers library to render date-picker controls. This particular component renders a Material-UI TextField. However, I am interested in modifying it to only di ...

What is the best way to showcase a table below a form containing multiple inputs using JavaScript?

Context: The form I have contains various input fields. Upon pressing the [verify] button, only the "first name" is displayed. My goal is to display all input fields, whether empty or filled, in a table format similar to that of the form. Exploration: ...

Numerous JavaScript functions seamlessly integrated into HTML

I'm currently working on incorporating two sliders into my HTML code and utilizing JavaScript functions to update the values indicated by those sliders. I am facing an issue with organizing the code for the output of each slider, possibly due to how t ...

Angular 4 is displaying an error message indicating that the expression has been modified after being initially verified

I've been utilizing Angular Material within my Angular 4 application. There seems to be an issue when attempting to utilize the MatSnackBar in the ngAfterViewInit(). The error I encounter is as follows: ExpressionChangedAfterItHasBeenCheckedError: ...

The Google Chart is failing to show up on the screen

I'm having trouble implementing a feature that involves selecting a region from a dropdown list and requesting rainfall data to display in a Google Chart. Unfortunately, I've encountered some issues with it. Could you please help me identify ...

What is the best way to retrieve the Axios response using Express?

I've recently delved into working with Express and I'm currently struggling with making an Axios request using route parameters, and then updating some local variables based on the response. Here's a snippet of what I've been working on ...