0

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
}
3
  • Why don't you use bcrypt instead? I believe it's more secure than md5 Commented Oct 26, 2021 at 5:16
  • BTW: You should not rely on client side crypto functions. This should be done on the server side/backend Commented Oct 26, 2021 at 6:31
  • As stated before this shouldn't be handled client side, but more importantly MD5 is very outdated and insecure for passwords use bcrypt instead or even better use an auth provider Commented Nov 12, 2022 at 19:05

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

-1
<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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.