1 | # js-sha3
|
2 |
|
3 | [![Build Status](https://travis-ci.org/emn178/js-sha3.svg?branch=master)](https://travis-ci.org/emn178/js-sha3)
|
4 | [![Coverage Status](https://coveralls.io/repos/emn178/js-sha3/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha3?branch=master)
|
5 |
|
6 | A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
|
7 |
|
8 | ## Notice
|
9 | Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
|
10 |
|
11 | ## Demo
|
12 | [SHA3-512 Online](http://emn178.github.io/online-tools/sha3_512.html)
|
13 | [SHA3-384 Online](http://emn178.github.io/online-tools/sha3_384.html)
|
14 | [SHA3-256 Online](http://emn178.github.io/online-tools/sha3_256.html)
|
15 | [SHA3-224 Online](http://emn178.github.io/online-tools/sha3_224.html)
|
16 | [Keccak-512 Online](http://emn178.github.io/online-tools/keccak_512.html)
|
17 | [Keccak-384 Online](http://emn178.github.io/online-tools/keccak_384.html)
|
18 | [Keccak-256 Online](http://emn178.github.io/online-tools/keccak_256.html)
|
19 | [Keccak-224 Online](http://emn178.github.io/online-tools/keccak_224.html)
|
20 |
|
21 | ## Download
|
22 | [Compress](https://raw.github.com/emn178/js-sha3/master/build/sha3.min.js)
|
23 | [Uncompress](https://raw.github.com/emn178/js-sha3/master/src/sha3.js)
|
24 |
|
25 | ## Installation
|
26 | You can also install js-sha3 by using Bower.
|
27 |
|
28 | bower install js-sha3
|
29 |
|
30 | For node.js, you can use this command to install:
|
31 |
|
32 | npm install js-sha3
|
33 |
|
34 | ## Usage
|
35 | You could use like this:
|
36 | ```JavaScript
|
37 | sha3_512('Message to hash');
|
38 | sha3_384('Message to hash');
|
39 | sha3_256('Message to hash');
|
40 | sha3_224('Message to hash');
|
41 | keccak_512('Message to hash');
|
42 | keccak_384('Message to hash');
|
43 | keccak_256('Message to hash');
|
44 | keccak_224('Message to hash');
|
45 | shake_128('Message to hash', 256);
|
46 | shake_256('Message to hash', 512);
|
47 |
|
48 | // Support ArrayBuffer output
|
49 | var buffer = keccak_224.buffer('Message to hash');
|
50 |
|
51 | // Support Array output
|
52 | var buffer = keccak_224.array('Message to hash');
|
53 |
|
54 | // update hash
|
55 | sha3_512.update('Message ').update('to ').update('hash').hex();
|
56 | // specify shake output bits at first update
|
57 | shake_128.update('Message ', 256).update('to ').update('hash').hex();
|
58 |
|
59 | // or to use create
|
60 | var hash = sha3_512.create();
|
61 | hash.update('...');
|
62 | hash.update('...');
|
63 | hash.hex();
|
64 |
|
65 | // specify shake output bits when creating
|
66 | var hash = shake_128.create(256);
|
67 | hash.update('...');
|
68 | hash.update('...');
|
69 | hash.hex();
|
70 | ```
|
71 | If you use node.js, you should require the module first:
|
72 | ```JavaScript
|
73 | sha3_512 = require('js-sha3').sha3_512;
|
74 | sha3_384 = require('js-sha3').sha3_384;
|
75 | sha3_256 = require('js-sha3').sha3_256;
|
76 | sha3_224 = require('js-sha3').sha3_224;
|
77 | keccak_512 = require('js-sha3').keccak_512;
|
78 | keccak_384 = require('js-sha3').keccak_384;
|
79 | keccak_256 = require('js-sha3').keccak_256;
|
80 | keccak_224 = require('js-sha3').keccak_224;
|
81 | shake_128 = require('js-sha3').shake_128;
|
82 | shake_256 = require('js-sha3').shake_256;
|
83 | ```
|
84 |
|
85 | ## Example
|
86 | Code
|
87 | ```JavaScript
|
88 | sha3_512('');
|
89 | // a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26
|
90 |
|
91 | sha3_512('The quick brown fox jumps over the lazy dog');
|
92 | // 01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450
|
93 |
|
94 | sha3_512('The quick brown fox jumps over the lazy dog.');
|
95 | // 18f4f4bd419603f95538837003d9d254c26c23765565162247483f65c50303597bc9ce4d289f21d1c2f1f458828e33dc442100331b35e7eb031b5d38ba6460f8
|
96 |
|
97 | sha3_384('');
|
98 | // 0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004
|
99 |
|
100 | sha3_384('The quick brown fox jumps over the lazy dog');
|
101 | // 7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41
|
102 |
|
103 | sha3_384('The quick brown fox jumps over the lazy dog.');
|
104 | // 1a34d81695b622df178bc74df7124fe12fac0f64ba5250b78b99c1273d4b080168e10652894ecad5f1f4d5b965437fb9
|
105 |
|
106 | sha3_256('');
|
107 | // a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
|
108 |
|
109 | sha3_256('The quick brown fox jumps over the lazy dog');
|
110 | // 69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04
|
111 |
|
112 | sha3_256('The quick brown fox jumps over the lazy dog.');
|
113 | // a80f839cd4f83f6c3dafc87feae470045e4eb0d366397d5c6ce34ba1739f734d
|
114 |
|
115 | sha3_224('');
|
116 | // 6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7
|
117 |
|
118 | sha3_224('The quick brown fox jumps over the lazy dog');
|
119 | // d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795
|
120 |
|
121 | sha3_224('The quick brown fox jumps over the lazy dog.');
|
122 | // 2d0708903833afabdd232a20201176e8b58c5be8a6fe74265ac54db0
|
123 |
|
124 | keccak_512('');
|
125 | // 0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e
|
126 |
|
127 | keccak_512('The quick brown fox jumps over the lazy dog');
|
128 | // d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609
|
129 |
|
130 | keccak_512('The quick brown fox jumps over the lazy dog.');
|
131 | // ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760
|
132 |
|
133 | keccak_384('');
|
134 | // 2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff
|
135 |
|
136 | keccak_384('The quick brown fox jumps over the lazy dog');
|
137 | // 283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3
|
138 |
|
139 | keccak_384('The quick brown fox jumps over the lazy dog.');
|
140 | // 9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b
|
141 |
|
142 | keccak_256('');
|
143 | // c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
|
144 |
|
145 | keccak_256('The quick brown fox jumps over the lazy dog');
|
146 | // 4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15
|
147 |
|
148 | keccak_256('The quick brown fox jumps over the lazy dog.');
|
149 | // 578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d
|
150 |
|
151 | keccak_224('');
|
152 | // f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd
|
153 |
|
154 | keccak_224('The quick brown fox jumps over the lazy dog');
|
155 | // 310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe
|
156 |
|
157 | keccak_224('The quick brown fox jumps over the lazy dog.');
|
158 | // c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab
|
159 |
|
160 | shake_128('', 256);
|
161 | // 7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26
|
162 |
|
163 | shake_256('', 512);
|
164 | // 46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be
|
165 | ```
|
166 | It also supports UTF-8 encoding:
|
167 |
|
168 | Code
|
169 | ```JavaScript
|
170 | sha3_512('中文');
|
171 | // 059bbe2efc50cc30e4d8ec5a96be697e2108fcbf9193e1296192eddabc13b143c0120d059399a13d0d42651efe23a6c1ce2d1efb576c5b207fa2516050505af7
|
172 |
|
173 | sha3_384('中文');
|
174 | // 9fb5b99e3c546f2738dcd50a14e9aef9c313800c1bf8cf76bc9b2c3a23307841364c5a2d0794702662c5796fb72f5432
|
175 |
|
176 | sha3_256('中文');
|
177 | // ac5305da3d18be1aed44aa7c70ea548da243a59a5fd546f489348fd5718fb1a0
|
178 |
|
179 | sha3_224('中文');
|
180 | // 106d169e10b61c2a2a05554d3e631ec94467f8316640f29545d163ee
|
181 |
|
182 | keccak_512('中文');
|
183 | // 2f6a1bd50562230229af34b0ccf46b8754b89d23ae2c5bf7840b4acfcef86f87395edc0a00b2bfef53bafebe3b79de2e3e01cbd8169ddbb08bde888dcc893524
|
184 |
|
185 | keccak_384('中文');
|
186 | // 743f64bb7544c6ed923be4741b738dde18b7cee384a3a09c4e01acaaac9f19222cdee137702bd3aa05dc198373d87d6c
|
187 |
|
188 | keccak_256('中文');
|
189 | // 70a2b6579047f0a977fcb5e9120a4e07067bea9abb6916fbc2d13ffb9a4e4eee
|
190 |
|
191 | keccak_224('中文');
|
192 | // f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd
|
193 | ```
|
194 |
|
195 | It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` input:
|
196 |
|
197 | Code
|
198 | ```JavaScript
|
199 | sha3_512([]);
|
200 | // a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26
|
201 |
|
202 | sha3_512(new Uint8Array([]));
|
203 | // a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26
|
204 |
|
205 | // ...
|
206 | ```
|
207 |
|
208 | ## Benchmark
|
209 | [UTF8](http://jsperf.com/sha3/5)
|
210 | [ASCII](http://jsperf.com/sha3/4)
|
211 |
|
212 | ## Extensions
|
213 | ### jQuery
|
214 | If you prefer jQuery style, you can add following code to add a jQuery extension.
|
215 |
|
216 | Code
|
217 | ```JavaScript
|
218 | jQuery.sha3_512 = sha3_512;
|
219 | jQuery.sha3_384 = sha3_384;
|
220 | jQuery.sha3_256 = sha3_256;
|
221 | jQuery.sha3_224 = sha3_224;
|
222 | jQuery.keccak_512 = keccak_512;
|
223 | jQuery.keccak_384 = keccak_384;
|
224 | jQuery.keccak_256 = keccak_256;
|
225 | jQuery.keccak_224 = keccak_224;
|
226 | ```
|
227 | And then you could use like this:
|
228 | ```JavaScript
|
229 | $.sha3_512('message');
|
230 | $.sha3_384('message');
|
231 | $.sha3_256('message');
|
232 | $.sha3_224('message');
|
233 | $.keccak_512('message');
|
234 | $.keccak_384('message');
|
235 | $.keccak_256('message');
|
236 | $.keccak_224('message');
|
237 | ```
|
238 | ### Prototype
|
239 | If you prefer prototype style, you can add following code to add a prototype extension.
|
240 |
|
241 | Code
|
242 | ```JavaScript
|
243 | String.prototype.sha3_512 = function() {
|
244 | return sha3_512(this);
|
245 | };
|
246 | String.prototype.sha3_384 = function() {
|
247 | return sha3_384(this);
|
248 | };
|
249 | String.prototype.sha3_256 = function() {
|
250 | return sha3_256(this);
|
251 | };
|
252 | String.prototype.sha3_224 = function() {
|
253 | return sha3_224(this);
|
254 | };
|
255 | String.prototype.keccak_512 = function() {
|
256 | return keccak_512(this);
|
257 | };
|
258 | String.prototype.keccak_384 = function() {
|
259 | return keccak_384(this);
|
260 | };
|
261 | String.prototype.keccak_256 = function() {
|
262 | return keccak_256(this);
|
263 | };
|
264 | String.prototype.keccak_224 = function() {
|
265 | return keccak_224(this);
|
266 | };
|
267 | ```
|
268 | And then you could use like this:
|
269 | ```JavaScript
|
270 | 'message'.sha3_512();
|
271 | 'message'.sha3_384();
|
272 | 'message'.sha3_256();
|
273 | 'message'.sha3_224();
|
274 | 'message'.keccak_512();
|
275 | 'message'.keccak_384();
|
276 | 'message'.keccak_256();
|
277 | 'message'.keccak_224();
|
278 | ```
|
279 | ## License
|
280 | The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
|
281 |
|
282 | ## Contact
|
283 | The project's website is located at https://github.com/emn178/js-sha3
|
284 | Author: emn178@gmail.com
|