Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 944
krishna Avatar asked Dec 28 '18 10:12

krishna


People also ask

How do you define CryptoJS?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

What is CryptoJS in Nodejs?

Crypto is a module in Node. js which deals with an algorithm that performs data encryption and decryption. This is used for security purpose like user authentication where storing the password in Database in the encrypted form. Crypto module provides set of classes like hash, HMAC, cipher, decipher, sign, and verify.

What is CryptoJS sha256?

crypto-js/hmac-sha256An HMAC is a message authentication code that uses a hash algorithm. In this example, an HMAC is demonstrated using the sha256 algorithm, but any supported algorithm will work. var hmac = CryptoJS.HmacSHA256("message", "secretkey");

How do you use CryptoJS in react?

To encrypt and decrypt data, simply use encrypt() and decrypt() function from an instance of crypto-js. var bytes = CryptoJS. AES. decrypt(ciphertext, 'my-secret-key@123');


1 Answers

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>
like image 179
Mortz Avatar answered Nov 14 '22 23:11

Mortz