create a md5 hash from a string in javascript

To create an MD5 hash from a string in JavaScript, you can use the built-in crypto module available in Node.js. Here's an example code snippet:

index.tsx
// Import the crypto module
const crypto = require('crypto');

// Define the string to be hashed
const message = 'Hello, world!';

// Create an instance of the MD5 hash object
const hash = crypto.createHash('md5');

// Pass the string to be hashed to the hash object
hash.update(message);

// Generate the MD5 hash in hexadecimal format
const hashHex = hash.digest('hex');

// Print the MD5 hash
console.log(hashHex); // Output: '86fb269d190d2c85f6e0468ceca42a20'
464 chars
18 lines

In the code above, we first import the crypto module. Then, we define the string to be hashed as the variable message.

Next, we create an instance of the MD5 hash object using crypto.createHash('md5'). We pass the string message to the hash object using .update(message).

We then generate the MD5 hash in hexadecimal format using .digest('hex') and store it in the variable hashHex. Finally, we print the MD5 hash to the console using console.log(hashHex).

gistlibby LogSnag