Crypto-JS 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.
Discussion GroupQuick-start
... [More]
GuideMD5SHA-1SHA-256AESRabbitMARC4HMACHMAC-MD5HMAC-SHA1HMAC-SHA256PBKDF2Utilities
Discussion GroupShare your praises and criticims at the Crypto-JS discussion group.
http://groups.google.com/group/crypto-js/topics Quick-start GuideMD5MD5 is a widely used hash function. It's been used in a variety of security applications and is also commonly used to check the integrity of files. Though, MD5 is not collision resistant, and it isn't suitable for applications like SSL certificates or digital signatures that rely on this property.
var digest = Crypto.MD5("Message");
var digestBytes = Crypto.MD5("Message", { asBytes: true });
var digestString = Crypto.MD5("Message", { asString: true });
SHA-1The SHA hash functions were designed by the National Security Agency (NSA). SHA-1 is the most established of the existing SHA hash functions, and it's used in a variety of security applications and protocols. Though, SHA-1's collision resistance has been weakening as new attacks are discovered or improved.
var digest = Crypto.SHA1("Message");
var digestBytes = Crypto.SHA1("Message", { asBytes: true });
var digestString = Crypto.SHA1("Message", { asString: true });
SHA-256SHA-256 is one of the three variants in the SHA-2 set. It isn't as widely used as SHA-1, though it appears to provide much better security.
var digest = Crypto.SHA256("Message");
var digestBytes = Crypto.SHA256("Message", { asBytes: true });
var digestString = Crypto.SHA256("Message", { asString: true });
AESThe Advanced Encryption Standard (AES) is a U.S. Federal Information Processing Standard (FIPS). It was selected after a 5-year process where 15 competing designs were evaluated.
var crypted = Crypto.AES.encrypt("Message", "Secret Passphrase");
var plain = Crypto.AES.decrypt(crypted, "Secret Passphrase");
RabbitRabbit is a high-performance stream cipher and a finalist in the eSTREAM Portfolio. It is one of the four designs selected after a 3 1/2-year process where 22 designs were evaluated.
var crypted = Crypto.Rabbit.encrypt("Message", "Secret Passphrase");
var plain = Crypto.Rabbit.decrypt(crypted, "Secret Passphrase");
MARC4MARC4 (Modified Allegedly RC4) is based on RC4, a widely-used stream cipher. RC4 is used in popular protocols such as SSL and WEP. But though it's remarkable for its simplicity and speed, it has weaknesses. Crypto-JS provides a modified version that corrects these weaknesses, but the algorithm's history still doesn't inspire confidence in its security.
var crypted = Crypto.MARC4.encrypt("Message", "Secret Passphrase");
var plain = Crypto.MARC4.decrypt(crypted, "Secret Passphrase");
HMACKeyed-hash message authentication codes (HMAC) is a mechanism for message authentication using cryptographic hash functions. HMAC can be used in combination with any iterated cryptographic hash function.
HMAC-MD5
var hmac = Crypto.HMAC(Crypto.MD5, "Message", "Secret Passphrase");
var hmacBytes = Crypto.HMAC(Crypto.MD5, "Message", "Secret Passphrase", { asBytes: true });
var hmacString = Crypto.HMAC(Crypto.MD5, "Message", "Secret Passphrase", { asString: true });
HMAC-SHA1
var hmac = Crypto.HMAC(Crypto.SHA1, "Message", "Secret Passphrase");
var hmacBytes = Crypto.HMAC(Crypto.SHA1, "Message", "Secret Passphrase", { asBytes: true });
var hmacString = Crypto.HMAC(Crypto.SHA1, "Message", "Secret Passphrase", { asString: true });
HMAC-SHA256
var hmac = Crypto.HMAC(Crypto.SHA256, "Message", "Secret Passphrase");
var hmacBytes = Crypto.HMAC(Crypto.SHA256, "Message", "Secret Passphrase", { asBytes: true });
var hmacString = Crypto.HMAC(Crypto.SHA256, "Message", "Secret Passphrase", { asString: true });
PBKDF2PBKDF2 is a password-based key derivation function. In many applications of cryptography, user security is ultimately dependent on a password. And because a password usually cannot be used directly as a cryptographic key, some processing is required.
A salt in password-based cryptography provides a large set of keys for any given password. An iteration count has traditionally served the purpose of increasing the cost of producing keys from a password, thereby also increasing the difficulty of attack.
var salt = Crypto.charenc.Binary.bytesToString(Crypto.util.randomBytes(16));
var key128bit = Crypto.PBKDF2("Secret Passphrase", salt, 16);
var key256bit = Crypto.PBKDF2("Secret Passphrase", salt, 32);
var key512bit = Crypto.PBKDF2("Secret Passphrase", salt, 64);
var key512bit1000 = Crypto.PBKDF2("Secret Passphrase", salt, 64, { iterations: 1000 });
Utilities
var helloBytes = Crypto.charenc.Binary.stringToBytes("Hello, World!");
var helloString = Crypto.charenc.Binary.bytesToString(helloBytes);
var utf8Bytes = Crypto.charenc.UTF8.stringToBytes("България");
var unicodeString = Crypto.charenc.UTF8.bytesToString(utf8Bytes);
var helloHex = Crypto.util.bytesToHex(helloBytes);
var helloBytes = Crypto.util.hexToBytes(helloHex);
var helloBase64 = Crypto.util.bytesToBase64(helloBytes);
var helloBytes = Crypto.util.base64ToBytes(helloBase64); [Less]