@ocap/mcrypto 1.16.16 → 1.16.17
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/lib/crypter/aes.d.ts +8 -0
- package/lib/crypter/aes.js +51 -28
- package/lib/encode.d.ts +2 -2
- package/lib/encode.js +22 -25
- package/lib/hasher/keccak.d.ts +13 -0
- package/lib/hasher/keccak.js +36 -78
- package/lib/hasher/sha2.d.ts +13 -0
- package/lib/hasher/sha2.js +40 -82
- package/lib/hasher/sha3.d.ts +13 -0
- package/lib/hasher/sha3.js +36 -78
- package/lib/index.d.ts +203 -3
- package/lib/index.js +171 -201
- package/lib/protocols/crypter.d.ts +2 -0
- package/lib/protocols/crypter.js +9 -3
- package/lib/protocols/hasher.d.ts +2 -0
- package/lib/protocols/hasher.js +9 -3
- package/lib/protocols/signer.d.ts +2 -0
- package/lib/protocols/signer.js +9 -3
- package/lib/signer/ed25519.d.ts +53 -0
- package/lib/signer/ed25519.js +81 -86
- package/lib/signer/ethereum.d.ts +17 -0
- package/lib/signer/ethereum.js +35 -34
- package/lib/signer/secp256k1.d.ts +39 -0
- package/lib/signer/secp256k1.js +82 -107
- package/package.json +23 -16
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Crypter from '../protocols/crypter';
|
|
2
|
+
declare class AesCrypter extends Crypter {
|
|
3
|
+
encrypt(message: string | object, secret: string): string;
|
|
4
|
+
decrypt(cipher: string, secret: string, outputEncoding?: string): string;
|
|
5
|
+
}
|
|
6
|
+
declare const _default: AesCrypter;
|
|
7
|
+
export default _default;
|
|
8
|
+
export { AesCrypter };
|
package/lib/crypter/aes.js
CHANGED
|
@@ -1,32 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.AesCrypter = void 0;
|
|
30
|
+
const AES = __importStar(require("crypto-js/aes"));
|
|
31
|
+
const enc_latin1_1 = __importDefault(require("crypto-js/enc-latin1"));
|
|
32
|
+
const enc_utf8_1 = __importDefault(require("crypto-js/enc-utf8"));
|
|
33
|
+
const enc_utf16_1 = __importDefault(require("crypto-js/enc-utf16"));
|
|
34
|
+
const enc_base64_1 = __importDefault(require("crypto-js/enc-base64"));
|
|
35
|
+
const enc_hex_1 = __importDefault(require("crypto-js/enc-hex"));
|
|
36
|
+
const crypter_1 = __importDefault(require("../protocols/crypter"));
|
|
6
37
|
const encoders = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
38
|
+
latin1: enc_latin1_1.default,
|
|
39
|
+
utf8: enc_utf8_1.default,
|
|
40
|
+
hex: enc_hex_1.default,
|
|
41
|
+
utf16: enc_utf16_1.default,
|
|
42
|
+
base64: enc_base64_1.default,
|
|
12
43
|
};
|
|
13
|
-
|
|
14
|
-
const Crypter = require('../protocols/crypter');
|
|
15
|
-
|
|
16
44
|
// AES-CBC-256
|
|
17
|
-
class AesCrypter extends
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
decrypt(cipher, secret, outputEncoding = 'utf8') {
|
|
28
|
-
return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
|
|
29
|
-
}
|
|
45
|
+
class AesCrypter extends crypter_1.default {
|
|
46
|
+
encrypt(message, secret) {
|
|
47
|
+
const text = typeof message === 'string' ? message : JSON.stringify(message);
|
|
48
|
+
return AES.encrypt(text, secret).toString();
|
|
49
|
+
}
|
|
50
|
+
decrypt(cipher, secret, outputEncoding = 'utf8') {
|
|
51
|
+
return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
|
|
52
|
+
}
|
|
30
53
|
}
|
|
31
|
-
|
|
32
|
-
|
|
54
|
+
exports.AesCrypter = AesCrypter;
|
|
55
|
+
exports.default = new AesCrypter();
|
package/lib/encode.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
export default function encode(data: BytesType, encoding?: EncodingType): BytesType;
|
package/lib/encode.js
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return data;
|
|
25
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util_1 = require("@ocap/util");
|
|
4
|
+
function encode(data, encoding = 'hex') {
|
|
5
|
+
if (['hex', 'base16'].includes(encoding)) {
|
|
6
|
+
return (0, util_1.toHex)(data);
|
|
7
|
+
}
|
|
8
|
+
if (encoding === 'base58') {
|
|
9
|
+
return (0, util_1.toBase58)(data);
|
|
10
|
+
}
|
|
11
|
+
if (encoding === 'base64') {
|
|
12
|
+
return (0, util_1.toBase64)(data);
|
|
13
|
+
}
|
|
14
|
+
if (encoding === 'Uint8Array') {
|
|
15
|
+
return (0, util_1.toUint8Array)(data);
|
|
16
|
+
}
|
|
17
|
+
if (encoding === 'buffer') {
|
|
18
|
+
return (0, util_1.toBuffer)(data);
|
|
19
|
+
}
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
exports.default = encode;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Keccak support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class KeccakHasher
|
|
6
|
+
*/
|
|
7
|
+
declare class KeccakHasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: KeccakHasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { KeccakHasher };
|
package/lib/hasher/keccak.js
CHANGED
|
@@ -1,85 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.KeccakHasher = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
|
+
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
9
|
+
const util_1 = require("@ocap/util");
|
|
10
|
+
const encode_1 = __importDefault(require("../encode"));
|
|
5
11
|
/**
|
|
6
12
|
* Keccak support with different hash length
|
|
7
13
|
*
|
|
8
14
|
* @class KeccakHasher
|
|
9
15
|
*/
|
|
10
16
|
class KeccakHasher {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
constructor() {
|
|
18
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
19
|
+
const name = `hash${x}`;
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
const hasher = (data) => js_sha3_1.default[`keccak${x}`](data);
|
|
22
|
+
const hashFn = (input, round) => {
|
|
23
|
+
if (round === 1) {
|
|
24
|
+
return hasher(input);
|
|
25
|
+
}
|
|
26
|
+
return hashFn(hasher(input), round - 1);
|
|
27
|
+
};
|
|
28
|
+
this[name] = (data, round = 1, encoding = 'hex') => {
|
|
29
|
+
let input = data;
|
|
30
|
+
try {
|
|
31
|
+
input = (0, util_1.toUint8Array)(data);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
// Do nothing
|
|
35
|
+
}
|
|
36
|
+
const res = hashFn(input, round);
|
|
37
|
+
return (0, encode_1.default)(`0x${res}`, encoding);
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* Do hash with length of 224
|
|
39
|
-
*
|
|
40
|
-
* @function
|
|
41
|
-
* @name KeccakHasher#hash224
|
|
42
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
43
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
44
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
45
|
-
* @returns {string} depends on encoding param, hex by default
|
|
46
|
-
* @memberof KeccakHasher
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Do hash with length of 256
|
|
51
|
-
*
|
|
52
|
-
* @function
|
|
53
|
-
* @name KeccakHasher#hash256
|
|
54
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
55
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
56
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
57
|
-
* @returns {string} depends on encoding param, hex by default
|
|
58
|
-
* @memberof KeccakHasher
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Do hash with length of 384
|
|
63
|
-
*
|
|
64
|
-
* @function
|
|
65
|
-
* @name KeccakHasher#hash384
|
|
66
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
67
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
68
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
69
|
-
* @returns {string} depends on encoding param, hex by default
|
|
70
|
-
* @memberof KeccakHasher
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Do hash with length of 512
|
|
75
|
-
*
|
|
76
|
-
* @function
|
|
77
|
-
* @name KeccakHasher#hash512
|
|
78
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
79
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
80
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
81
|
-
* @returns {string} depends on encoding param, hex by default
|
|
82
|
-
* @memberof KeccakHasher
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
module.exports = new KeccakHasher();
|
|
42
|
+
exports.KeccakHasher = KeccakHasher;
|
|
43
|
+
exports.default = new KeccakHasher();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Sha2 support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
declare class Sha2Hasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: Sha2Hasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { Sha2Hasher };
|
package/lib/hasher/sha2.js
CHANGED
|
@@ -1,91 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Sha2Hasher = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
|
+
const util_1 = require("@ocap/util");
|
|
9
|
+
const hash_js_1 = require("hash.js");
|
|
10
|
+
const encode_1 = __importDefault(require("../encode"));
|
|
5
11
|
const hashFns = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
sha224: hash_js_1.sha224,
|
|
13
|
+
sha256: hash_js_1.sha256,
|
|
14
|
+
sha384: hash_js_1.sha384,
|
|
15
|
+
sha512: hash_js_1.sha512,
|
|
10
16
|
};
|
|
11
|
-
|
|
12
17
|
/**
|
|
13
18
|
* Sha2 support with different hash length
|
|
14
19
|
*
|
|
15
20
|
* @class
|
|
16
21
|
*/
|
|
17
22
|
class Sha2Hasher {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
23
|
+
constructor() {
|
|
24
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
25
|
+
const name = `hash${x}`;
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
const hasher = hashFns[`sha${x}`];
|
|
28
|
+
const hashFn = (input, round) => {
|
|
29
|
+
let inputBytes = input;
|
|
30
|
+
try {
|
|
31
|
+
inputBytes = (0, util_1.toUint8Array)(input);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
// Do nothing
|
|
35
|
+
}
|
|
36
|
+
if (round === 1) {
|
|
37
|
+
return `0x${hasher().update(inputBytes).digest('hex')}`;
|
|
38
|
+
}
|
|
39
|
+
return hashFn(hashFn(inputBytes, 1), round - 1);
|
|
40
|
+
};
|
|
41
|
+
this[name] = (data, round = 2, encoding = 'hex') => {
|
|
42
|
+
const res = hashFn(data, round);
|
|
43
|
+
return (0, encode_1.default)(res, encoding);
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* Do hash with length of 224
|
|
46
|
-
*
|
|
47
|
-
* @function
|
|
48
|
-
* @name Sha2Hasher#hash224
|
|
49
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
50
|
-
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
|
|
51
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
52
|
-
* @returns {string} depends on encoding param, hex by default
|
|
53
|
-
* @memberof Sha2Hasher
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Do hash with length of 256
|
|
58
|
-
*
|
|
59
|
-
* @function
|
|
60
|
-
* @name Sha2Hasher#hash256
|
|
61
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
62
|
-
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
|
|
63
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
64
|
-
* @returns {string} depends on encoding param, hex by default
|
|
65
|
-
* @memberof Sha2Hasher
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Do hash with length of 384
|
|
70
|
-
*
|
|
71
|
-
* @function
|
|
72
|
-
* @name Sha2Hasher#hash384
|
|
73
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
74
|
-
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
|
|
75
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
76
|
-
* @returns {string} depends on encoding param, hex by default
|
|
77
|
-
* @memberof Sha2Hasher
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Do hash with length of 512
|
|
82
|
-
*
|
|
83
|
-
* @function
|
|
84
|
-
* @name Sha2Hasher#hash512
|
|
85
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
86
|
-
* @param {number} [round=2] - how many round to do the hash, larger = safer = slower
|
|
87
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
88
|
-
* @returns {string} depends on encoding param, hex by default
|
|
89
|
-
* @memberof Sha2Hasher
|
|
90
|
-
*/
|
|
91
|
-
module.exports = new Sha2Hasher();
|
|
48
|
+
exports.Sha2Hasher = Sha2Hasher;
|
|
49
|
+
exports.default = new Sha2Hasher();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
+
/**
|
|
3
|
+
* Sha3 support with different hash length
|
|
4
|
+
*
|
|
5
|
+
* @class Sha3Hasher
|
|
6
|
+
*/
|
|
7
|
+
declare class Sha3Hasher {
|
|
8
|
+
[x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
|
9
|
+
constructor();
|
|
10
|
+
}
|
|
11
|
+
declare const _default: Sha3Hasher;
|
|
12
|
+
export default _default;
|
|
13
|
+
export { Sha3Hasher };
|
package/lib/hasher/sha3.js
CHANGED
|
@@ -1,85 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Sha3Hasher = void 0;
|
|
7
|
+
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
8
|
+
const util_1 = require("@ocap/util");
|
|
9
|
+
const encode_1 = __importDefault(require("../encode"));
|
|
5
10
|
/**
|
|
6
11
|
* Sha3 support with different hash length
|
|
7
12
|
*
|
|
8
13
|
* @class Sha3Hasher
|
|
9
14
|
*/
|
|
10
15
|
class Sha3Hasher {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
16
|
+
constructor() {
|
|
17
|
+
[224, 256, 384, 512].forEach((x) => {
|
|
18
|
+
const name = `hash${x}`;
|
|
19
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
const hasher = js_sha3_1.default[`sha3_${x}`];
|
|
22
|
+
const hashFn = (input, round) => {
|
|
23
|
+
if (round === 1) {
|
|
24
|
+
return hasher(input);
|
|
25
|
+
}
|
|
26
|
+
return hashFn(hasher(input), round - 1);
|
|
27
|
+
};
|
|
28
|
+
this[name] = (data, round = 1, encoding = 'hex') => {
|
|
29
|
+
let input = data;
|
|
30
|
+
try {
|
|
31
|
+
input = (0, util_1.toUint8Array)(data);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
// Do nothing
|
|
35
|
+
}
|
|
36
|
+
const res = hashFn(input, round);
|
|
37
|
+
return (0, encode_1.default)(`0x${res}`, encoding);
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* Do hash with length of 224
|
|
39
|
-
*
|
|
40
|
-
* @function
|
|
41
|
-
* @name Sha3Hasher#hash224
|
|
42
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
43
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
44
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
45
|
-
* @returns {string} depends on encoding param, hex by default
|
|
46
|
-
* @memberof Sha3Hasher
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Do hash with length of 256
|
|
51
|
-
*
|
|
52
|
-
* @function
|
|
53
|
-
* @name Sha3Hasher#hash256
|
|
54
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
55
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
56
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
57
|
-
* @returns {string} depends on encoding param, hex by default
|
|
58
|
-
* @memberof Sha3Hasher
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Do hash with length of 384
|
|
63
|
-
*
|
|
64
|
-
* @function
|
|
65
|
-
* @name Sha3Hasher#hash384
|
|
66
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
67
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
68
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
69
|
-
* @returns {string} depends on encoding param, hex by default
|
|
70
|
-
* @memberof Sha3Hasher
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Do hash with length of 512
|
|
75
|
-
*
|
|
76
|
-
* @function
|
|
77
|
-
* @name Sha3Hasher#hash512
|
|
78
|
-
* @param {hex|buffer|base58|Uint8Array|string} input - data to hash
|
|
79
|
-
* @param {number} [round=1] - how many round to do the hash, larger = safer = slower
|
|
80
|
-
* @param {string} [encoding="hex"] - output encoding
|
|
81
|
-
* @returns {string} depends on encoding param, hex by default
|
|
82
|
-
* @memberof Sha3Hasher
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
module.exports = new Sha3Hasher();
|
|
42
|
+
exports.Sha3Hasher = Sha3Hasher;
|
|
43
|
+
exports.default = new Sha3Hasher();
|