@ohos-ports/crypto 0.0.0-stage → 0.3.1-beta.1
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/LICENSE +7 -0
- package/README.md +60 -2
- package/dist/bech32.d.ts +53 -0
- package/dist/bech32.js +187 -0
- package/dist/blake2b.d.ts +7 -0
- package/dist/blake2b.js +213 -0
- package/dist/bls12_318.d.ts +48 -0
- package/dist/bls12_318.js +268 -0
- package/dist/ed25519.d.ts +55 -0
- package/dist/ed25519.js +721 -0
- package/dist/hasGlobalWebCrypto.d.ts +1 -0
- package/dist/hasGlobalWebCrypto.js +21 -0
- package/dist/hmacSHA512.d.ts +6 -0
- package/dist/hmacSHA512.js +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/keccak.d.ts +4 -0
- package/dist/keccak.js +43 -0
- package/dist/kes/constants.d.ts +2 -0
- package/dist/kes/constants.js +5 -0
- package/dist/kes/index.d.ts +0 -0
- package/dist/kes/index.js +0 -0
- package/dist/kes/macro/sum_kes.d.ts +15 -0
- package/dist/kes/macro/sum_kes.js +9 -0
- package/dist/kes/singleKes.d.ts +0 -0
- package/dist/kes/singleKes.js +0 -0
- package/dist/noble/_assert.d.ts +23 -0
- package/dist/noble/_assert.js +55 -0
- package/dist/noble/_md.d.ts +29 -0
- package/dist/noble/_md.js +170 -0
- package/dist/noble/_shortw_utils.d.ts +61 -0
- package/dist/noble/_shortw_utils.js +63 -0
- package/dist/noble/_u64.d.ts +28 -0
- package/dist/noble/_u64.js +116 -0
- package/dist/noble/abstract/bls.d.ts +119 -0
- package/dist/noble/abstract/bls.js +334 -0
- package/dist/noble/abstract/curve.d.ts +83 -0
- package/dist/noble/abstract/curve.js +319 -0
- package/dist/noble/abstract/edwards.d.ts +85 -0
- package/dist/noble/abstract/edwards.js +508 -0
- package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
- package/dist/noble/abstract/hash-to-curve.js +220 -0
- package/dist/noble/abstract/modular.d.ts +122 -0
- package/dist/noble/abstract/modular.js +455 -0
- package/dist/noble/abstract/montgomery.d.ts +28 -0
- package/dist/noble/abstract/montgomery.js +172 -0
- package/dist/noble/abstract/utils.d.ts +106 -0
- package/dist/noble/abstract/utils.js +442 -0
- package/dist/noble/bls12_381.d.ts +95 -0
- package/dist/noble/bls12_381.js +1434 -0
- package/dist/noble/crypto.d.ts +1 -0
- package/dist/noble/crypto.js +4 -0
- package/dist/noble/curves/curve.d.ts +69 -0
- package/dist/noble/curves/curve.js +167 -0
- package/dist/noble/curves/modular.d.ts +122 -0
- package/dist/noble/curves/modular.js +442 -0
- package/dist/noble/curves/utils.d.ts +93 -0
- package/dist/noble/curves/utils.js +385 -0
- package/dist/noble/curves/weierstrass.d.ts +240 -0
- package/dist/noble/curves/weierstrass.js +1178 -0
- package/dist/noble/ed25519.d.ts +100 -0
- package/dist/noble/ed25519.js +494 -0
- package/dist/noble/hmac.d.ts +25 -0
- package/dist/noble/hmac.js +101 -0
- package/dist/noble/index.d.ts +2 -0
- package/dist/noble/index.js +20 -0
- package/dist/noble/keccak.d.ts +58 -0
- package/dist/noble/keccak.js +289 -0
- package/dist/noble/ripemd_160.d.ts +20 -0
- package/dist/noble/ripemd_160.js +147 -0
- package/dist/noble/secp256k1.d.ts +98 -0
- package/dist/noble/secp256k1.js +309 -0
- package/dist/noble/sha256.d.ts +34 -0
- package/dist/noble/sha256.js +146 -0
- package/dist/noble/sha512.d.ts +66 -0
- package/dist/noble/sha512.js +301 -0
- package/dist/noble/utils.d.ts +90 -0
- package/dist/noble/utils.js +278 -0
- package/dist/ripemd_160.d.ts +1 -0
- package/dist/ripemd_160.js +23 -0
- package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
- package/dist/rust_vrf_reimpl/curves.js +4225 -0
- package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
- package/dist/rust_vrf_reimpl/vrf03.js +373 -0
- package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
- package/dist/rust_vrf_reimpl/vrf10.js +468 -0
- package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
- package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
- package/dist/secp256k1.d.ts +4 -0
- package/dist/secp256k1.js +33 -0
- package/dist/sha2_256.d.ts +16 -0
- package/dist/sha2_256.js +403 -0
- package/dist/sha2_512.d.ts +16 -0
- package/dist/sha2_512.js +256 -0
- package/dist/sha3.d.ts +17 -0
- package/dist/sha3.js +181 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.js +120 -0
- package/dist/utils/assert.d.ts +3 -0
- package/dist/utils/assert.js +55 -0
- package/dist/utils/base32.d.ts +5 -0
- package/dist/utils/base32.js +52 -0
- package/dist/utils/bigintToBuffer.d.ts +1 -0
- package/dist/utils/bigintToBuffer.js +27 -0
- package/dist/utils/bytesToHex.d.ts +2 -0
- package/dist/utils/bytesToHex.js +15 -0
- package/dist/utils/getRandomValues.d.ts +4 -0
- package/dist/utils/getRandomValues.js +12 -0
- package/dist/utils/hexToBytes.d.ts +2 -0
- package/dist/utils/hexToBytes.js +14 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +19 -0
- package/dist/utils/positiveMod.d.ts +1 -0
- package/dist/utils/positiveMod.js +11 -0
- package/dist/utils/promisifySync.d.ts +0 -0
- package/dist/utils/promisifySync.js +0 -0
- package/dist/utils/rotr32.d.ts +2 -0
- package/dist/utils/rotr32.js +7 -0
- package/dist/vrf.d.ts +74 -0
- package/dist/vrf.js +286 -0
- package/package.json +49 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.hmac = exports.HMAC = void 0;
|
|
19
|
+
var _assert_1 = require("./_assert.js");
|
|
20
|
+
var utils_1 = require("./utils.js");
|
|
21
|
+
// HMAC (RFC 2104)
|
|
22
|
+
var HMAC = /** @class */ (function (_super) {
|
|
23
|
+
__extends(HMAC, _super);
|
|
24
|
+
function HMAC(hash, _key) {
|
|
25
|
+
var _this = _super.call(this) || this;
|
|
26
|
+
_this.finished = false;
|
|
27
|
+
_this.destroyed = false;
|
|
28
|
+
(0, _assert_1.hash)(hash);
|
|
29
|
+
var key = (0, utils_1.toBytes)(_key);
|
|
30
|
+
_this.iHash = hash.create();
|
|
31
|
+
if (typeof _this.iHash.update !== 'function')
|
|
32
|
+
throw new Error('Expected instance of class which extends utils.Hash');
|
|
33
|
+
_this.blockLen = _this.iHash.blockLen;
|
|
34
|
+
_this.outputLen = _this.iHash.outputLen;
|
|
35
|
+
var blockLen = _this.blockLen;
|
|
36
|
+
var pad = new Uint8Array(blockLen);
|
|
37
|
+
// blockLen can be bigger than outputLen
|
|
38
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
39
|
+
for (var i = 0; i < pad.length; i++)
|
|
40
|
+
pad[i] ^= 0x36;
|
|
41
|
+
_this.iHash.update(pad);
|
|
42
|
+
// By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
|
|
43
|
+
_this.oHash = hash.create();
|
|
44
|
+
// Undo internal XOR && apply outer XOR
|
|
45
|
+
for (var i = 0; i < pad.length; i++)
|
|
46
|
+
pad[i] ^= 0x36 ^ 0x5c;
|
|
47
|
+
_this.oHash.update(pad);
|
|
48
|
+
pad.fill(0);
|
|
49
|
+
return _this;
|
|
50
|
+
}
|
|
51
|
+
HMAC.prototype.update = function (buf) {
|
|
52
|
+
(0, _assert_1.exists)(this);
|
|
53
|
+
this.iHash.update(buf);
|
|
54
|
+
return this;
|
|
55
|
+
};
|
|
56
|
+
HMAC.prototype.digestInto = function (out) {
|
|
57
|
+
(0, _assert_1.exists)(this);
|
|
58
|
+
(0, _assert_1.bytes)(out, this.outputLen);
|
|
59
|
+
this.finished = true;
|
|
60
|
+
this.iHash.digestInto(out);
|
|
61
|
+
this.oHash.update(out);
|
|
62
|
+
this.oHash.digestInto(out);
|
|
63
|
+
this.destroy();
|
|
64
|
+
};
|
|
65
|
+
HMAC.prototype.digest = function () {
|
|
66
|
+
var out = new Uint8Array(this.oHash.outputLen);
|
|
67
|
+
this.digestInto(out);
|
|
68
|
+
return out;
|
|
69
|
+
};
|
|
70
|
+
HMAC.prototype._cloneInto = function (to) {
|
|
71
|
+
// Create new instance without calling constructor since key already in state and we don't know it.
|
|
72
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
73
|
+
var _a = this, oHash = _a.oHash, iHash = _a.iHash, finished = _a.finished, destroyed = _a.destroyed, blockLen = _a.blockLen, outputLen = _a.outputLen;
|
|
74
|
+
to = to;
|
|
75
|
+
to.finished = finished;
|
|
76
|
+
to.destroyed = destroyed;
|
|
77
|
+
to.blockLen = blockLen;
|
|
78
|
+
to.outputLen = outputLen;
|
|
79
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
80
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
81
|
+
return to;
|
|
82
|
+
};
|
|
83
|
+
HMAC.prototype.destroy = function () {
|
|
84
|
+
this.destroyed = true;
|
|
85
|
+
this.oHash.destroy();
|
|
86
|
+
this.iHash.destroy();
|
|
87
|
+
};
|
|
88
|
+
return HMAC;
|
|
89
|
+
}(utils_1.Hash));
|
|
90
|
+
exports.HMAC = HMAC;
|
|
91
|
+
/**
|
|
92
|
+
* HMAC: RFC2104 message authentication code.
|
|
93
|
+
* @param hash - function that would be used e.g. sha256
|
|
94
|
+
* @param key - message key
|
|
95
|
+
* @param message - message data
|
|
96
|
+
*/
|
|
97
|
+
var hmac = function (hash, key, message) {
|
|
98
|
+
return new HMAC(hash, key).update(message).digest();
|
|
99
|
+
};
|
|
100
|
+
exports.hmac = hmac;
|
|
101
|
+
exports.hmac.create = function (hash, key) { return new HMAC(hash, key); };
|
|
@@ -0,0 +1,20 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./keccak.js"), exports);
|
|
18
|
+
__exportStar(require("./bls12_381.js"), exports);
|
|
19
|
+
// DO NOT export "./_u64";
|
|
20
|
+
// DO NOT export "./utils";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Hash, Input, HashXOF } from './utils.js';
|
|
2
|
+
export declare function keccakP(s: Uint32Array, rounds?: number): void;
|
|
3
|
+
export declare class Keccak extends Hash<Keccak> implements HashXOF<Keccak> {
|
|
4
|
+
blockLen: number;
|
|
5
|
+
suffix: number;
|
|
6
|
+
outputLen: number;
|
|
7
|
+
protected enableXOF: boolean;
|
|
8
|
+
protected rounds: number;
|
|
9
|
+
protected state: Uint8Array;
|
|
10
|
+
protected pos: number;
|
|
11
|
+
protected posOut: number;
|
|
12
|
+
protected finished: boolean;
|
|
13
|
+
protected state32: Uint32Array;
|
|
14
|
+
protected destroyed: boolean;
|
|
15
|
+
constructor(blockLen: number, suffix: number, outputLen: number, enableXOF?: boolean, rounds?: number);
|
|
16
|
+
protected keccak(): void;
|
|
17
|
+
update(data: Input): this;
|
|
18
|
+
protected finish(): void;
|
|
19
|
+
protected writeInto(out: Uint8Array): Uint8Array;
|
|
20
|
+
xofInto(out: Uint8Array): Uint8Array;
|
|
21
|
+
xof(bytes: number): Uint8Array;
|
|
22
|
+
digestInto(out: Uint8Array): Uint8Array;
|
|
23
|
+
digest(): Uint8Array;
|
|
24
|
+
destroy(): void;
|
|
25
|
+
_cloneInto(to?: Keccak): Keccak;
|
|
26
|
+
assertExists(checkFinished?: boolean): void;
|
|
27
|
+
}
|
|
28
|
+
export declare const keccak_224: {
|
|
29
|
+
(msg: Input): Uint8Array;
|
|
30
|
+
outputLen: number;
|
|
31
|
+
blockLen: number;
|
|
32
|
+
create(): Hash<Keccak>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* keccak-256 hash function. Different from SHA3-256.
|
|
36
|
+
* @param message - that would be hashed
|
|
37
|
+
*/
|
|
38
|
+
export declare const keccak_256: {
|
|
39
|
+
(msg: Input): Uint8Array;
|
|
40
|
+
outputLen: number;
|
|
41
|
+
blockLen: number;
|
|
42
|
+
create(): Hash<Keccak>;
|
|
43
|
+
};
|
|
44
|
+
export declare const keccak_384: {
|
|
45
|
+
(msg: Input): Uint8Array;
|
|
46
|
+
outputLen: number;
|
|
47
|
+
blockLen: number;
|
|
48
|
+
create(): Hash<Keccak>;
|
|
49
|
+
};
|
|
50
|
+
export declare const keccak_512: {
|
|
51
|
+
(msg: Input): Uint8Array;
|
|
52
|
+
outputLen: number;
|
|
53
|
+
blockLen: number;
|
|
54
|
+
create(): Hash<Keccak>;
|
|
55
|
+
};
|
|
56
|
+
export type ShakeOpts = {
|
|
57
|
+
dkLen?: number;
|
|
58
|
+
};
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
var _a;
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.keccak_512 = exports.keccak_384 = exports.keccak_256 = exports.keccak_224 = exports.Keccak = exports.keccakP = void 0;
|
|
36
|
+
/*
|
|
37
|
+
THIS IMPLEMENTATION WAS TAKEN FROM `@noble/hashes`
|
|
38
|
+
|
|
39
|
+
original source: https://github.com/paulmillr/noble-hashes/blob/6ae82e75d4c354d53a488e95b1bfe9cba90bd6ff/src/sha3.ts#L1
|
|
40
|
+
|
|
41
|
+
it was modified and adapted to this repository to re-use already defined stuff
|
|
42
|
+
|
|
43
|
+
the main reason for copying over and not using as dependency is because of different target in the `tsconfig.json`
|
|
44
|
+
|
|
45
|
+
`@noble/hashes` targets es2020, which tends to cause problems with some other (older) packages sometimes
|
|
46
|
+
|
|
47
|
+
this repo targets ES5+
|
|
48
|
+
*/
|
|
49
|
+
var _u64_1 = require("./_u64.js");
|
|
50
|
+
var utils_1 = require("./utils.js");
|
|
51
|
+
var assert_1 = require("../utils/assert.js");
|
|
52
|
+
// SHA3 (keccak) is based on a new design: basically, the internal state is bigger than output size.
|
|
53
|
+
// It's called a sponge function.
|
|
54
|
+
// Various per round constants calculations
|
|
55
|
+
var SHA3_PI = [];
|
|
56
|
+
var SHA3_ROTL = [];
|
|
57
|
+
var _SHA3_IOTA = [];
|
|
58
|
+
var _0n = /** @__PURE__ */ BigInt(0);
|
|
59
|
+
var _1n = /** @__PURE__ */ BigInt(1);
|
|
60
|
+
var _2n = /** @__PURE__ */ BigInt(2);
|
|
61
|
+
var _7n = /** @__PURE__ */ BigInt(7);
|
|
62
|
+
var _256n = /** @__PURE__ */ BigInt(256);
|
|
63
|
+
var _0x71n = /** @__PURE__ */ BigInt(0x71);
|
|
64
|
+
for (var round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
|
65
|
+
// Pi
|
|
66
|
+
_a = __read([y, (2 * x + 3 * y) % 5], 2), x = _a[0], y = _a[1];
|
|
67
|
+
SHA3_PI.push(2 * (5 * y + x));
|
|
68
|
+
// Rotational
|
|
69
|
+
SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
|
|
70
|
+
// Iota
|
|
71
|
+
var t = _0n;
|
|
72
|
+
for (var j = 0; j < 7; j++) {
|
|
73
|
+
R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
|
|
74
|
+
if (R & _2n)
|
|
75
|
+
t ^= _1n << ((_1n << /** @__PURE__ */ BigInt(j)) - _1n);
|
|
76
|
+
}
|
|
77
|
+
_SHA3_IOTA.push(t);
|
|
78
|
+
}
|
|
79
|
+
var _b = __read(/** @__PURE__ */ (0, _u64_1.split)(_SHA3_IOTA, true), 2), SHA3_IOTA_H = _b[0], SHA3_IOTA_L = _b[1];
|
|
80
|
+
// Left rotation (without 0, 32, 64)
|
|
81
|
+
var rotlH = function (h, l, s) { return (s > 32 ? (0, _u64_1.rotlBH)(h, l, s) : (0, _u64_1.rotlSH)(h, l, s)); };
|
|
82
|
+
var rotlL = function (h, l, s) { return (s > 32 ? (0, _u64_1.rotlBL)(h, l, s) : (0, _u64_1.rotlSL)(h, l, s)); };
|
|
83
|
+
// Same as keccakf1600, but allows to skip some rounds
|
|
84
|
+
function keccakP(s, rounds) {
|
|
85
|
+
if (rounds === void 0) { rounds = 24; }
|
|
86
|
+
var B = new Uint32Array(5 * 2);
|
|
87
|
+
// NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
|
|
88
|
+
for (var round = 24 - rounds; round < 24; round++) {
|
|
89
|
+
// Theta θ
|
|
90
|
+
for (var x = 0; x < 10; x++)
|
|
91
|
+
B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
|
92
|
+
for (var x = 0; x < 10; x += 2) {
|
|
93
|
+
var idx1 = (x + 8) % 10;
|
|
94
|
+
var idx0 = (x + 2) % 10;
|
|
95
|
+
var B0 = B[idx0];
|
|
96
|
+
var B1 = B[idx0 + 1];
|
|
97
|
+
var Th = rotlH(B0, B1, 1) ^ B[idx1];
|
|
98
|
+
var Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
|
99
|
+
for (var y = 0; y < 50; y += 10) {
|
|
100
|
+
s[x + y] ^= Th;
|
|
101
|
+
s[x + y + 1] ^= Tl;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Rho (ρ) and Pi (π)
|
|
105
|
+
var curH = s[2];
|
|
106
|
+
var curL = s[3];
|
|
107
|
+
for (var t = 0; t < 24; t++) {
|
|
108
|
+
var shift = SHA3_ROTL[t];
|
|
109
|
+
var Th = rotlH(curH, curL, shift);
|
|
110
|
+
var Tl = rotlL(curH, curL, shift);
|
|
111
|
+
var PI = SHA3_PI[t];
|
|
112
|
+
curH = s[PI];
|
|
113
|
+
curL = s[PI + 1];
|
|
114
|
+
s[PI] = Th;
|
|
115
|
+
s[PI + 1] = Tl;
|
|
116
|
+
}
|
|
117
|
+
// Chi (χ)
|
|
118
|
+
for (var y = 0; y < 50; y += 10) {
|
|
119
|
+
for (var x = 0; x < 10; x++)
|
|
120
|
+
B[x] = s[y + x];
|
|
121
|
+
for (var x = 0; x < 10; x++)
|
|
122
|
+
s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
|
|
123
|
+
}
|
|
124
|
+
// Iota (ι)
|
|
125
|
+
s[0] ^= SHA3_IOTA_H[round];
|
|
126
|
+
s[1] ^= SHA3_IOTA_L[round];
|
|
127
|
+
}
|
|
128
|
+
B.fill(0);
|
|
129
|
+
}
|
|
130
|
+
exports.keccakP = keccakP;
|
|
131
|
+
var Keccak = /** @class */ (function (_super) {
|
|
132
|
+
__extends(Keccak, _super);
|
|
133
|
+
// NOTE: we accept arguments in bytes instead of bits here.
|
|
134
|
+
function Keccak(blockLen, suffix, outputLen, enableXOF, rounds) {
|
|
135
|
+
if (enableXOF === void 0) { enableXOF = false; }
|
|
136
|
+
if (rounds === void 0) { rounds = 24; }
|
|
137
|
+
var _this = _super.call(this) || this;
|
|
138
|
+
_this.blockLen = blockLen;
|
|
139
|
+
_this.suffix = suffix;
|
|
140
|
+
_this.outputLen = outputLen;
|
|
141
|
+
_this.enableXOF = enableXOF;
|
|
142
|
+
_this.rounds = rounds;
|
|
143
|
+
_this.pos = 0;
|
|
144
|
+
_this.posOut = 0;
|
|
145
|
+
_this.finished = false;
|
|
146
|
+
_this.destroyed = false;
|
|
147
|
+
// Can be passed from user as dkLen
|
|
148
|
+
(0, assert_1.assertPostiveInteger)(outputLen);
|
|
149
|
+
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
|
|
150
|
+
if (0 >= _this.blockLen || _this.blockLen >= 200)
|
|
151
|
+
throw new Error('Sha3 supports only keccak-f1600 function');
|
|
152
|
+
_this.state = new Uint8Array(200);
|
|
153
|
+
_this.state32 = (0, utils_1.u32)(_this.state);
|
|
154
|
+
return _this;
|
|
155
|
+
}
|
|
156
|
+
Keccak.prototype.keccak = function () {
|
|
157
|
+
if (!utils_1.isLE)
|
|
158
|
+
(0, utils_1.byteSwap32)(this.state32);
|
|
159
|
+
keccakP(this.state32, this.rounds);
|
|
160
|
+
if (!utils_1.isLE)
|
|
161
|
+
(0, utils_1.byteSwap32)(this.state32);
|
|
162
|
+
this.posOut = 0;
|
|
163
|
+
this.pos = 0;
|
|
164
|
+
};
|
|
165
|
+
Keccak.prototype.update = function (data) {
|
|
166
|
+
this.assertExists();
|
|
167
|
+
var _a = this, blockLen = _a.blockLen, state = _a.state;
|
|
168
|
+
data = (0, utils_1.toBytes)(data);
|
|
169
|
+
var len = data.length;
|
|
170
|
+
for (var pos = 0; pos < len;) {
|
|
171
|
+
var take = Math.min(blockLen - this.pos, len - pos);
|
|
172
|
+
for (var i = 0; i < take; i++)
|
|
173
|
+
state[this.pos++] ^= data[pos++];
|
|
174
|
+
if (this.pos === blockLen)
|
|
175
|
+
this.keccak();
|
|
176
|
+
}
|
|
177
|
+
return this;
|
|
178
|
+
};
|
|
179
|
+
Keccak.prototype.finish = function () {
|
|
180
|
+
if (this.finished)
|
|
181
|
+
return;
|
|
182
|
+
this.finished = true;
|
|
183
|
+
var _a = this, state = _a.state, suffix = _a.suffix, pos = _a.pos, blockLen = _a.blockLen;
|
|
184
|
+
// Do the padding
|
|
185
|
+
state[pos] ^= suffix;
|
|
186
|
+
if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
|
|
187
|
+
this.keccak();
|
|
188
|
+
state[blockLen - 1] ^= 0x80;
|
|
189
|
+
this.keccak();
|
|
190
|
+
};
|
|
191
|
+
Keccak.prototype.writeInto = function (out) {
|
|
192
|
+
this.assertExists(false);
|
|
193
|
+
if (!(out instanceof Uint8Array))
|
|
194
|
+
throw new Error("Uint8Array expected");
|
|
195
|
+
this.finish();
|
|
196
|
+
var bufferOut = this.state;
|
|
197
|
+
var blockLen = this.blockLen;
|
|
198
|
+
for (var pos = 0, len = out.length; pos < len;) {
|
|
199
|
+
if (this.posOut >= blockLen)
|
|
200
|
+
this.keccak();
|
|
201
|
+
var take = Math.min(blockLen - this.posOut, len - pos);
|
|
202
|
+
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
203
|
+
this.posOut += take;
|
|
204
|
+
pos += take;
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
};
|
|
208
|
+
Keccak.prototype.xofInto = function (out) {
|
|
209
|
+
// Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
|
|
210
|
+
if (!this.enableXOF)
|
|
211
|
+
throw new Error('XOF is not possible for this instance');
|
|
212
|
+
return this.writeInto(out);
|
|
213
|
+
};
|
|
214
|
+
Keccak.prototype.xof = function (bytes) {
|
|
215
|
+
(0, assert_1.assertPostiveInteger)(bytes);
|
|
216
|
+
return this.xofInto(new Uint8Array(bytes));
|
|
217
|
+
};
|
|
218
|
+
Keccak.prototype.digestInto = function (out) {
|
|
219
|
+
// output(out, this);
|
|
220
|
+
if (!(out instanceof Uint8Array))
|
|
221
|
+
throw new Error("Uint8Array expected");
|
|
222
|
+
var min = this.outputLen;
|
|
223
|
+
if (out.length < min)
|
|
224
|
+
throw new Error("digestInto() expects output buffer of length at least ".concat(min));
|
|
225
|
+
if (this.finished)
|
|
226
|
+
throw new Error('digest() was already called');
|
|
227
|
+
this.writeInto(out);
|
|
228
|
+
this.destroy();
|
|
229
|
+
return out;
|
|
230
|
+
};
|
|
231
|
+
Keccak.prototype.digest = function () {
|
|
232
|
+
return this.digestInto(new Uint8Array(this.outputLen));
|
|
233
|
+
};
|
|
234
|
+
Keccak.prototype.destroy = function () {
|
|
235
|
+
this.destroyed = true;
|
|
236
|
+
this.state.fill(0);
|
|
237
|
+
};
|
|
238
|
+
Keccak.prototype._cloneInto = function (to) {
|
|
239
|
+
var _a = this, blockLen = _a.blockLen, suffix = _a.suffix, outputLen = _a.outputLen, rounds = _a.rounds, enableXOF = _a.enableXOF;
|
|
240
|
+
to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
|
|
241
|
+
to.state32.set(this.state32);
|
|
242
|
+
to.pos = this.pos;
|
|
243
|
+
to.posOut = this.posOut;
|
|
244
|
+
to.finished = this.finished;
|
|
245
|
+
to.rounds = rounds;
|
|
246
|
+
// Suffix can change in cSHAKE
|
|
247
|
+
to.suffix = suffix;
|
|
248
|
+
to.outputLen = outputLen;
|
|
249
|
+
to.enableXOF = enableXOF;
|
|
250
|
+
to.destroyed = this.destroyed;
|
|
251
|
+
return to;
|
|
252
|
+
};
|
|
253
|
+
Keccak.prototype.assertExists = function (checkFinished) {
|
|
254
|
+
if (checkFinished === void 0) { checkFinished = true; }
|
|
255
|
+
if (this.destroyed)
|
|
256
|
+
throw new Error('Hash instance has been destroyed');
|
|
257
|
+
if (checkFinished && this.finished)
|
|
258
|
+
throw new Error('Hash#digest() has already been called');
|
|
259
|
+
};
|
|
260
|
+
return Keccak;
|
|
261
|
+
}(utils_1.Hash));
|
|
262
|
+
exports.Keccak = Keccak;
|
|
263
|
+
var gen = function (suffix, blockLen, outputLen) {
|
|
264
|
+
return (0, utils_1.wrapConstructor)(function () { return new Keccak(blockLen, suffix, outputLen); });
|
|
265
|
+
};
|
|
266
|
+
// export const sha3_224 = /** @__PURE__ */ gen(0x06, 144, 224 / 8);
|
|
267
|
+
// /**
|
|
268
|
+
// * SHA3-256 hash function
|
|
269
|
+
// * @param message - that would be hashed
|
|
270
|
+
// */
|
|
271
|
+
// export const sha3_256 = /** @__PURE__ */ gen(0x06, 136, 256 / 8);
|
|
272
|
+
// export const sha3_384 = /** @__PURE__ */ gen(0x06, 104, 384 / 8);
|
|
273
|
+
// export const sha3_512 = /** @__PURE__ */ gen(0x06, 72, 512 / 8);
|
|
274
|
+
exports.keccak_224 = gen(0x01, 144, 224 / 8);
|
|
275
|
+
/**
|
|
276
|
+
* keccak-256 hash function. Different from SHA3-256.
|
|
277
|
+
* @param message - that would be hashed
|
|
278
|
+
*/
|
|
279
|
+
exports.keccak_256 = gen(0x01, 136, 256 / 8);
|
|
280
|
+
exports.keccak_384 = gen(0x01, 104, 384 / 8);
|
|
281
|
+
exports.keccak_512 = gen(0x01, 72, 512 / 8);
|
|
282
|
+
// const genShake = (suffix: number, blockLen: number, outputLen: number) =>
|
|
283
|
+
// wrapXOFConstructorWithOpts<HashXOF<Keccak>, ShakeOpts>(
|
|
284
|
+
// (opts: ShakeOpts = {}) =>
|
|
285
|
+
// new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)
|
|
286
|
+
// );
|
|
287
|
+
//
|
|
288
|
+
// export const shake128 = /** @__PURE__ */ genShake(0x1f, 168, 128 / 8);
|
|
289
|
+
// export const shake256 = /** @__PURE__ */ genShake(0x1f, 136, 256 / 8);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RIPEMD-160 legacy hash function.
|
|
3
|
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
|
4
|
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { HashMD } from './_md.js';
|
|
8
|
+
export declare class RIPEMD160 extends HashMD<RIPEMD160> {
|
|
9
|
+
private h0;
|
|
10
|
+
private h1;
|
|
11
|
+
private h2;
|
|
12
|
+
private h3;
|
|
13
|
+
private h4;
|
|
14
|
+
constructor();
|
|
15
|
+
protected get(): [number, number, number, number, number];
|
|
16
|
+
protected set(h0: number, h1: number, h2: number, h3: number, h4: number): void;
|
|
17
|
+
protected process(view: DataView, offset: number): void;
|
|
18
|
+
protected roundClean(): void;
|
|
19
|
+
destroy(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __values = (this && this.__values) || function(o) {
|
|
18
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
19
|
+
if (m) return m.call(o);
|
|
20
|
+
if (o && typeof o.length === "number") return {
|
|
21
|
+
next: function () {
|
|
22
|
+
if (o && i >= o.length) o = void 0;
|
|
23
|
+
return { value: o && o[i++], done: !o };
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
27
|
+
};
|
|
28
|
+
var e_1, _a;
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.RIPEMD160 = void 0;
|
|
31
|
+
/**
|
|
32
|
+
* RIPEMD-160 legacy hash function.
|
|
33
|
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
|
34
|
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
|
|
35
|
+
* @module
|
|
36
|
+
*/
|
|
37
|
+
var _md_1 = require("./_md.js");
|
|
38
|
+
var utils_1 = require("./utils.js");
|
|
39
|
+
var Rho = /* @__PURE__ */ new Uint8Array([7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8]);
|
|
40
|
+
var Id = /* @__PURE__ */ new Uint8Array(new Array(16).fill(0).map(function (_, i) { return i; }));
|
|
41
|
+
var Pi = /* @__PURE__ */ Id.map(function (i) { return (9 * i + 5) % 16; });
|
|
42
|
+
var idxL = [Id];
|
|
43
|
+
var idxR = [Pi];
|
|
44
|
+
for (var i = 0; i < 4; i++)
|
|
45
|
+
try {
|
|
46
|
+
for (var _b = (e_1 = void 0, __values([idxL, idxR])), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
47
|
+
var j = _c.value;
|
|
48
|
+
j.push(j[i].map(function (k) { return Rho[k]; }));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
52
|
+
finally {
|
|
53
|
+
try {
|
|
54
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
55
|
+
}
|
|
56
|
+
finally { if (e_1) throw e_1.error; }
|
|
57
|
+
}
|
|
58
|
+
var shifts = /* @__PURE__ */ [
|
|
59
|
+
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
60
|
+
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
61
|
+
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
62
|
+
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
63
|
+
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
|
|
64
|
+
].map(function (i) { return new Uint8Array(i); });
|
|
65
|
+
var shiftsL = /* @__PURE__ */ idxL.map(function (idx, i) { return idx.map(function (j) { return shifts[i][j]; }); });
|
|
66
|
+
var shiftsR = /* @__PURE__ */ idxR.map(function (idx, i) { return idx.map(function (j) { return shifts[i][j]; }); });
|
|
67
|
+
var Kl = /* @__PURE__ */ new Uint32Array([
|
|
68
|
+
0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
|
|
69
|
+
]);
|
|
70
|
+
var Kr = /* @__PURE__ */ new Uint32Array([
|
|
71
|
+
0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
|
|
72
|
+
]);
|
|
73
|
+
// It's called f() in spec.
|
|
74
|
+
function f(group, x, y, z) {
|
|
75
|
+
if (group === 0)
|
|
76
|
+
return x ^ y ^ z;
|
|
77
|
+
else if (group === 1)
|
|
78
|
+
return (x & y) | (~x & z);
|
|
79
|
+
else if (group === 2)
|
|
80
|
+
return (x | ~y) ^ z;
|
|
81
|
+
else if (group === 3)
|
|
82
|
+
return (x & z) | (y & ~z);
|
|
83
|
+
else
|
|
84
|
+
return x ^ (y | ~z);
|
|
85
|
+
}
|
|
86
|
+
// Temporary buffer, not used to store anything between runs
|
|
87
|
+
var R_BUF = /* @__PURE__ */ new Uint32Array(16);
|
|
88
|
+
var RIPEMD160 = /** @class */ (function (_super) {
|
|
89
|
+
__extends(RIPEMD160, _super);
|
|
90
|
+
function RIPEMD160() {
|
|
91
|
+
var _this = _super.call(this, 64, 20, 8, true) || this;
|
|
92
|
+
_this.h0 = 0x67452301 | 0;
|
|
93
|
+
_this.h1 = 0xefcdab89 | 0;
|
|
94
|
+
_this.h2 = 0x98badcfe | 0;
|
|
95
|
+
_this.h3 = 0x10325476 | 0;
|
|
96
|
+
_this.h4 = 0xc3d2e1f0 | 0;
|
|
97
|
+
return _this;
|
|
98
|
+
}
|
|
99
|
+
RIPEMD160.prototype.get = function () {
|
|
100
|
+
var _a = this, h0 = _a.h0, h1 = _a.h1, h2 = _a.h2, h3 = _a.h3, h4 = _a.h4;
|
|
101
|
+
return [h0, h1, h2, h3, h4];
|
|
102
|
+
};
|
|
103
|
+
RIPEMD160.prototype.set = function (h0, h1, h2, h3, h4) {
|
|
104
|
+
this.h0 = h0 | 0;
|
|
105
|
+
this.h1 = h1 | 0;
|
|
106
|
+
this.h2 = h2 | 0;
|
|
107
|
+
this.h3 = h3 | 0;
|
|
108
|
+
this.h4 = h4 | 0;
|
|
109
|
+
};
|
|
110
|
+
RIPEMD160.prototype.process = function (view, offset) {
|
|
111
|
+
for (var i = 0; i < 16; i++, offset += 4)
|
|
112
|
+
R_BUF[i] = view.getUint32(offset, true);
|
|
113
|
+
// prettier-ignore
|
|
114
|
+
var al = this.h0 | 0, ar = al, bl = this.h1 | 0, br = bl, cl = this.h2 | 0, cr = cl, dl = this.h3 | 0, dr = dl, el = this.h4 | 0, er = el;
|
|
115
|
+
// Instead of iterating 0 to 80, we split it into 5 groups
|
|
116
|
+
// And use the groups in constants, functions, etc. Much simpler
|
|
117
|
+
for (var group = 0; group < 5; group++) {
|
|
118
|
+
var rGroup = 4 - group;
|
|
119
|
+
var hbl = Kl[group], hbr = Kr[group]; // prettier-ignore
|
|
120
|
+
var rl = idxL[group], rr = idxR[group]; // prettier-ignore
|
|
121
|
+
var sl = shiftsL[group], sr = shiftsR[group]; // prettier-ignore
|
|
122
|
+
for (var i = 0; i < 16; i++) {
|
|
123
|
+
var tl = ((0, utils_1.rotl)(al + f(group, bl, cl, dl) + R_BUF[rl[i]] + hbl, sl[i]) + el) | 0;
|
|
124
|
+
al = el, el = dl, dl = (0, utils_1.rotl)(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
|
|
125
|
+
}
|
|
126
|
+
// 2 loops are 10% faster
|
|
127
|
+
for (var i = 0; i < 16; i++) {
|
|
128
|
+
var tr = ((0, utils_1.rotl)(ar + f(rGroup, br, cr, dr) + R_BUF[rr[i]] + hbr, sr[i]) + er) | 0;
|
|
129
|
+
ar = er, er = dr, dr = (0, utils_1.rotl)(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Add the compressed chunk to the current hash value
|
|
133
|
+
this.set((this.h1 + cl + dr) | 0, (this.h2 + dl + er) | 0, (this.h3 + el + ar) | 0, (this.h4 + al + br) | 0, (this.h0 + bl + cr) | 0);
|
|
134
|
+
};
|
|
135
|
+
RIPEMD160.prototype.roundClean = function () {
|
|
136
|
+
R_BUF.fill(0);
|
|
137
|
+
};
|
|
138
|
+
RIPEMD160.prototype.destroy = function () {
|
|
139
|
+
this.destroyed = true;
|
|
140
|
+
this.buffer.fill(0);
|
|
141
|
+
this.set(0, 0, 0, 0, 0);
|
|
142
|
+
};
|
|
143
|
+
return RIPEMD160;
|
|
144
|
+
}(_md_1.HashMD));
|
|
145
|
+
exports.RIPEMD160 = RIPEMD160;
|
|
146
|
+
/** RIPEMD-160 - a legacy hash function from 1990s. */
|
|
147
|
+
var _ripemd160 = /* @__PURE__ */ (0, utils_1.wrapConstructor)(function () { return new RIPEMD160(); });
|