1# crypto-js [![Build Status](https://travis-ci.org/brix/crypto-js.svg?branch=develop)](https://travis-ci.org/brix/crypto-js) 2 3JavaScript library of crypto standards. 4 5## Node.js (Install) 6 7Requirements: 8 9- Node.js 10- npm (Node.js package manager) 11 12```bash 13npm install crypto-js 14``` 15 16### Usage 17 18ES6 import for typical API call signing use case: 19 20```javascript 21import sha256 from 'crypto-js/sha256'; 22import hmacSHA512 from 'crypto-js/hmac-sha512'; 23import Base64 from 'crypto-js/enc-base64'; 24 25const message, nonce, path, privateKey; // ... 26const hashDigest = sha256(nonce + message); 27const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey)); 28``` 29 30Modular include: 31 32```javascript 33var AES = require("crypto-js/aes"); 34var SHA256 = require("crypto-js/sha256"); 35... 36console.log(SHA256("Message")); 37``` 38 39Including all libraries, for access to extra methods: 40 41```javascript 42var CryptoJS = require("crypto-js"); 43console.log(CryptoJS.HmacSHA1("Message", "Key")); 44``` 45 46## Client (browser) 47 48Requirements: 49 50- Node.js 51- Bower (package manager for frontend) 52 53```bash 54bower install crypto-js 55``` 56 57### Usage 58 59Modular include: 60 61```javascript 62require.config({ 63 packages: [ 64 { 65 name: 'crypto-js', 66 location: 'path-to/bower_components/crypto-js', 67 main: 'index' 68 } 69 ] 70}); 71 72require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) { 73 console.log(SHA256("Message")); 74}); 75``` 76 77Including all libraries, for access to extra methods: 78 79```javascript 80// Above-mentioned will work or use this simple form 81require.config({ 82 paths: { 83 'crypto-js': 'path-to/bower_components/crypto-js/crypto-js' 84 } 85}); 86 87require(["crypto-js"], function (CryptoJS) { 88 console.log(CryptoJS.HmacSHA1("Message", "Key")); 89}); 90``` 91 92### Usage without RequireJS 93 94```html 95<script type="text/javascript" src="path-to/bower_components/crypto-js/crypto-js.js"></script> 96<script type="text/javascript"> 97 var encrypted = CryptoJS.AES(...); 98 var encrypted = CryptoJS.SHA256(...); 99</script> 100``` 101 102## API 103 104See: https://cryptojs.gitbook.io/docs/ 105 106### AES Encryption 107 108#### Plain text encryption 109 110```javascript 111var CryptoJS = require("crypto-js"); 112 113// Encrypt 114var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString(); 115 116// Decrypt 117var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123'); 118var originalText = bytes.toString(CryptoJS.enc.Utf8); 119 120console.log(originalText); // 'my message' 121``` 122 123#### Object encryption 124 125```javascript 126var CryptoJS = require("crypto-js"); 127 128var data = [{id: 1}, {id: 2}] 129 130// Encrypt 131var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString(); 132 133// Decrypt 134var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123'); 135var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); 136 137console.log(decryptedData); // [{id: 1}, {id: 2}] 138``` 139 140### List of modules 141 142 143- ```crypto-js/core``` 144- ```crypto-js/x64-core``` 145- ```crypto-js/lib-typedarrays``` 146 147--- 148 149- ```crypto-js/md5``` 150- ```crypto-js/sha1``` 151- ```crypto-js/sha256``` 152- ```crypto-js/sha224``` 153- ```crypto-js/sha512``` 154- ```crypto-js/sha384``` 155- ```crypto-js/sha3``` 156- ```crypto-js/ripemd160``` 157 158--- 159 160- ```crypto-js/hmac-md5``` 161- ```crypto-js/hmac-sha1``` 162- ```crypto-js/hmac-sha256``` 163- ```crypto-js/hmac-sha224``` 164- ```crypto-js/hmac-sha512``` 165- ```crypto-js/hmac-sha384``` 166- ```crypto-js/hmac-sha3``` 167- ```crypto-js/hmac-ripemd160``` 168 169--- 170 171- ```crypto-js/pbkdf2``` 172 173--- 174 175- ```crypto-js/aes``` 176- ```crypto-js/tripledes``` 177- ```crypto-js/rc4``` 178- ```crypto-js/rabbit``` 179- ```crypto-js/rabbit-legacy``` 180- ```crypto-js/evpkdf``` 181 182--- 183 184- ```crypto-js/format-openssl``` 185- ```crypto-js/format-hex``` 186 187--- 188 189- ```crypto-js/enc-latin1``` 190- ```crypto-js/enc-utf8``` 191- ```crypto-js/enc-hex``` 192- ```crypto-js/enc-utf16``` 193- ```crypto-js/enc-base64``` 194 195--- 196 197- ```crypto-js/mode-cfb``` 198- ```crypto-js/mode-ctr``` 199- ```crypto-js/mode-ctr-gladman``` 200- ```crypto-js/mode-ofb``` 201- ```crypto-js/mode-ecb``` 202 203--- 204 205- ```crypto-js/pad-pkcs7``` 206- ```crypto-js/pad-ansix923``` 207- ```crypto-js/pad-iso10126``` 208- ```crypto-js/pad-iso97971``` 209- ```crypto-js/pad-zeropadding``` 210- ```crypto-js/pad-nopadding``` 211 212 213## Release notes 214 215### 4.0.0 216 217This is an update including breaking changes for some environments. 218 219In this version `Math.random()` has been replaced by the random methods of the native crypto module. 220 221For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before or React Native. 222 223### 3.3.0 224 225Rollback, `3.3.0` is the same as `3.1.9-1`. 226 227The move of using native secure crypto module will be shifted to a new `4.x.x` version. As it is a breaking change the impact is too big for a minor release. 228 229### 3.2.1 230 231The usage of the native crypto module has been fixed. The import and access of the native crypto module has been improved. 232 233### 3.2.0 234 235In this version `Math.random()` has been replaced by the random methods of the native crypto module. 236 237For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before. 238 239If it's absolute required to run CryptoJS in such an environment, stay with `3.1.x` version. Encrypting and decrypting stays compatible. But keep in mind `3.1.x` versions still use `Math.random()` which is cryptographically not secure, as it's not random enough. 240 241This version came along with `CRITICAL` `BUG`. 242 243DO NOT USE THIS VERSION! Please, go for a newer version! 244 245### 3.1.x 246 247The `3.1.x` are based on the original CryptoJS, wrapped in CommonJS modules. 248 249 250