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