Generating an MD5 hash for a UTF16LE string in Javascript (with no BOM and excluding 0-byte at the end) - Illustrated with a C#

I've been trying to figure out how to get an MD5 Hash of a UTF16-LE encoded string in JavaScript for the past few days. I have a method in C# as an example, but I'm not sure how to replicate it in JavaScript.

Example:

public string GetMD5Hash (string input) { 
 MD5 md5Hasher = MD5.Create(); 
 byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(input)); 
 StringBuilder sb = new StringBuilder(); 
 for (int i = 0; i < data.Length; i++) { 
  sb.Append(data[i].ToString("x2")); 
 } 
 return sb.ToString(); 
}

Desired Output:

var getMD5Hash(input){
  ....
}

var t = getMD5Hash("1234567z-äbc");
console.log(t) // --> 9e224a41eeefa284df7bb0f26c2913e2 

Any help would be greatly appreciated :-/

Answer №1

Voila!

const secretCode = challenge + "-" + require('crypto').createHash('md5').update(Buffer(challenge+'-'+password, 'UTF-16LE')).digest('hex')

Answer №2

let md5 = require('md5');

function encodeStringToMD5_UTF16LE(str){
    let bytes = [];
    for (let i = 0; i < str.length; ++i) {
      let charCode = str.charCodeAt(i);
      bytes = bytes.concat([charCode & 0xff, charCode / 256 >>> 0]);
    }
    return md5(bytes);
}

This code snippet requires the library https://www.npmjs.com/package/md5

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

Adding custom data attributes to HTML tags in Vue.js

I have a question regarding adding a data attribute to the <html> element in vue.js (v2). I haven't been able to find any guidance on how to do this in the auto generated code or the documentation. The end goal is to achieve this desired output ...

The highlight_row function seems to be delayed, as it does not trigger on the initial onClick but works on subsequent clicks. How can I ensure it works properly right from the first click?

I developed an application using the Google JavaScript Maps API to calculate distances between addresses. I've encountered a bug where the row in the table is not highlighted on the first click, requiring a second click for the highlighting to take ef ...

The AngularJS beginner routing application is malfunctioning and needs fixing

I've been diving into Angular JS but hit a roadblock with a basic angular routing program. I need some guidance on what's going wrong. If you want to check out the complete project code, visit my GitHub repository: https://github.com/ashpratap00 ...

Optimal method for parsing URLs using ES6

There have been many remarkable inquiries regarding this topic, such as: how to parse a url. However, as time has gone by, the answers I come across are outdated. I am looking for a more modern and flexible method to parse URLs, without relying on regular ...

Using ReactJS to trigger a timer function on a button click

Kindly Note: This is a unique query and not related to ReactJS - Need to click twice to set State and run function. The suggested solution there does not resolve my issue. This represents my original state: constructor(props) { super(props) this. ...

Attempting to control an array of objects

In my current records: The parts with IDs 14.3, 14.2, and 14.1 belong to part ID = 30. The goal is to achieve the following: 1) By default, the first two IDs will be selected. If a user tries to select ID = 71, which belongs to part 30, they should not ...

Loading JSON data from a file in an AngularJS service

Looking to retrieve JSON data from a file named driverStandings.json, which can be found in the structure from the provided image. I am utilizing a service API to fetch this information by using the code displayed below (refer to image). After compiling, ...

Vuetify: Utilizing Time Range Inputs for Start and End Time

I am encountering some difficulty in identifying what I may have missed. I am dealing with 2 inputs: time, startTime, and endTime startTime <v-col cols="12" sm="6" md="2"> <v-menu ref="menu" ...

Creating a custom function in JavaScript to interact with the `windows.external` object specifically for use

In my current project, I am facing an issue with JavaScript file compatibility across different browsers. Specifically, I have a JavaScript file that calls an external function (from a separate file) using windows.external, like this: windows.external.se ...

Vue: restrict entry to the view unless props are configured

Currently, I am in the process of creating a Vue game that consists of two main views: a 'setup' view and a 'play' view. The values that are configured in the setup view are then passed as props to the play view, initiating the game wit ...

Guide to sending client-to-client notifications in Angular/Ionic with Firebase Cloud Messaging

I am looking to implement client-client push notifications (not server-to-client). My goal is to send a notification when one user messages another. Is this feasible? How can I achieve this using the structure in the Firebase real-time database? Here is a ...

Unable to detect errors using React/Redux

I encountered a login error handling issue with redux that is structured like this: export const login = (params: any) => async (dispatch: Dispatch) => { try { const authData = await API.post("login", params); sessionStorage.setIt ...

Transforming retrieved data into an array using Node.js

Got some data from a URL that looks like this: data=%5B1%2C2%2C3%2C4%2C0%5D which when decoded is [1,2,3,4,0]. Used the snippet below to parse the data: var queryObj = querystring.parse( theUrl.query ); Seems like it's not an array. How can I con ...

Prevent the parent component's ripple effect from being activated by the child component

If I have a simple code snippet like the following: <ListItem button={true} > <Typography variant='caption' color='primary'> {value} </Typography> <Button onClick={foo} > Button ...

How can I make the top two divs stay fixed as I scroll down, and then reset back to their initial position when scrolled

Hey there, I'm looking to have a div stay fixed after scrolling within its parent div. <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> <div class="fw ofndr-box"> <div class="ofndr-box-half add-here"> <a href="#! ...

Convert an array into a query string parameter

Currently, I am refactoring some code and attempting to replace the generation of query strings through concatenation with serializing a JSON object. Original Code: $.ajax({ url:'./services/DataService/getDetails?metric=9&'+dashBoards.g ...

Selecting options using AngularJS to parse through a list

I am faced with a challenge involving a collection of strings representing years. Here is an example: $scope.years = ["2001", "2002", "2003", ...]; My goal is to display these values in a select tag within a web page. However, whenever I attempt this usi ...

Is tsconfig.json Utilized by Gatsby When Using Typescript?

Many blog posts and the example on Gatsby JS's website demonstrate the use of a tsconfig.json file alongside the gatsby-plugin-typescript for TypeScript support in Gatsby. However, it seems like the tsconfig.json file is not actually utilized for conf ...

Looping through objects within objects using .map in React can be done by iterating over

This is the information I have export const courses = [ { id: 0, title: "first year", subjects: [ { id: 0, class: "french" }, { id: 1, class: "history" }, { id: 2, class: "geometry" } ...

Utilizing JSON API data to populate Leaflet maps

I am currently working on fetching JSON data from an API call, extracting the latitude, longitude, and name variables to create a GeoJSON array, and then displaying it on a Leaflet map. Despite not encountering any errors in the console, the geojson appea ...