What is sjcl?
The Stanford Javascript Crypto Library (SJCL) is a library for cryptography in JavaScript. It provides a variety of cryptographic functions including encryption, hashing, and key derivation.
What are sjcl's main functionalities?
Encryption and Decryption
This feature allows you to encrypt and decrypt messages using a password. The `sjcl.encrypt` function takes a password and plaintext message, returning an encrypted string. The `sjcl.decrypt` function takes the same password and the encrypted string to return the original plaintext.
const sjcl = require('sjcl');
// Encrypt a message
const password = 'password123';
const plaintext = 'Hello, World!';
const encrypted = sjcl.encrypt(password, plaintext);
console.log('Encrypted:', encrypted);
// Decrypt the message
const decrypted = sjcl.decrypt(password, encrypted);
console.log('Decrypted:', decrypted);
Hashing
This feature allows you to hash messages using the SHA-256 algorithm. The `sjcl.hash.sha256.hash` function takes a message and returns a hash in bit array format. The `sjcl.codec.hex.fromBits` function converts the bit array to a hexadecimal string.
const sjcl = require('sjcl');
// Hash a message
const message = 'Hello, World!';
const hash = sjcl.hash.sha256.hash(message);
const hashHex = sjcl.codec.hex.fromBits(hash);
console.log('Hash:', hashHex);
Key Derivation
This feature allows you to derive cryptographic keys from passwords using the PBKDF2 algorithm. The `sjcl.misc.pbkdf2` function takes a password, salt, iteration count, and key length, returning a derived key in bit array format. The `sjcl.codec.hex.fromBits` function converts the bit array to a hexadecimal string.
const sjcl = require('sjcl');
// Derive a key from a password
const password = 'password123';
const salt = sjcl.random.randomWords(2, 0); // Generate a random salt
const key = sjcl.misc.pbkdf2(password, salt, 1000, 256);
const keyHex = sjcl.codec.hex.fromBits(key);
console.log('Derived Key:', keyHex);
Other packages similar to sjcl
crypto-js
CryptoJS is a widely-used library that provides standard and secure cryptographic algorithms for JavaScript. It supports a variety of cryptographic functions including AES encryption, SHA hashing, and HMAC. Compared to SJCL, CryptoJS offers a broader range of algorithms and is more commonly used in the JavaScript community.
forge
Forge is a comprehensive cryptographic library for JavaScript that supports a wide range of cryptographic functions including encryption, hashing, and digital signatures. It also provides tools for working with X.509 certificates and TLS. Forge is more feature-rich compared to SJCL and is suitable for more complex cryptographic tasks.
tweetnacl
TweetNaCl is a cryptographic library that focuses on simplicity and security. It provides a small set of high-level cryptographic functions including public-key encryption, secret-key encryption, and hashing. Compared to SJCL, TweetNaCl is designed to be minimalistic and easy to audit, making it a good choice for security-critical applications.
sjcl
Stanford Javascript Crypto Library
Security Advisories
- 12.02.2014: the current development version has a paranoia bug in the ecc module. The bug was introduced in commit ac0b3fe0 and might affect ecc key generation on platforms without a platform random number generator.
Security Contact
Security Mail: sjcl@ovt.me
OpenPGP-Key Fingerprint: 0D54 3E52 87B4 EC06 3FA9 0115 72ED A6C7 7AAF 48ED
Keyserver: pool.sks-keyservers.net
Upgrade Guide
1.0.3 -> 1.0.4
codecBase32
has been re-enabled with changes to conform to RFC 4648:
- Padding with
=
is now applied to the output of fromBits
. If you don't want that padding, you can disable it by calling fromBits
with a second parameter of true
or anything that evaluates as "truthy" in JS - The encoding alphabet for
sjcl.codec.base32
now matches that specified by the RFC, rather than the extended hex alphabet. - The former extended hex alphabet is now available through
sjcl.codec.base32hex
(also matching the RFC). So if you encoded something with base32
before, you'll want to decode it with base32hex
now.
Documentation
The documentation is available here