Nick Perum
Nick Perum

Reputation: 3

How to generate an MD5 password string hash in JavaScript or Node.js?

I need to write a function to generate an md5 hash for a password on a site page. Help write the md5 function?

For example:

function handleEvent() {
   var md5 = generateMD5(document.getElementById("password").value);
   console.log(md5);
}

function generateMD5(string) {
   // generate md5 hash here
}

Upvotes: 0

Views: 6858

Answers (2)

simetrio
simetrio

Reputation: 46

You can use the already written md5 generation function: GitHub (npm, Demo)

Using script tag:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/md5.min.js"></script>
<script>
   var hash = MD5.generate("Example string");
   console.log(hash);
</script>

With ES5:

import { MD5 } from "md5-js-tools";
var hash = MD5.generate("Example string");
console.log(hash);

or

const { MD5 } = require("md5-js-tools");
var hash = MD5.generate("Example string");
console.log(hash);

Upvotes: 3

Moinul Robin
Moinul Robin

Reputation: 36

<script type="text/javascript" src="md5.min.js"></script>
<script>
window.addEventListener('load', function() {
    var strHash = md5('tutsplus');
    alert('The MD5 hash of the tutsplus string is:' + strHash);
});
</script>

Ref: For more details follow the link

Upvotes: -1

Related Questions