• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

All things PKI, HTTPS, SSL, TLS, Digital Certificates

  • Buy SSL Certificates
  • Blog
  • Java Keytool
  • OpenSSL
  • Certificate Decoder
  • Donate

cryptojs (crypto-js)

March 20, 2020 by Mister PKI 2 Comments

What is cryptojs?

cryptojs is a library in javascript complete with cryptographic functions including encryption, decryption, and hashing functions. crypto-js is licensed under the MIT license.

crypto-js/hmac-sha256

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

cryptojs aes encrypt

AES (Advanced Encryption Standard) is a popular symmetric encryption algorithm that uses a shared secret key for both encryption and decryption. The example demonstrates AES encryption with a shared secret key “secretkey”.

function encryptWithSecretOnly() {
   var encrypted = CryptoJS.AES.encrypt("plain text", "secretkey");
}

cryptojs aes decrypt

The example demonstrates the decryption of the previously encrypted data, using the shared secret key “secretkey”.

function decrypt() {
  var encrypted = CryptoJS.AES.encrypt("plain text", "secretkey");
  var bytes  = CryptoJS.AES.decrypt(encrypted, "secretkey");
  var decrypted = bytes.toString(CryptoJS.enc.Utf8);
}

crypto-js hashing algorithm examples

See https://csrc.nist.gov/projects/hash-functions for a more in-depth analysis of the hashing algorithms before making a choice on which algorithm you should use.

cryptojs sha1

SHA1 is a secure hash algorithm that produces a 160 bit output. SHA1 has recently been deemed not secure, and members of the SHA2 family are recommended for cryptographic hashing.

var sha1 = CryptoJS.SHA1(document.getElementById("password").value);

cryptojs sha256

It generates a 32-byte output and is one of the more commonly used hashing algorithms today.

var sha256 = CryptoJS.SHA256(document.getElementById("password").value);

cryptojs sha224

While meeting the security requirement for 112-bits of security, it is 32 bits shorter than SHA256.

var sha224 = CryptoJS.SHA224(document.getElementById("password").value);

cryptojs sha512

Produces 512 bits of output and is probably overkill in the near future.

var sha512 = CryptoJS.SHA512(document.getElementById("password").value);

cryptojs sha384

Useful to generating a 256-bit HMAC key and 128-bit encryption key.

var sha384 = CryptoJS.SHA384(document.getElementById("password").value);

cryptojs sha3

SHA3 is not meant to replace SHA2 but is merely an additional tool in the NIST toolkit.

var sha3 = CryptoJS.SHA3(document.getElementById("password").value);

cryptojs ripemd160

Considered a robust algorithm and alternative to the SHA family.

var ripemd160 = CryptoJS.RIPEMD160(document.getElementById("password").value);

cryptojs md5

MD5 was once one of the more popular hashing algorithms, but has many vulnerabilities and is not recommended today.

var md5 = CryptoJS.MD5(document.getElementById("password").value);

How to install crypto-js with npm

npm install crypto-js

The library may also be used by including the source in a script tag. The sources can be found in the Cloudfare CDN.

jsfiddle examples

jsfiddle cryptojs aes encryption and decryption example

jsfiddle crypto-js hashing algorithm examples

Read more of our content.

Uncategorized

Reader Interactions

Comments

  1. noob says

    December 12, 2020 at 9:37 pm

    Hi, in jsfiddle crypto-js hashing algorithm examples https://www.misterpki.com/cryptojs/
    What should I do to decrypt sha-256 hash to passwor?

    Reply
    • Mister PKI says

      December 14, 2020 at 8:55 pm

      Hashing is a one-way operation, unlike encryption which is two-way. Hashing is convenient, for example, for an authentication password where the authentication service does not need to know the plain text password, but only the hash. When the user enters his/her password it will be hashed and if the hashed value matches the hash stored by the authentication service the user will have successfully authenticated.

      For clarity of my response, the hash created in the examples cannot be returned back to its plain text form.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Buy SSL Certificates

SSL/TLS Certificate Small Square (200 x 200)

Recent Posts

  • PKCS7
  • PKCS8 (PKCS #8)
  • keytool delete alias – How to delete an alias from a keystore
  • keytool alias -changealias – How to change a private key alias
  • SSL Certificate Expiration and SSL Certificate Renewal

Footer

  • Twitter
  • YouTube

Copyright © 2021 ยท Designed by North Flow Tech