crypto-keccak-js 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of crypto-keccak-js might be problematic. Click here for more details.
- package/.eslintrc.json +43 -0
- package/LICENSE +21 -0
- package/Makefile +21 -0
- package/README.md +52 -0
- package/dist/keccak256.d.ts +5 -0
- package/dist/keccak256.d.ts.map +1 -0
- package/dist/keccak256.js +86 -0
- package/example/example.js +5 -0
- package/funding.json +5 -0
- package/keccak256.d.ts +6 -0
- package/keccak256.js +10362 -0
- package/package.json +76 -0
- package/script/postinstall.js +1 -0
- package/src/keccak256.ts +93 -0
- package/test/keccak256.js +9 -0
- package/tsconfig.json +26 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es2021": true
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"standard-with-typescript"
|
|
8
|
+
],
|
|
9
|
+
"parser": "@typescript-eslint/parser",
|
|
10
|
+
"parserOptions": {
|
|
11
|
+
"ecmaVersion": 12,
|
|
12
|
+
"sourceType": "module",
|
|
13
|
+
"project": "./tsconfig.json"
|
|
14
|
+
},
|
|
15
|
+
"plugins": [
|
|
16
|
+
"@typescript-eslint",
|
|
17
|
+
"unused-imports",
|
|
18
|
+
"sort-imports-es6-autofix"
|
|
19
|
+
],
|
|
20
|
+
"rules": {
|
|
21
|
+
"no-unused-vars": 0,
|
|
22
|
+
"@typescript-eslint/no-unused-vars": 0,
|
|
23
|
+
"unused-imports/no-unused-imports": 1,
|
|
24
|
+
"new-cap": 0,
|
|
25
|
+
"prefer-const": 1,
|
|
26
|
+
"no-async-promise-executor": 0,
|
|
27
|
+
"node/no-callback-literal": 0,
|
|
28
|
+
"sort-imports-es6-autofix/sort-imports-es6": [1, {
|
|
29
|
+
"ignoreCase": false,
|
|
30
|
+
"ignoreMemberSort": false,
|
|
31
|
+
"memberSyntaxSortOrder": ["none", "single", "multiple", "all"]
|
|
32
|
+
}],
|
|
33
|
+
"@typescript-eslint/strict-boolean-expressions": 0,
|
|
34
|
+
"@typescript-eslint/restrict-template-expressions": 0,
|
|
35
|
+
"@typescript-eslint/explicit-function-return-type": 0,
|
|
36
|
+
"@typescript-eslint/no-floating-promises": 0,
|
|
37
|
+
"@typescript-eslint/no-misused-promises": 0,
|
|
38
|
+
"@typescript-eslint/no-dynamic-delete": 0,
|
|
39
|
+
"@typescript-eslint/return-await": 0,
|
|
40
|
+
"@typescript-eslint/consistent-type-definitions": 0,
|
|
41
|
+
"@typescript-eslint/method-signature-style": 0
|
|
42
|
+
}
|
|
43
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT license
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2015 Miguel Mota
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Makefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
all: build
|
|
2
|
+
|
|
3
|
+
.PHONY: build
|
|
4
|
+
build:
|
|
5
|
+
@npm run build
|
|
6
|
+
|
|
7
|
+
.PHONY: test
|
|
8
|
+
test:
|
|
9
|
+
@npm test
|
|
10
|
+
|
|
11
|
+
.PHONY: lint
|
|
12
|
+
lint:
|
|
13
|
+
@standard index.js
|
|
14
|
+
@standard test/index.js
|
|
15
|
+
@standard example/example.js
|
|
16
|
+
|
|
17
|
+
.PHONY: lint-fix
|
|
18
|
+
lint-fix:
|
|
19
|
+
@standard --fix index.js
|
|
20
|
+
@standard --fix test/index.js
|
|
21
|
+
@standard --fix example/example.js
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# keccak256
|
|
2
|
+
|
|
3
|
+
> A wrapper for the [`keccak`](https://www.npmjs.com/package/keccak) library to compute 256 bit keccak hash in JavaScript.
|
|
4
|
+
|
|
5
|
+
[](https://raw.githubusercontent.com/miguelmota/keccak256/master/LICENSE.md)
|
|
6
|
+
|
|
7
|
+
[](https://github.com/feross/standard)
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install keccak256
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### CDN
|
|
16
|
+
|
|
17
|
+
Available on [jsDelivr](https://www.jsdelivr.com/) CDN:
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<script src="https://cdn.jsdelivr.net/npm/keccak256@latest/keccak256.js"></script>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
- **keccak256**(data) -> {Buffer}
|
|
26
|
+
- {String | Buffer} data - data string or Buffer
|
|
27
|
+
|
|
28
|
+
Returns a Buffer
|
|
29
|
+
|
|
30
|
+
## Getting Started
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const keccak256 = require('keccak256')
|
|
34
|
+
|
|
35
|
+
console.log(keccak256('hello').toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
|
36
|
+
|
|
37
|
+
console.log(keccak256(Buffer.from('hello')).toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Test
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm test
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
Released under the [MIT](./LICENSE) license.
|
|
50
|
+
|
|
51
|
+
© [Miguel Mota](https://github.com/miguelmota)
|
|
52
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keccak256.d.ts","sourceRoot":"","sources":["../src/keccak256.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAA;AACtB,OAAO,MAAM,MAAM,QAAQ,CAAA;AAK3B,iBAAS,SAAS,CAAE,KAAK,EAAE,MAAM,GAAG,EAAE,GAAG,MAAM,GAAG,MAAM,iBAGvD;AAmFD,SAAS,SAAS,CAAA"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
6
|
+
const buffer_1 = __importDefault(require("buffer"));
|
|
7
|
+
const keccak_1 = __importDefault(require("keccak"));
|
|
8
|
+
const Buffer = buffer_1.default.Buffer;
|
|
9
|
+
function keccak256(value) {
|
|
10
|
+
value = toBuffer(value);
|
|
11
|
+
return (0, keccak_1.default)('keccak256').update(value).digest();
|
|
12
|
+
}
|
|
13
|
+
function toBuffer(value) {
|
|
14
|
+
if (!Buffer.isBuffer(value)) {
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
value = Buffer.from(value);
|
|
17
|
+
}
|
|
18
|
+
else if (typeof value === 'string') {
|
|
19
|
+
if (isHexString(value)) {
|
|
20
|
+
value = Buffer.from(padToEven(stripHexPrefix(value)), 'hex');
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
value = Buffer.from(value);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (typeof value === 'number') {
|
|
27
|
+
value = intToBuffer(value);
|
|
28
|
+
}
|
|
29
|
+
else if (value === null || value === undefined) {
|
|
30
|
+
value = Buffer.allocUnsafe(0);
|
|
31
|
+
}
|
|
32
|
+
else if (bn_js_1.default.isBN(value)) {
|
|
33
|
+
value = value.toArrayLike(Buffer);
|
|
34
|
+
}
|
|
35
|
+
else if (value.toArray) {
|
|
36
|
+
// converts a BN to a Buffer
|
|
37
|
+
value = Buffer.from(value.toArray());
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
throw new Error('invalid type');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
function isHexString(value, length) {
|
|
46
|
+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
function padToEven(value) {
|
|
55
|
+
if (typeof value !== 'string') {
|
|
56
|
+
throw new Error(`while padding to even, value must be string, is currently ${typeof value}, while padToEven.`);
|
|
57
|
+
}
|
|
58
|
+
if (value.length % 2) {
|
|
59
|
+
value = `0${value}`;
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
function stripHexPrefix(value) {
|
|
64
|
+
if (typeof value !== 'string') {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
return isHexPrefixed(value) ? value.slice(2) : value;
|
|
68
|
+
}
|
|
69
|
+
function isHexPrefixed(value) {
|
|
70
|
+
if (typeof value !== 'string') {
|
|
71
|
+
throw new Error("value must be type 'string', is currently type " + (typeof value) + ', while checking isHexPrefixed.');
|
|
72
|
+
}
|
|
73
|
+
return value.slice(0, 2) === '0x';
|
|
74
|
+
}
|
|
75
|
+
function intToBuffer(i) {
|
|
76
|
+
const hex = intToHex(i);
|
|
77
|
+
return Buffer.from(padToEven(hex.slice(2)), 'hex');
|
|
78
|
+
}
|
|
79
|
+
function intToHex(i) {
|
|
80
|
+
const hex = i.toString(16);
|
|
81
|
+
return `0x${hex}`;
|
|
82
|
+
}
|
|
83
|
+
if (typeof window !== 'undefined') {
|
|
84
|
+
window.keccak256 = keccak256;
|
|
85
|
+
}
|
|
86
|
+
module.exports = keccak256;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const keccak256 = require('keccak256')
|
|
2
|
+
|
|
3
|
+
console.log(keccak256('hello').toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
|
4
|
+
|
|
5
|
+
console.log(keccak256(Buffer.from('hello')).toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
package/funding.json
ADDED