UNPKG

8.23 kBMarkdownView Raw
1# jsSHA
2
3A pure TypeScript/JavaScript streaming implementation of the complete Secure
4Hash Standard (SHA) family (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512,
5SHAKE128/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
14More complete documentation can be found on the
15[jsSHA Wiki](https://github.com/Caligatio/jsSHA/wiki) but below are common
16use-cases.
17
18### Installation
19
20#### Browser
21
22Include the desired JavaScript file (sha.js, sha1.js, sha256.js, sha512.js, or
23sha3.js) in your header:
24
25```html
26<script type="text/javascript" src="/path/to/sha.js"></script>
27```
28
29#### Node.js
30
31jsSHA is available through NPM and be installed by simply doing
32
33```console
34npm install jssha
35```
36
37To use the module, first require it using:
38
39```javascript
40const 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 */
44const jsSHA1 = require("jssha/sha1");
45/* For Node.js versions that don't support subpath exports, you can do the
46 * following instead: */
47const jsSHA1 = require("jssha/dist/sha1");
48/* Alternatively, you can load it as an ESM (Node.js v13+ or using
49 * --experimental-modules in v12) */
50import jsSHA from "jssha";
51```
52
53### Hashing
54
55Instantiate a new `jsSHA` object with the desired hash variant, input format,
56and options as parameters. The hash variant can be one of SHA-1, SHA-224,
57SHA3-224, SHA-256, SHA3-256, SHA-384, SHA3-384, SHA-512, SHA3-512, SHAKE128, or
58SHAKE256. The input format can be one of HEX, TEXT, B64, BYTES, ARRAYBUFFER, or
59UINT8ARRAY. You can then stream in input using the `update` object function,
60calling it multiple times if needed. Finally, simply call `getHash` with the
61output type as a parameter (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY).
62Example to calculate the SHA-512 of "This is a test":
63
64```javascript
65const shaObj = new jsSHA("SHA-512", "TEXT", { encoding: "UTF8" });
66shaObj.update("This is a ");
67shaObj.update("test");
68const hash = shaObj.getHash("HEX");
69```
70
71The constructor takes a hashmap as a optional third argument with defaults
72`{"encoding" : "UTF8", "numRounds" : 1}`. `numRounds` controls the number of
73hashing iterations/rounds performed and `encoding` specifies the encoding used
74to 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"
79outputs and `b64Pad` only for "B64" outputs.
80
81**_Important_**: SHAKE128 and SHAKE256 require `outputLen` to be in the hashmap
82where `outputLen` is the desired output length of the SHAKE algorithm in a
83multiple of 8 bits.
84
85### HMAC
86
87Instantiate a new `jsSHA` object similiar to hashing but with the third argument
88in the form of `{ "hmacKey": { "value": VALUE, "format": FORMAT } }`. FORMAT
89takes the same values as the input format from hashing and the VALUE is then
90either a `string`, `ArrayBuffer`, or `Uint8Array`. You can stream in the input
91using the `update` object function just like hashing. Finally, get the HMAC by
92calling the `getHash` function with the output type as its argument. Example to
93calculate the SHA-512 HMAC of the string "This is a test" with the key "abc":
94
95```javascript
96const shaObj = new jsSHA("SHA-512", "TEXT", {
97 hmacKey: { value: "abc", format: "TEXT" },
98});
99shaObj.update("This is a ");
100shaObj.update("test");
101const hmac = shaObj.getHash("HEX");
102```
103
104Note: You cannot specify `numRounds` with HMAC.
105
106### cSHAKE
107
108Instantiate a new `jsSHA` object similiar to HMAC but first argument being
109either "CSHAKE128" or "CSHAKE256" and the third argument in the form of
110`{ "customization"?: { "value": VALUE, "format": FORMAT }, "funcName"?: { "value": VALUE, "format": FORMAT } }`.
111FORMAT takes the same values as the input format from hashing and the VALUE is
112then either a `string`, `ArrayBuffer`, or `Uint8Array`. Per the NIST
113specification, both `customization` and `funcName` are optional. You can stream
114in the input using the `update` object function just like hashing. Finally, get
115the hash by calling the `getHash` function with the output type and length as
116arguments. Example to calculate the cSHAKE128 of the string "This is a test"
117with the customization string "My Tagged Application" and an output size of
118256-bits.
119
120```javascript
121const shaObj = new jsSHA("CSHAKE128", "TEXT", {
122 customization: { value: "My Tagged Application", format: "TEXT" },
123});
124shaObj.update("This is a ");
125shaObj.update("test");
126const cshake = shaObj.getHash("HEX", { outputLen: 256 });
127```
128
129Note: You cannot specify `numRounds` with cSHAKE.
130
131**_Important_**: `outputLen` is required to be in the hashmap where `outputLen`
132is the desired output length of the cSHAKE algorithm in a multiple of 8 bits.
133
134### KMAC
135
136Instantiate a new `jsSHA` object similiar to cSHAKE but first argument being
137either "KMAC128" or "KMAC256" and the third argument in the form of
138`{ "customization"?: { "value": VALUE, "format": FORMAT }, "kmacKey?: { "value": VALUE, "format": FORMAT } }`.
139FORMAT takes the same values as the input format from hashing and the VALUE is
140then either a `string`, `ArrayBuffer`, or `Uint8Array`. Per the NIST
141specification `customization` is optional whereas `kmacKey` is required. You can
142stream in the input using the `update` object function just like hashing.
143Finally, get the hash by calling the `getHash` function with the output type and
144length as arguments. Example to calculate the KMAC128 of the string "This is a
145test" with the customization string "My Tagged Application", key "abc", and an
146output size of 256-bits.
147
148```javascript
149const shaObj = new jsSHA("KMAC128", "TEXT", {
150 customization: { value: "My Tagged Application", format: "TEXT" },
151 kmacKey: { value: "abc", format: "TEXT" },
152});
153shaObj.update("This is a ");
154shaObj.update("test");
155const kmac = shaObj.getHash("HEX", { outputLen: 256 });
156```
157
158Note: You cannot specify `numRounds` with KMAC.
159
160**_Important_**: `outputLen` is required to be in the hashmap where `outputLen`
161is 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
189The project's website is located at https://caligatio.github.com/jsSHA/
190
191[umd]: https://github.com/umdjs/umd