Javascript Cryptography Examples

Demonstration of cryptography in Javascript with the CryptoJS library v3.1.2. This library is out of date. The examples below are illustrational and should not be used in production.

Hashes

Hash functions can encrypt text, but hashes cannot be decrypted. They are useful for encrypting passwords and authenticating messages.

The CryptoJS hash functions return WordArray objects.

MD5

MD5 is a popular hash, but is no longer considered secure.

SHA-1

SHA-2

SHA-2 is a set of hash algorithms.

SHA-256

SHA-512

SHA-3

SHA-3 can output hash lengths of 224, 256, 384, or 512 bits. The default is 512 bits.

RIPEMD

CryptoJS provides a RIPEMD-160 function which is used in Bitcoin.

Ciphers

Ciphers can encrypt and decrypt data.

DES

The Data Encryption Standard was developed by IBM in the early 1970's and was adopted by the Federal Information Processing Standard (FIPS) in 1977. It is now considered insecure, but the TripleDES function is still considered secure.

AES

The Advanced Encryption Standard is based on Rijndael. Adopted by NIST in 2001.

Rabbit

Stream cipher introduced in 2003.

RC4

Rivest Cipher 4. Designed in 1987 and leaked in 1994. While insecure, its speed and simplicity led to wide adoption. It is utilized in SSL, WEP and TLS protocols.

TEA

The Tiny Encryption Algorithm was designed by David Wheeler and Roger Needham in 1994. Nowadays, it is considered a weak cipher and has been supplanted by Corrected Block TEA (XXTEA). A Javascript implementation was written by Chris Veness.