1 | # crypto-js
|
2 |
|
3 | JavaScript library of crypto standards.
|
4 |
|
5 | ## Discontinued
|
6 |
|
7 | Active development of CryptoJS has been discontinued. This library is no longer maintained.
|
8 |
|
9 | 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.
|
10 |
|
11 | ## Node.js (Install)
|
12 |
|
13 | Requirements:
|
14 |
|
15 | - Node.js
|
16 | - npm (Node.js package manager)
|
17 |
|
18 | ```bash
|
19 | npm install crypto-js
|
20 | ```
|
21 |
|
22 | ### Usage
|
23 |
|
24 | ES6 import for typical API call signing use case:
|
25 |
|
26 | ```javascript
|
27 | import sha256 from 'crypto-js/sha256';
|
28 | import hmacSHA512 from 'crypto-js/hmac-sha512';
|
29 | import Base64 from 'crypto-js/enc-base64';
|
30 |
|
31 | const message, nonce, path, privateKey; // ...
|
32 | const hashDigest = sha256(nonce + message);
|
33 | const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));
|
34 | ```
|
35 |
|
36 | Modular include:
|
37 |
|
38 | ```javascript
|
39 | var AES = require("crypto-js/aes");
|
40 | var SHA256 = require("crypto-js/sha256");
|
41 | ...
|
42 | console.log(SHA256("Message"));
|
43 | ```
|
44 |
|
45 | Including all libraries, for access to extra methods:
|
46 |
|
47 | ```javascript
|
48 | var CryptoJS = require("crypto-js");
|
49 | console.log(CryptoJS.HmacSHA1("Message", "Key"));
|
50 | ```
|
51 |
|
52 | ## Client (browser)
|
53 |
|
54 | Requirements:
|
55 |
|
56 | - Node.js
|
57 | - Bower (package manager for frontend)
|
58 |
|
59 | ```bash
|
60 | bower install crypto-js
|
61 | ```
|
62 |
|
63 | ### Usage
|
64 |
|
65 | Modular include:
|
66 |
|
67 | ```javascript
|
68 | require.config({
|
69 | packages: [
|
70 | {
|
71 | name: 'crypto-js',
|
72 | location: 'path-to/bower_components/crypto-js',
|
73 | main: 'index'
|
74 | }
|
75 | ]
|
76 | });
|
77 |
|
78 | require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) {
|
79 | console.log(SHA256("Message"));
|
80 | });
|
81 | ```
|
82 |
|
83 | Including all libraries, for access to extra methods:
|
84 |
|
85 | ```javascript
|
86 | // Above-mentioned will work or use this simple form
|
87 | require.config({
|
88 | paths: {
|
89 | 'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'
|
90 | }
|
91 | });
|
92 |
|
93 | require(["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 |
|
110 | See: https://cryptojs.gitbook.io/docs/
|
111 |
|
112 | ### AES Encryption
|
113 |
|
114 | #### Plain text encryption
|
115 |
|
116 | ```javascript
|
117 | var CryptoJS = require("crypto-js");
|
118 |
|
119 | // Encrypt
|
120 | var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString();
|
121 |
|
122 | // Decrypt
|
123 | var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
|
124 | var originalText = bytes.toString(CryptoJS.enc.Utf8);
|
125 |
|
126 | console.log(originalText); // 'my message'
|
127 | ```
|
128 |
|
129 | #### Object encryption
|
130 |
|
131 | ```javascript
|
132 | var CryptoJS = require("crypto-js");
|
133 |
|
134 | var data = [{id: 1}, {id: 2}]
|
135 |
|
136 | // Encrypt
|
137 | var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();
|
138 |
|
139 | // Decrypt
|
140 | var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
|
141 | var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
|
142 |
|
143 | console.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 |
|
223 | Change default hash algorithm and iteration's for PBKDF2 to prevent weak security by using the default configuration.
|
224 |
|
225 | Custom KDF Hasher
|
226 |
|
227 | Blowfish support
|
228 |
|
229 | ### 4.1.1
|
230 |
|
231 | Fix module order in bundled release.
|
232 |
|
233 | Include the browser field in the released package.json.
|
234 |
|
235 | ### 4.1.0
|
236 |
|
237 | Added url safe variant of base64 encoding. [357](https://github.com/brix/crypto-js/pull/357)
|
238 |
|
239 | Avoid webpack to add crypto-browser package. [364](https://github.com/brix/crypto-js/pull/364)
|
240 |
|
241 | ### 4.0.0
|
242 |
|
243 | This is an update including breaking changes for some environments.
|
244 |
|
245 | In this version `Math.random()` has been replaced by the random methods of the native crypto module.
|
246 |
|
247 | For 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 |
|
251 | Rollback, `3.3.0` is the same as `3.1.9-1`.
|
252 |
|
253 | The 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 |
|
257 | The 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 |
|
261 | In this version `Math.random()` has been replaced by the random methods of the native crypto module.
|
262 |
|
263 | For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before.
|
264 |
|
265 | If 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 |
|
267 | This version came along with `CRITICAL` `BUG`.
|
268 |
|
269 | DO NOT USE THIS VERSION! Please, go for a newer version!
|
270 |
|
271 | ### 3.1.x
|
272 |
|
273 | The `3.1.x` are based on the original CryptoJS, wrapped in CommonJS modules.
|
274 |
|
275 |
|
276 |
|
\ | No newline at end of file |