1 | # jsSHA
|
2 |
|
3 | A pure TypeScript/JavaScript streaming implementation of the complete Secure
|
4 | Hash Standard (SHA) family (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512,
|
5 | SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC.
|
6 |
|
7 | [![npm](https://img.shields.io/npm/v/jssha)](https://www.npmjs.com/package/jssha)
|
8 | [![Build Status](https://travis-ci.org/Caligatio/jsSHA.svg?branch=master)](https://travis-ci.org/Caligatio/jsSHA)
|
9 | [![Coverage Status](https://coveralls.io/repos/github/Caligatio/jsSHA/badge.svg?branch=master)](https://coveralls.io/github/Caligatio/jsSHA?branch=master)
|
10 | [![NPM](https://img.shields.io/npm/l/jssha)](https://opensource.org/licenses/BSD-3-Clause)
|
11 |
|
12 | ## Usage
|
13 |
|
14 | More complete documentation can be found on the
|
15 | [jsSHA Wiki](https://github.com/Caligatio/jsSHA/wiki) but below are common
|
16 | use-cases.
|
17 |
|
18 | ### Installation
|
19 |
|
20 | #### Browser
|
21 |
|
22 | Include the desired JavaScript file (sha.js, sha1.js, sha256.js, sha512.js, or
|
23 | sha3.js) in your header:
|
24 |
|
25 | ```html
|
26 | <script type="text/javascript" src="/path/to/sha.js"></script>
|
27 | ```
|
28 |
|
29 | #### Node.js
|
30 |
|
31 | jsSHA is available through NPM and be installed by simply doing
|
32 |
|
33 | ```console
|
34 | npm install jssha
|
35 | ```
|
36 |
|
37 | To use the module, first require it using:
|
38 |
|
39 | ```javascript
|
40 | const jsSHA = require("jssha");
|
41 | /* The limited variant files are also exported (sha1, sha256, sha512, and sha3)
|
42 | * using conditional subpath exports in Node.js v13+ or using --experimental-modules
|
43 | * in v12 */
|
44 | const jsSHA1 = require("jssha/sha1");
|
45 | /* For Node.js versions that don't support subpath exports, you can do the
|
46 | * following instead: */
|
47 | const jsSHA1 = require("jssha/dist/sha1");
|
48 | /* Alternatively, you can load it as an ESM (Node.js v13+ or using
|
49 | * --experimental-modules in v12) */
|
50 | import jsSHA from "jssha";
|
51 | ```
|
52 |
|
53 | ### Hashing
|
54 |
|
55 | Instantiate a new `jsSHA` object with the desired hash variant, input format,
|
56 | and options as parameters. The hash variant can be one of SHA-1, SHA-224,
|
57 | SHA3-224, SHA-256, SHA3-256, SHA-384, SHA3-384, SHA-512, SHA3-512, SHAKE128, or
|
58 | SHAKE256. The input format can be one of HEX, TEXT, B64, BYTES, ARRAYBUFFER, or
|
59 | UINT8ARRAY. You can then stream in input using the `update` object function,
|
60 | calling it multiple times if needed. Finally, simply call `getHash` with the
|
61 | output type as a parameter (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY).
|
62 | Example to calculate the SHA-512 of "This is a test":
|
63 |
|
64 | ```javascript
|
65 | const shaObj = new jsSHA("SHA-512", "TEXT", { encoding: "UTF8" });
|
66 | shaObj.update("This is a ");
|
67 | shaObj.update("test");
|
68 | const hash = shaObj.getHash("HEX");
|
69 | ```
|
70 |
|
71 | The constructor takes a hashmap as a optional third argument with defaults
|
72 | `{"encoding" : "UTF8", "numRounds" : 1}`. `numRounds` controls the number of
|
73 | hashing iterations/rounds performed and `encoding` specifies the encoding used
|
74 | to encode TEXT-type inputs. Valid `encoding` values are "UTF8", "UTF16BE", and
|
75 | "UTF16LE".
|
76 |
|
77 | `getHash` also takes a hashmap as an optional second argument with defaults
|
78 | `{"outputUpper" : false, "b64Pad" : "="}`. `outputUpper` is only used for "HEX"
|
79 | outputs and `b64Pad` only for "B64" outputs.
|
80 |
|
81 | **_Important_**: SHAKE128 and SHAKE256 require `outputLen` to be in the hashmap
|
82 | where `outputLen` is the desired output length of the SHAKE algorithm in a
|
83 | multiple of 8 bits.
|
84 |
|
85 | ### HMAC
|
86 |
|
87 | Instantiate a new `jsSHA` object similiar to hashing but with the third argument
|
88 | in the form of `{ "hmacKey": { "value": VALUE, "format": FORMAT } }`. FORMAT
|
89 | takes the same values as the input format from hashing and the VALUE is then
|
90 | either a `string`, `ArrayBuffer`, or `Uint8Array`. You can stream in the input
|
91 | using the `update` object function just like hashing. Finally, get the HMAC by
|
92 | calling the `getHash` function with the output type as its argument. Example to
|
93 | calculate the SHA-512 HMAC of the string "This is a test" with the key "abc":
|
94 |
|
95 | ```javascript
|
96 | const shaObj = new jsSHA("SHA-512", "TEXT", {
|
97 | hmacKey: { value: "abc", format: "TEXT" },
|
98 | });
|
99 | shaObj.update("This is a ");
|
100 | shaObj.update("test");
|
101 | const hmac = shaObj.getHash("HEX");
|
102 | ```
|
103 |
|
104 | Note: You cannot specify `numRounds` with HMAC.
|
105 |
|
106 | ### cSHAKE
|
107 |
|
108 | Instantiate a new `jsSHA` object similiar to HMAC but first argument being
|
109 | either "CSHAKE128" or "CSHAKE256" and the third argument in the form of
|
110 | `{ "customization"?: { "value": VALUE, "format": FORMAT }, "funcName"?: { "value": VALUE, "format": FORMAT } }`.
|
111 | FORMAT takes the same values as the input format from hashing and the VALUE is
|
112 | then either a `string`, `ArrayBuffer`, or `Uint8Array`. Per the NIST
|
113 | specification, both `customization` and `funcName` are optional. You can stream
|
114 | in the input using the `update` object function just like hashing. Finally, get
|
115 | the hash by calling the `getHash` function with the output type and length as
|
116 | arguments. Example to calculate the cSHAKE128 of the string "This is a test"
|
117 | with the customization string "My Tagged Application" and an output size of
|
118 | 256-bits.
|
119 |
|
120 | ```javascript
|
121 | const shaObj = new jsSHA("CSHAKE128", "TEXT", {
|
122 | customization: { value: "My Tagged Application", format: "TEXT" },
|
123 | });
|
124 | shaObj.update("This is a ");
|
125 | shaObj.update("test");
|
126 | const cshake = shaObj.getHash("HEX", { outputLen: 256 });
|
127 | ```
|
128 |
|
129 | Note: You cannot specify `numRounds` with cSHAKE.
|
130 |
|
131 | **_Important_**: `outputLen` is required to be in the hashmap where `outputLen`
|
132 | is the desired output length of the cSHAKE algorithm in a multiple of 8 bits.
|
133 |
|
134 | ### KMAC
|
135 |
|
136 | Instantiate a new `jsSHA` object similiar to cSHAKE but first argument being
|
137 | either "KMAC128" or "KMAC256" and the third argument in the form of
|
138 | `{ "customization"?: { "value": VALUE, "format": FORMAT }, "kmacKey?: { "value": VALUE, "format": FORMAT } }`.
|
139 | FORMAT takes the same values as the input format from hashing and the VALUE is
|
140 | then either a `string`, `ArrayBuffer`, or `Uint8Array`. Per the NIST
|
141 | specification `customization` is optional whereas `kmacKey` is required. You can
|
142 | stream in the input using the `update` object function just like hashing.
|
143 | Finally, get the hash by calling the `getHash` function with the output type and
|
144 | length as arguments. Example to calculate the KMAC128 of the string "This is a
|
145 | test" with the customization string "My Tagged Application", key "abc", and an
|
146 | output size of 256-bits.
|
147 |
|
148 | ```javascript
|
149 | const shaObj = new jsSHA("KMAC128", "TEXT", {
|
150 | customization: { value: "My Tagged Application", format: "TEXT" },
|
151 | kmacKey: { value: "abc", format: "TEXT" },
|
152 | });
|
153 | shaObj.update("This is a ");
|
154 | shaObj.update("test");
|
155 | const kmac = shaObj.getHash("HEX", { outputLen: 256 });
|
156 | ```
|
157 |
|
158 | Note: You cannot specify `numRounds` with KMAC.
|
159 |
|
160 | **_Important_**: `outputLen` is required to be in the hashmap where `outputLen`
|
161 | is the desired output length of the KMAC algorithm in a multiple of 8 bits.
|
162 |
|
163 | ## Files
|
164 |
|
165 | - **dist/sha.js** - The minified ECMAScript 3 (ES3) compatible [Universal Module
|
166 | Definition (UMD)][umd] version of the library with support for all hash
|
167 | variants. Its accompanying source map can be found in dist/sha.js.map and its
|
168 | TypeScript declarations in dist/sha.d.ts.
|
169 | - **dist/sha.mjs** - The minified ECMAScript 2015 (ES6) compatible ESM version
|
170 | of the library with support for all hash variants. Its accompanying source map
|
171 | can be found in dist/sha.mjs.map and its TypeScript declarations in
|
172 | dist/sha.d.ts.
|
173 | - **dist/sha1.{js,mjs}** - The minified UMD and ESM versions of the library with
|
174 | support for only the SHA-1 hash variant. Its accompanying TypeScript
|
175 | declarations can be found in dist/sha1.d.ts.
|
176 | - **dist/sha256.{js,mjs}** - The minified UMD and ESM versions of the library
|
177 | with support for only the SHA-224 and SHA-256 hash variants. Its accompanying
|
178 | TypeScript declarations can be found in dist/sha256.d.ts.
|
179 | - **dist/sha512.{js,mjs}** - The minified UMD and ESM versions of the library
|
180 | with support for only the SHA-384 and SHA-512 hash variants. Its accompanying
|
181 | TypeScript declarations can be found in dist/sha513.d.ts.
|
182 | - **dist/sha3.{js,mjs}** - The minified UMD and ESM versions of the library with
|
183 | support for only the SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128,
|
184 | SHAKE256, cSHAKE128, cSHAKE256, KMAC128, and KMAC256 hash variants. Its
|
185 | accompanying TypeScript declarations can be found in dist/sha3.d.ts.
|
186 |
|
187 | ## Contact Info
|
188 |
|
189 | The project's website is located at https://caligatio.github.com/jsSHA/
|
190 |
|
191 | [umd]: https://github.com/umdjs/umd
|