You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.7 KiB

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