b0164ad3d310929e43d800a28a222683dcfea5c0f8ef131b7159a5364c042102e5d3212c3006fff5f9c6408fdabe87c5f5a72f2a4501bbec8717176f8968f7 6.3 KB

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