OpenCrypto
OpenCrypto is a lightweight, high performance, standard-compliant JavaScript library built on top of Web Cryptography API. This library makes it easier to implement cryptography in a browser with less code. It can convert and encode ASN.1, PEM and CryptoKey. OpenCrypto is created and maintained by SafeBash.
Import into your web application
<script type="text/javascript" src="OpenCrypto.min.js"></script>
const crypt = new OpenCrypto()
or
import OpenCrypto from 'opencrypto'
const crypt = new OpenCrypto()
Conversion of CryptoKey, PEM and Base64
crypt.cryptoPrivateToPem(privateKey).then(privatePem => {
console.log(privatePem)
})
crypt.pemPrivateToCrypto(pem, options).then(cryptoPrivate => {
console.log(cryptoPrivate)
})
crypt.cryptoPublicToPem(publicKey).then(publicPem => {
console.log(publicPem)
})
crypt.pemPublicToCrypto(pem, options).then(cryptoPublic => {
console.log(cryptoPublic)
})
crypt.cryptoToBase64(key, type).then(base64Key => {
console.log(base64Key)
})
crypt.base64ToCrypto(key, options).then(cryptoKey => {
console.log(cryptoKey)
})
Asymmetric Encryption
crypt.getRSAKeyPair(modulusLength, hash, paddingScheme, usages, isExtractable).then(keyPair => {
console.log(keyPair.publicKey)
console.log(keyPair.privateKey)
})
crypt.rsaEncrypt(publicKey, data).then(encryptedData => {
console.log(encryptedData)
})
crypt.rsaDecrypt(privateKey, encryptedData).then(decryptedData => {
console.log(decryptedData)
})
crypt.getECKeyPair(curve, type, usages, isExtractable).then(keyPair => {
console.log(keyPair.privateKey)
console.log(keyPair.publicKey)
})
crypt.getPublicKey(privateKey, options).then(publicKey => {
console.log(publicKey)
})
crypt.encryptPrivateKey(privateKey, passphrase, iterations, hash, cipher, length).then(encryptedPrivateKey => {
console.log(encryptedPrivateKey)
})
crypt.decryptPrivateKey(encryptedPrivateKey, passphrase, options).then(decryptedPrivateKey => {
console.log(decryptedPrivateKey)
})
crypt.keyAgreement(privateKey, publicKey, options).then(sharedKey => {
console.log(sharedKey)
})
Symmetric Encryption
crypt.getSharedKey(length, options).then(sharedKey => {
console.log(sharedKey)
})
crypt.encryptKey(wrappingKey, key).then(encryptedKey => {
console.log(encryptedKey)
})
crypt.decryptKey(unwrappingKey, encryptedKey, options).then(decryptedKey => {
console.log(decryptedKey)
})
crypt.signKey(privateKey, key, options).then(keySignature => {
console.log(keySignature)
})
crypt.verifyKey(publicKey, key, signature, options).then(isValid => {
console.log(isValid)
})
crypt.sign(privateKey, data, options).then(signature => {
console.log(signature)
})
crypt.verify(publicKey, data, signature, options).then(isValid => {
console.log(isValid)
})
crypt.encrypt(sharedKey, data).then(encryptedData => {
console.log(encryptedData)
})
crypt.decrypt(sharedKey, encryptedData, options).then(decryptedData => {
console.log(decryptedData)
})
Passphrase derivation
crypt.derivePassphraseKey(passphrase, salt, iterations, options).then(derivedKey => {
console.log(derivedKey)
})
crypt.hashPassphrase(passphrase, salt, iterations, options).then(hashedPassphrase => {
console.log(derivedHash)
})
crypt.getFingerprint(key, options).then(fingerprint => {
console.log(fingerprint)
})
Other
crypt.getRandomBytes(size).then(data => {
console.log(data)
})
Standards Compliance
RFC 5280
RFC 6090
RFC 5208
RFC 5480
RFC 5915
RFC 8018
RFC 3394
NIST SP 800-38A
NIST SP 800-38B
NIST SP 800-38D
NIST SP 800-56A
NIST SP 800-56C
NIST FIPS 180-4
Contributors
Peter Bielak
Andrew Kozlik, Ph.D.
License
MIT