UNPKG

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