7

I tried to hash a text in client-side. I used following code to hash it, but it shows this Reference Error.

<html>
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/md5.js">
 </script>
</head>
<body>
 <script>
 var plaintext = "hiii";
 var encrptedText = CryptoJs.md5(plaintext);
 alert("Encrpted Text : " + encrptedText.toString());
 </script>
</body>
</html>

Sebastian Simon
19.8k8 gold badges61 silver badges88 bronze badges
asked Dec 28, 2018 at 10:09
3
  • There’s an error in the JS file itself. Commented Dec 28, 2018 at 10:21
  • 2
    the variable is named CryptoJS not CryptoJs Commented Dec 28, 2018 at 10:24
  • If you try to evaluate the script in the browser console, you get the following error: Uncaught TypeError: Cannot read property 'lib' of undefined Commented Dec 28, 2018 at 10:24

2 Answers 2

16

Use the entire package - not just the md5 module - change the src in your script tag

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script></head>
<body>
<script>
var plaintext="hiii";
var encrptedText = CryptoJS.MD5(plaintext)
alert("Encrpted Text : "+ encrptedText.toString());
</script>
 </body>
</html>
answered Dec 28, 2018 at 10:35
Sign up to request clarification or add additional context in comments.

Comments

1

If for you important the size of extended libraries, that you can use pure-md5 (4.76kb) instead crypto-js (187.44kb).

<html>
<head>
<script src="https://unpkg.com/pure-md5@latest/lib/index.js">
 </script>
</head>
<body>
 <script>
 var plaintext = "hiii";
 var encrptedText = md5(plaintext);
 alert("Encrpted Text : " + encrptedText.toString());
 </script>
</body>
</html>

answered Jun 19, 2019 at 13:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.