Search code examples
javascripthashmd5cryptojs

ReferenceError: CryptoJs is not defined


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>


Solution

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