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

Time:10-26

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
}

CodePudding user response:

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);

CodePudding user response:

<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

  • Related