kecak256 1.0.0
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.
- package/README.md +29 -0
- package/kecak256.js +86 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# keccak256
|
|
2
|
+
|
|
3
|
+
> Keccak library to compute 256 bit keccak hash in JavaScript.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install kecak256
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
- **keccak256**(data) -> {Buffer}
|
|
15
|
+
- {String | Buffer} data - data string or Buffer
|
|
16
|
+
|
|
17
|
+
Returns a Buffer
|
|
18
|
+
|
|
19
|
+
## Getting Started
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const keccak256 = require('kecak256')
|
|
23
|
+
|
|
24
|
+
console.log(keccak256('hello').toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
|
25
|
+
|
|
26
|
+
console.log(keccak256(Buffer.from('hello')).toString('hex')) // "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
|
package/kecak256.js
ADDED
|
@@ -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;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kecak256",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Keccak library to compute 256 bit keccak hash in JavaScript",
|
|
5
|
+
"main": "kecak256.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"kecak256.js",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Miguel Mota"
|
|
12
|
+
},
|
|
13
|
+
"license": {
|
|
14
|
+
"type": "MIT"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"bn.js": "^5.2.0",
|
|
18
|
+
"buffer": "^6.0.3",
|
|
19
|
+
"keccak": "^3.0.2"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"crypto",
|
|
23
|
+
"hash",
|
|
24
|
+
"sha3",
|
|
25
|
+
"algorithm",
|
|
26
|
+
"keccak",
|
|
27
|
+
"keccak256"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/bn.js": "^5.1.0",
|
|
31
|
+
"@types/keccak": "^3.0.1",
|
|
32
|
+
"@types/node": "^16.11.12",
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
|
34
|
+
"@typescript-eslint/parser": "^4.28.2",
|
|
35
|
+
"babel": "^6.23.0",
|
|
36
|
+
"babel-cli": "^6.26.0",
|
|
37
|
+
"babel-core": "^6.26.3",
|
|
38
|
+
"babel-preset-env": "^1.7.0",
|
|
39
|
+
"babel-preset-es2015": "^6.24.1",
|
|
40
|
+
"babelify": "^8.0.0",
|
|
41
|
+
"browserify": "^17.0.0",
|
|
42
|
+
"eslint": "^7.30.0",
|
|
43
|
+
"eslint-config-standard": "^16.0.3",
|
|
44
|
+
"eslint-config-standard-with-typescript": "^21.0.1",
|
|
45
|
+
"eslint-plugin-import": "^2.23.4",
|
|
46
|
+
"eslint-plugin-node": "^11.1.0",
|
|
47
|
+
"eslint-plugin-promise": "^5.1.0",
|
|
48
|
+
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
|
|
49
|
+
"eslint-plugin-unused-imports": "^1.1.2",
|
|
50
|
+
"rimraf": "^3.0.2",
|
|
51
|
+
"standard": "^16.0.4",
|
|
52
|
+
"tape": "^4.9.1",
|
|
53
|
+
"typescript": "^4.5.3"
|
|
54
|
+
}
|
|
55
|
+
}
|