The MD5 encryption varies from one another

When using SQL Server:

0x5C8C8AAFE7AE37EA4EBDF8BFA01F82B8

SELECT HASHBYTES('MD5', convert(varchar,getdate(),112)+'mytest@+')

With JavaScript: 5c8c8aafe7ae37ea4ebdf8bfa01f82b8

// Function to get MD5 Hash bytes
vm.getMd5Hashbytes = function () {
    var currentDate = moment().format('YYYYMMDD');
    var md5Hash = md5.createHash(currentDate + 'mytest@+');
    return md5Hash;
}

Learn more about the angular-md5 module

Q: Why does SQL Server show 0x as a prefix, whereas JavaScript does not? Can you explain this difference?

Answer №1

This issue is simply related to formatting differences. Despite this, both versions are generating the same sequence of bytes. SQL Server and node.js just have distinct conventions for displaying these bytes in a human-readable manner.

You can achieve a similar format by explicitly instructing SQL Server on how to format your binary data

declare @hashAsBinary varbinary(max)
declare @hashAsText char(32)
set @hashAsBinary = HASHBYTES('MD5', '20160818mytest@+')
set @hashAsText = LOWER(CONVERT(varchar(max), @hashAsBinary, 2))
select @hashAsText

Output:

5c8c8aafe7ae37ea4ebdf8bfa01f82b8

Reference: SQL Server converting varbinary to string

Answer №2

Explaining the difference between two data types can be complex, requiring more space than a comment allows. Therefore, I will elaborate on it in an answer instead.

Take a look at the source code you are referring to. Towards the end (lines 210 and 212), you'll notice that it converts the binary value into a hex string (and then to lower case, although this is not crucial unless there's a string comparison involved at the end). Essentially, your JavaScript library returns a representation as a hex formatted string.

On the other hand, your Sql function HASHBYTES produces a result of type varbinary (a different type from varchar, which is a string type).

Essentially, you have two distinct data types (each residing in their own realm since one has not been transformed to match the other). You haven't specified where the comparison is taking place - within the database or between the database and script. Regardless, for a valid comparison, you need to convert one type to ensure you're either comparing two string types or two binary types. Failure to compare similar types may lead to unexpected outcomes or runtime errors.

If the comparison involves strings in JavaScript, refer back to the library you are utilizing - it already features a method called wordToHex. Simply copy that and use it to convert your Sql result into a string so you can perform a string comparison (remember to account for case sensitivity or convert everything to lower case).


Edit

I am unfamiliar with WebApi as it functions as a black box third-party service. My task is simply to send the security token mentioned above.

If the web api accepts a type like byt[], attaching 0x to your string in JavaScript before sending it to the web api could potentially work. The web api would interpret the incoming parameter as a byte array and conduct the comparison using appropriate data types. Due to the opaque nature of this black box, confirming the accepted type as a byte array requires either direct inquiry or testing.

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

Exploring Node.js and JSON: Retrieving specific object attributes

Utilizing ExpressJS, NodeJS, and Bookshelf.js In an attempt to display a user's list of friends, encountering the error "Unhandled rejection TypeError: Cannot read property 'friends' of undefined" when trying to access a property of the obj ...

Problem with detecting collisions in Three.js

Currently, I am developing a simple game where players can add objects (cubes) to the scene at the position of a raycaster (mouse) click on a large ground plane. To prevent cubes from overlapping with each other, I have implemented basic collision detectio ...

What steps can I take in JavaScript to assign a value of 0 to values[1] in order to prevent receiving NaN as the output

I'm currently working on a program that calculates the sum of 6 input values, but I've encountered an issue where if a value is missing (for example, only providing 5 values), the result becomes NaN. I attempted to address this by assigning empty ...

Laravel: The current configuration does not support the experimental syntax 'classProperties' at this time

When compiling my javascript files using npm run dev, I encountered a warning in my resource/app.js file where I require my custom validation script. The warning stated the following: Module build failed (from ./node_modules/babel-loader/lib/index.js): Syn ...

When the video message bubble is touched within the chat app, a video player will automatically pop up (using React Native)

