-1
日增长
16,397
stars
2,488
forks
243
watchers
78
prs
427
issues
0
releases
README
crypto-js
JavaScript library of crypto standards.
Discontinued
Active development of CryptoJS has been discontinued. This library is no longer maintained.
Nowadays, NodeJS and modern browsers have a native Crypto module. The latest version of CryptoJS already uses the native Crypto module for random number generation, since Math.random() is not crypto-safe. Further development of CryptoJS would result in it only being a wrapper of native Crypto. Therefore, development and maintenance has been discontinued, it is time to go for the native crypto module.
Node.js (Install)
Requirements:
- Node.js
- npm (Node.js package manager)
bash
npm install crypto-js
Usage
ES6 import for typical API call signing use case:
javascript
import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';
const message, nonce, path, privateKey; // ...
const hashDigest = sha256(nonce + message);
const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));
Modular include:
javascript
var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
...
console.log(SHA256("Message"));
Including all libraries, for access to extra methods:
javascript
var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));
Client (browser)
Requirements:
- Node.js
- Bower (package manager for frontend)
bash
bower install crypto-js
Usage
Modular include:
javascript
require.config({
packages: [
{
name: 'crypto-js',
location: 'path-to/bower_components/crypto-js',
main: 'index'
}
]
});
require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) {
console.log(SHA256("Message"));
});
Including all libraries, for access to extra methods:
javascript
// Above-mentioned will work or use this simple form
require.config({
paths: {
'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'
}
});
require(["crypto-js"], function (CryptoJS) {
console.log(CryptoJS.HmacSHA1("Message", "Key"));
});
Usage without RequireJS
html
var encrypted = CryptoJS.AES(...);
var encrypted = CryptoJS.SHA256(...);
API
See: https://cryptojs.gitbook.io/docs/
AES Encryption
Plain text encryption
javascript
var CryptoJS = require("crypto-js");
// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString();
// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);
console.log(originalText); // 'my message'
Object encryption
javascript
var CryptoJS = require("crypto-js");
var data = [{id: 1}, {id: 2}]
// Encrypt
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();
// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
consoTop issues
#85Cannot read property 'sigBytes' of undefined26 comments · 2017-02-22#168ES6 Imports are not working as described20 comments · 2018-08-09#271Malformed UTF-8 data18 comments · 2020-03-03#262Angular & native crypto module18 comments · 2020-02-12#195Help Needed: How to decrypt AES 256 CBC with IV15 comments · 2018-12-24#143AES Encrypt and decrypt from Hex15 comments · 2018-01-13#93Cannot read property 'words' of undefined at crypto-js.js : 23215 comments · 2017-05-14#303Does crypto-js work with react-native?14 comments · 2020-06-17