Currently, I am developing a chat app in React Native that can handle the sending and receiving of video files. However, I am facing challenges with activating the video player when a user presses on the video message inside the chat bubble. Despite my att ...

Issue with Ionic Native File: File.writeFile function - file is not being created and there is no callback response

I've exhausted all the different solutions I could find, but unfortunately, the file isn't getting saved and nothing seems to be happening. The callback functions aren't being called - neither success nor error. Here are the solutions I&apo ...

Creating interactive table columns in ASP.NET MVC Razor with AngularJS for a dynamic user experience

For my assignment, I am working on creating a dynamic table in ASP.NET MVC Razor. The challenge is to add a new column at runtime when the user fills all the rows of the first column. There is no set limit for the number of columns, and a new column should ...

Grunt encounters a hurdle as it attempts to construct the tasks hierarchy for the ng-grid project within Webstorm on a Windows 2012

Having trouble setting up ng-grid project in WebStorm on Windows 2012. I have double-checked NPM and Grunt references. Here are my current Grunt references: Node interpreter: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7&b ...

remove a specific element from an array

Hey there! I'm attempting to remove only the keys from an array. Here's the initial array: {everyone: "everyone", random: "random", fast response time: "fast response time", less conversations: "less conversatio ...

Converting datetime to time format

I have a datetime column in the format of YYYY-MM-DD HH:mm:ss.SSS. Is there a way to display only the time in the format of HH:mm:ss.SSS without showing the complete datetime? I have attempted the following code, but it does not seem to affect the format: ...

Step-by-step guide on duplicating a div a set number of times

I am looking to replicate the entire div multiple times (e.g., on an A4 paper, top left corner, top right corner, bottom left corner, and bottom right corner). <script language="javascript" type='text/javascript'> function updateD ...

In JavaScript, the clearTimeout function may not always return a

Could someone please help me troubleshoot the issue in my code snippet below? I am trying to declare a public variable and assign it to a setTimeout function. If the variable is not null, I want to clear the timeout before setting it again. However, when ...

Utilize a button in React as a way to simultaneously submit a form and redirect with an href

I've created a form where users can input their information and click on a send button to submit it (see code below, button at the end). The form works fine, but when a user clicks on send, the input disappears which might give the impression that the ...

Retrieve data from AJAX once the cycle is complete

Currently, I am attempting to calculate the sum of results from an external API by making a single request for each keyword I possess. The code is functioning properly; however, the function returns the value before all the ajax requests are completed. Eve ...

The initial rendering of the connected component is unsuccessful due to the fact that the Redux state has not been fully

Currently, I am utilizing Redux thunk and axios to handle server requests and update the state based on the response. An issue arises when trying to render a connected component with an initial state that relies on data from the server. In this scenario, ...

Arranging a group of objects in Angular2

I am facing challenges with organizing an array of objects in Angular2. The structure of the object is as follows: [ { "name": "t10", "ts": 1476778297100, "value": "32.339264", "xid": "DP_049908" }, { "name": "t17", "ts": 14 ...

I encountered an ERR_CONNECTION_REFUSED issue after deploying my Node.js application with Socket.io to Heroku

Upon running my live node.js Heroku app, I encountered the following error message in the web console: polling-xhr.js:264 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M2MDZUw net::ERR_CONNECTION_REFUSED Interestingly, when testin ...

Transferring data from a text area to a table with AngularJS

Creating a C# MVC App with AngularJS My goal is to populate a table with values entered in a text area using Angular. Here is the process: Users input values into a text area like this: A B C When a user clicks a button, a modal window should open and ...

There is a complete absence of light within my three.js scene

Hey there! I'm facing an issue with my code. I've been working on a project using three.js which includes a scene, 2 objects, renderer and camera. However, when I added the light, it didn't show up at all! I've tried multiple options bu ...

Error: Unable to encode data into JSON format encountered while using Firebase serverless functions

I am currently working on deploying an API for my application. However, when using the following code snippet, I encountered an unhandled error stating "Error: Data cannot be encoded in JSON." const functions = require("firebase-functions"); const axios = ...