quarkdash 1.0.7 → 1.1.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 +4 -2
- package/dist/cjs/cipher/chacha.js +154 -0
- package/dist/cjs/cipher/chacha.js.map +1 -0
- package/dist/cjs/cipher/cipher.js +34 -0
- package/dist/cjs/cipher/cipher.js.map +1 -0
- package/dist/cjs/cipher/gimli.js +143 -0
- package/dist/cjs/cipher/gimli.js.map +1 -0
- package/dist/cjs/core/kdf.js.map +1 -0
- package/dist/cjs/{mac.js → core/mac.js} +37 -1
- package/dist/cjs/core/mac.js.map +1 -0
- package/dist/cjs/{types.js.map → core/types.js.map} +1 -1
- package/dist/cjs/{utils.js → core/utils.js} +25 -8
- package/dist/cjs/core/utils.js.map +1 -0
- package/dist/cjs/core/wasm_loader.js +100 -0
- package/dist/cjs/core/wasm_loader.js.map +1 -0
- package/dist/cjs/crypto.js +30 -13
- package/dist/cjs/crypto.js.map +1 -1
- package/dist/cjs/{sha.js → hash/sha.js} +3 -2
- package/dist/cjs/hash/sha.js.map +1 -0
- package/dist/cjs/hash/shake.js +318 -0
- package/dist/cjs/hash/shake.js.map +1 -0
- package/dist/cjs/index.js +16 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/{ringlwe.js → session/baselwe.js} +246 -203
- package/dist/cjs/session/baselwe.js.map +1 -0
- package/dist/cjs/session/ringlwe.js +19 -0
- package/dist/cjs/session/ringlwe.js.map +1 -0
- package/dist/cjs/session/rringlwe.js +42 -0
- package/dist/cjs/session/rringlwe.js.map +1 -0
- package/dist/esm/cipher/chacha.js +150 -0
- package/dist/esm/cipher/chacha.js.map +1 -0
- package/dist/esm/cipher/cipher.js +30 -0
- package/dist/esm/cipher/cipher.js.map +1 -0
- package/dist/esm/cipher/gimli.js +139 -0
- package/dist/esm/cipher/gimli.js.map +1 -0
- package/dist/esm/core/kdf.js.map +1 -0
- package/dist/esm/core/mac.js +87 -0
- package/dist/esm/core/mac.js.map +1 -0
- package/dist/esm/{types.js.map → core/types.js.map} +1 -1
- package/dist/esm/{utils.js → core/utils.js} +24 -4
- package/dist/esm/core/utils.js.map +1 -0
- package/dist/esm/core/wasm_loader.js +64 -0
- package/dist/esm/core/wasm_loader.js.map +1 -0
- package/dist/esm/crypto.js +30 -13
- package/dist/esm/crypto.js.map +1 -1
- package/dist/esm/{sha.js → hash/sha.js} +3 -2
- package/dist/esm/hash/sha.js.map +1 -0
- package/dist/esm/hash/shake.js +311 -0
- package/dist/esm/hash/shake.js.map +1 -0
- package/dist/esm/index.js +16 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/{ringlwe.js → session/baselwe.js} +244 -201
- package/dist/esm/session/baselwe.js.map +1 -0
- package/dist/esm/session/ringlwe.js +15 -0
- package/dist/esm/session/ringlwe.js.map +1 -0
- package/dist/esm/session/rringlwe.js +38 -0
- package/dist/esm/session/rringlwe.js.map +1 -0
- package/dist/shake.wasm +0 -0
- package/dist/types/cipher/chacha.d.ts +67 -0
- package/dist/types/cipher/cipher.d.ts +31 -0
- package/dist/types/cipher/gimli.d.ts +64 -0
- package/dist/types/{kdf.d.ts → core/kdf.d.ts} +3 -2
- package/dist/types/{mac.d.ts → core/mac.d.ts} +21 -2
- package/dist/types/{types.d.ts → core/types.d.ts} +5 -2
- package/dist/types/{utils.d.ts → core/utils.d.ts} +13 -0
- package/dist/types/core/wasm_loader.d.ts +5 -0
- package/dist/types/crypto.d.ts +8 -4
- package/dist/types/{sha.d.ts → hash/sha.d.ts} +3 -2
- package/dist/types/hash/shake.d.ts +85 -0
- package/dist/types/index.d.ts +15 -10
- package/dist/types/{ringlwe.d.ts → session/baselwe.d.ts} +87 -91
- package/dist/types/session/ringlwe.d.ts +21 -0
- package/dist/types/session/rringlwe.d.ts +27 -0
- package/package.json +13 -3
- package/wasm/shake.wasm +0 -0
- package/dist/cjs/cipher.js +0 -265
- package/dist/cjs/cipher.js.map +0 -1
- package/dist/cjs/kdf.js.map +0 -1
- package/dist/cjs/mac.js.map +0 -1
- package/dist/cjs/ringlwe.js.map +0 -1
- package/dist/cjs/sha.js.map +0 -1
- package/dist/cjs/shake.js +0 -159
- package/dist/cjs/shake.js.map +0 -1
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/cipher.js +0 -259
- package/dist/esm/cipher.js.map +0 -1
- package/dist/esm/kdf.js.map +0 -1
- package/dist/esm/mac.js +0 -51
- package/dist/esm/mac.js.map +0 -1
- package/dist/esm/ringlwe.js.map +0 -1
- package/dist/esm/sha.js.map +0 -1
- package/dist/esm/shake.js +0 -155
- package/dist/esm/shake.js.map +0 -1
- package/dist/esm/utils.js.map +0 -1
- package/dist/types/cipher.d.ts +0 -153
- package/dist/types/shake.d.ts +0 -53
- package/img/cover.png +0 -0
- package/src/cipher.ts +0 -260
- package/src/crypto.ts +0 -306
- package/src/index.ts +0 -27
- package/src/kdf.ts +0 -64
- package/src/mac.ts +0 -65
- package/src/ringlwe.ts +0 -289
- package/src/sha.ts +0 -265
- package/src/shake.ts +0 -163
- package/src/types.ts +0 -75
- package/src/utils.ts +0 -290
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.json +0 -18
- /package/dist/cjs/{kdf.js → core/kdf.js} +0 -0
- /package/dist/cjs/{types.js → core/types.js} +0 -0
- /package/dist/esm/{kdf.js → core/kdf.js} +0 -0
- /package/dist/esm/{types.js → core/types.js} +0 -0
package/src/shake.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Crypto SHAKE-256 Implementation
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Shake-256 Hash
|
|
12
|
-
*/
|
|
13
|
-
export class Shake256 {
|
|
14
|
-
/* Shake 256 Constants */
|
|
15
|
-
private static KECCAK_ROUNDS = 24;
|
|
16
|
-
private static RATE_BYTES = 136;
|
|
17
|
-
private static ROTATIONS: number[] = [
|
|
18
|
-
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
|
|
19
|
-
27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
|
|
20
|
-
];
|
|
21
|
-
private static RC: bigint[] = [
|
|
22
|
-
0x0000000000000001n, 0x0000000000008082n, 0x800000000000808an, 0x8000000080008000n,
|
|
23
|
-
0x000000000000808bn, 0x0000000080000001n, 0x8000000080008081n, 0x8000000000008009n,
|
|
24
|
-
0x000000000000008an, 0x0000000000000088n, 0x0000000080008009n, 0x000000008000000an,
|
|
25
|
-
0x000000008000808bn, 0x800000000000008bn, 0x8000000000008089n, 0x8000000000008003n,
|
|
26
|
-
0x8000000000008002n, 0x8000000000000080n, 0x000000000000800an, 0x800000008000000an,
|
|
27
|
-
0x8000000080008081n, 0x8000000000008080n, 0x0000000080000001n, 0x8000000080008008n
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Shake-256 async
|
|
32
|
-
* @param input {Uint8Array} Input buffer
|
|
33
|
-
* @param outputLength {number} Output buffer length
|
|
34
|
-
* @returns {Uint8Array} Output buffer
|
|
35
|
-
*/
|
|
36
|
-
public static async hash(input: Uint8Array, outputLength: number): Promise<Uint8Array> {
|
|
37
|
-
return this.process(input, outputLength);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Shake-256 sync
|
|
42
|
-
* @param input {Uint8Array} Input buffer
|
|
43
|
-
* @param outputLength {number} Output buffer length
|
|
44
|
-
* @returns {Uint8Array} Output buffer
|
|
45
|
-
*/
|
|
46
|
-
public static hashSync(input: Uint8Array, outputLength: number): Uint8Array {
|
|
47
|
-
return this.process(input, outputLength);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Process SHAKE-256
|
|
52
|
-
* @param input {Uint8Array} Input buffer
|
|
53
|
-
* @param outputLength {number} Output length
|
|
54
|
-
* @returns {Uint8Array} Output buffer
|
|
55
|
-
* @private
|
|
56
|
-
*/
|
|
57
|
-
private static process(input: Uint8Array, outputLength: number): Uint8Array {
|
|
58
|
-
const state = new Array(25).fill(0n);
|
|
59
|
-
// Absorb phase
|
|
60
|
-
let offset = 0;
|
|
61
|
-
let blockSize = this.RATE_BYTES;
|
|
62
|
-
while (offset < input.length) {
|
|
63
|
-
const block = input.subarray(offset, Math.min(offset + blockSize, input.length));
|
|
64
|
-
for (let i = 0; i < block.length; i++) {
|
|
65
|
-
const lane = i % 8;
|
|
66
|
-
const bytePos = i - lane;
|
|
67
|
-
const laneIdx = bytePos / 8;
|
|
68
|
-
const shift = BigInt(lane * 8);
|
|
69
|
-
const val = BigInt(block[i]) << shift;
|
|
70
|
-
state[laneIdx] ^= val;
|
|
71
|
-
}
|
|
72
|
-
offset += block.length;
|
|
73
|
-
if (block.length === blockSize || offset >= input.length) {
|
|
74
|
-
// padding
|
|
75
|
-
if (offset >= input.length) {
|
|
76
|
-
const lastByteIdx = block.length;
|
|
77
|
-
const padByte = 0x1F; // domain for SHAKE256
|
|
78
|
-
const laneIdx = Math.floor(lastByteIdx / 8);
|
|
79
|
-
const shift = BigInt((lastByteIdx % 8) * 8);
|
|
80
|
-
state[laneIdx] ^= (BigInt(padByte) << shift);
|
|
81
|
-
// final padding bit
|
|
82
|
-
const finalBytePos = lastByteIdx + 1;
|
|
83
|
-
const finalLane = Math.floor(finalBytePos / 8);
|
|
84
|
-
const finalShift = BigInt((finalBytePos % 8) * 8);
|
|
85
|
-
state[finalLane] ^= (0x80n << finalShift);
|
|
86
|
-
}
|
|
87
|
-
this.keccakF(state);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Squeeze phase
|
|
92
|
-
const result = new Uint8Array(outputLength);
|
|
93
|
-
let outOffset = 0;
|
|
94
|
-
while (outOffset < outputLength) {
|
|
95
|
-
for (let lane = 0; lane < 25 && outOffset < outputLength; lane++) {
|
|
96
|
-
let val = state[lane];
|
|
97
|
-
for (let byte = 0; byte < 8 && outOffset < outputLength; byte++) {
|
|
98
|
-
result[outOffset++] = Number(val & 0xFFn);
|
|
99
|
-
val >>= 8n;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (outOffset < outputLength) {
|
|
103
|
-
this.keccakF(state);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Keccak Function
|
|
111
|
-
* @param state {bigint[]} State array
|
|
112
|
-
* @private
|
|
113
|
-
*/
|
|
114
|
-
private static keccakF(state: bigint[]): void {
|
|
115
|
-
for (let round = 0; round < this.KECCAK_ROUNDS; round++) {
|
|
116
|
-
// Theta
|
|
117
|
-
const C = new Array(5);
|
|
118
|
-
for (let x = 0; x < 5; x++) {
|
|
119
|
-
C[x] = state[x] ^ state[x+5] ^ state[x+10] ^ state[x+15] ^ state[x+20];
|
|
120
|
-
}
|
|
121
|
-
const D = new Array(5);
|
|
122
|
-
for (let x = 0; x < 5; x++) {
|
|
123
|
-
D[x] = C[(x+4)%5] ^ this.rot(C[(x+1)%5], 1n);
|
|
124
|
-
}
|
|
125
|
-
for (let x = 0; x < 5; x++) {
|
|
126
|
-
for (let y = 0; y < 5; y++) {
|
|
127
|
-
state[x+5*y] ^= D[x];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Rho and Pi
|
|
132
|
-
let current = state[1];
|
|
133
|
-
for (let i = 0; i < 24; i++) {
|
|
134
|
-
const nextIdx = (2 * ((i + 1) % 5) + 5*Math.floor((i+1)/5)) % 25;
|
|
135
|
-
const temp = state[nextIdx];
|
|
136
|
-
state[nextIdx] = this.rot(current, BigInt(this.ROTATIONS[i]));
|
|
137
|
-
current = temp;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Chi
|
|
141
|
-
for (let y = 0; y < 5; y++) {
|
|
142
|
-
const row = state.slice(y*5, y*5+5);
|
|
143
|
-
for (let x = 0; x < 5; x++) {
|
|
144
|
-
state[y*5 + x] = row[x] ^ ((~row[(x+1)%5]) & row[(x+2)%5]);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Iota
|
|
149
|
-
state[0] ^= this.RC[round];
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Rotate function
|
|
155
|
-
* @param x {number}
|
|
156
|
-
* @param n {number}
|
|
157
|
-
* @private
|
|
158
|
-
*/
|
|
159
|
-
private static rot(x: bigint, n: bigint): bigint {
|
|
160
|
-
const mask = (1n << 64n) - 1n;
|
|
161
|
-
return ((x << n) | (x >> (64n - n))) & mask;
|
|
162
|
-
}
|
|
163
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Crypto Types
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Crypto methods async interface
|
|
12
|
-
*/
|
|
13
|
-
export interface ICryptoMethodAsync {
|
|
14
|
-
encrypt(decryptedData: Uint8Array): Promise<Uint8Array>;
|
|
15
|
-
decrypt(encryptedData: Uint8Array): Promise<Uint8Array>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Crypto methods sync interface
|
|
20
|
-
*/
|
|
21
|
-
export interface ICryptoMethodSync {
|
|
22
|
-
encryptSync(decryptedData: Uint8Array): Uint8Array;
|
|
23
|
-
decryptSync(encryptedData: Uint8Array): Uint8Array;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Cipher interface
|
|
28
|
-
*/
|
|
29
|
-
export interface ICipher extends ICryptoMethodAsync, ICryptoMethodSync{}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* KDF interface
|
|
33
|
-
*/
|
|
34
|
-
export interface IKDF {
|
|
35
|
-
derive(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, length: number): Promise<Uint8Array>;
|
|
36
|
-
deriveSync(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, length: number): Uint8Array;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* MAC interface
|
|
41
|
-
*/
|
|
42
|
-
export interface IMAC {
|
|
43
|
-
sign(data: Uint8Array, key: Uint8Array): Promise<Uint8Array>;
|
|
44
|
-
verify(data: Uint8Array, key: Uint8Array, tag: Uint8Array): Promise<boolean>;
|
|
45
|
-
signSync(data: Uint8Array, key: Uint8Array): Uint8Array;
|
|
46
|
-
verifySync(data: Uint8Array, key: Uint8Array, tag: Uint8Array): boolean;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Key exchange interface
|
|
51
|
-
*/
|
|
52
|
-
export interface IKeyExchange {
|
|
53
|
-
generateKeyPair(): Promise<ICryptoKeyPair>;
|
|
54
|
-
generateKeyPairSync(): ICryptoKeyPair;
|
|
55
|
-
encapsulate(publicKey: Uint8Array): Promise<ICryptoEncapsulated>;
|
|
56
|
-
encapsulateSync(publicKey: Uint8Array): ICryptoEncapsulated;
|
|
57
|
-
decapsulate(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>;
|
|
58
|
-
decapsulateSync(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Crypto key pair
|
|
63
|
-
*/
|
|
64
|
-
export interface ICryptoKeyPair {
|
|
65
|
-
publicKey : Uint8Array;
|
|
66
|
-
privateKey : Uint8Array;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Crypto encapsulated
|
|
71
|
-
*/
|
|
72
|
-
export interface ICryptoEncapsulated {
|
|
73
|
-
ciphertext: Uint8Array;
|
|
74
|
-
sharedSecret: Uint8Array;
|
|
75
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QuarkDash Crypto Utils
|
|
3
|
-
*
|
|
4
|
-
* @git https://github.com/devsdaddy/quarkdash
|
|
5
|
-
* @version 1.0.0
|
|
6
|
-
* @author Elijah Rastorguev
|
|
7
|
-
* @build 1000
|
|
8
|
-
* @website https://dev.to/devsdaddy
|
|
9
|
-
*/
|
|
10
|
-
import crypto from "crypto";
|
|
11
|
-
import {Shake256} from "./shake";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Crypto Utils Class
|
|
15
|
-
*/
|
|
16
|
-
export class QuarkDashUtils {
|
|
17
|
-
// Protected Constants
|
|
18
|
-
private static HEXChars : string = '0123456789abcdef';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Get Shake-256 result async
|
|
22
|
-
* @param data {Uint8Array} Bytes buffer
|
|
23
|
-
* @param len {number} Buffer length
|
|
24
|
-
* @return {Promise<Uint8Array>} Result buffer
|
|
25
|
-
*/
|
|
26
|
-
public static async shake256(data: Uint8Array, len: number): Promise<Uint8Array> {
|
|
27
|
-
return await Shake256.hash(data, len);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Get Shake-256 result sync
|
|
32
|
-
* @param data {Uint8Array} Bytes buffer
|
|
33
|
-
* @param len {number} Buffer length
|
|
34
|
-
* @return {Uint8Array} Result buffer
|
|
35
|
-
*/
|
|
36
|
-
public static shake256Sync(data: Uint8Array, len: number): Uint8Array {
|
|
37
|
-
return Shake256.hashSync(data, len);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Concat bytes
|
|
42
|
-
* @param arrays {Uint8Array|null|undefined} Input arrays for concat
|
|
43
|
-
* @returns {Uint8Array} Result buffer
|
|
44
|
-
*/
|
|
45
|
-
public static concatBytes(...arrays: (Uint8Array|null|undefined)[]): Uint8Array {
|
|
46
|
-
const valid = arrays.filter(a => a != null) as Uint8Array[];
|
|
47
|
-
const total = valid.reduce((s, a) => s + a.length, 0);
|
|
48
|
-
const res = new Uint8Array(total);
|
|
49
|
-
let pos = 0;
|
|
50
|
-
for (const a of valid) {
|
|
51
|
-
res.set(a, pos);
|
|
52
|
-
pos += a.length;
|
|
53
|
-
}
|
|
54
|
-
return res;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Coerce Array
|
|
59
|
-
* @param arg {any} Argument
|
|
60
|
-
* @param copy {any} Copy
|
|
61
|
-
* @protected
|
|
62
|
-
*/
|
|
63
|
-
public static coerceArray(arg : any, copy ? : any) : any {
|
|
64
|
-
let self = this
|
|
65
|
-
|
|
66
|
-
// ArrayBuffer view
|
|
67
|
-
if (arg.buffer && arg.name === 'Uint8Array') {
|
|
68
|
-
if (copy) {
|
|
69
|
-
if (arg.slice) {
|
|
70
|
-
arg = arg.slice()
|
|
71
|
-
} else {
|
|
72
|
-
arg = Array.prototype.slice.call(arg)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return arg
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// It's an array; check it is a valid representation of a byte
|
|
80
|
-
if (Array.isArray(arg)) {
|
|
81
|
-
if (!self.checkInts(arg)) {
|
|
82
|
-
throw new Error('Array contains invalid value: ' + arg)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return new Uint8Array(arg)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Something else, but behaves like an array (maybe a Buffer? Arguments?)
|
|
89
|
-
if (self.checkInt(arg.length) && self.checkInts(arg)) {
|
|
90
|
-
return new Uint8Array(arg)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
throw new Error('unsupported array-like object')
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Check if value is int
|
|
98
|
-
* @param value {any} Value
|
|
99
|
-
* @returns {boolean}
|
|
100
|
-
* @protected
|
|
101
|
-
*/
|
|
102
|
-
public static checkInt(value: any) : boolean {
|
|
103
|
-
return parseInt(value) === value
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Check Ints inside array
|
|
108
|
-
* @param arrayish {any} Array
|
|
109
|
-
* @returns {boolean} Any value is integer and between 0 and 255
|
|
110
|
-
* @protected
|
|
111
|
-
*/
|
|
112
|
-
public static checkInts(arrayish : any) : boolean {
|
|
113
|
-
let self = this
|
|
114
|
-
if (!self.checkInt(arrayish.length)) {
|
|
115
|
-
return false
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
for (let i = 0; i < arrayish.length; i++) {
|
|
119
|
-
if (!self.checkInt(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) {
|
|
120
|
-
return false
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return true
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Get random bytes
|
|
129
|
-
* @param len {number} buffer length
|
|
130
|
-
* @returns {Uint8Array} Random bytes buffer
|
|
131
|
-
*/
|
|
132
|
-
public static randomBytes(len: number): Uint8Array {
|
|
133
|
-
return crypto.getRandomValues(new Uint8Array(len));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Convert raw text to bytes array
|
|
138
|
-
* @param text {string} raw string
|
|
139
|
-
* @returns {any} bytes array
|
|
140
|
-
*/
|
|
141
|
-
public static textToBytes(text : string) : any {
|
|
142
|
-
let self = this
|
|
143
|
-
let result = [],
|
|
144
|
-
i = 0
|
|
145
|
-
text = encodeURI(text)
|
|
146
|
-
while (i < text.length) {
|
|
147
|
-
let c = text.charCodeAt(i++)
|
|
148
|
-
|
|
149
|
-
// if it is a % sign, encode the following 2 bytes as a hex value
|
|
150
|
-
if (c === 37) {
|
|
151
|
-
result.push(parseInt(text.substr(i, 2), 16))
|
|
152
|
-
i += 2
|
|
153
|
-
|
|
154
|
-
// otherwise, just the actual byte
|
|
155
|
-
} else {
|
|
156
|
-
result.push(c)
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return self.coerceArray(result)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Convert bytes array to raw string
|
|
165
|
-
* @param bytes {number[]|Uint8Array} Bytes array
|
|
166
|
-
* @returns {string} raw string
|
|
167
|
-
*/
|
|
168
|
-
public static bytesToText(bytes : number[] | Uint8Array) : string {
|
|
169
|
-
return new TextDecoder().decode(bytes as Uint8Array);
|
|
170
|
-
|
|
171
|
-
let result = [],
|
|
172
|
-
i = 0
|
|
173
|
-
|
|
174
|
-
while (i < bytes.length) {
|
|
175
|
-
let c = bytes[i]
|
|
176
|
-
|
|
177
|
-
if (c < 128) {
|
|
178
|
-
result.push(String.fromCharCode(c))
|
|
179
|
-
i++
|
|
180
|
-
} else if (c > 191 && c < 224) {
|
|
181
|
-
result.push(String.fromCharCode(((c & 0x1f) << 6) | (bytes[i + 1] & 0x3f)))
|
|
182
|
-
i += 2
|
|
183
|
-
} else {
|
|
184
|
-
result.push(
|
|
185
|
-
String.fromCharCode(
|
|
186
|
-
((c & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f),
|
|
187
|
-
),
|
|
188
|
-
)
|
|
189
|
-
i += 3
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return result.join('')
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Convert HEX string to bytes array
|
|
198
|
-
* @param text {string} HEX string
|
|
199
|
-
* @returns {number[]} bytes array
|
|
200
|
-
* @constructor
|
|
201
|
-
*/
|
|
202
|
-
public static HEXToBytes(text : string) : number[] {
|
|
203
|
-
let result = []
|
|
204
|
-
for (let i = 0; i < text.length; i += 2) {
|
|
205
|
-
result.push(parseInt(text.substr(i, 2), 16))
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return result
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Convert bytes array to HEX string
|
|
213
|
-
* @param bytes {number[]|Uint8Array} Bytes array
|
|
214
|
-
* @returns {string} HEX String
|
|
215
|
-
*/
|
|
216
|
-
public static bytesToHEX(bytes : number[] | Uint8Array) : string {
|
|
217
|
-
let self = this
|
|
218
|
-
let result = []
|
|
219
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
220
|
-
let v = bytes[i]
|
|
221
|
-
result.push(self.HEXChars[(v & 0xf0) >> 4] + self.HEXChars[v & 0x0f])
|
|
222
|
-
}
|
|
223
|
-
return result.join('')
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Constant time equal
|
|
228
|
-
* @param a {Uint8Array} first buffer
|
|
229
|
-
* @param b {Uint8Array} second buffer
|
|
230
|
-
* @returns {boolean} Equal or not
|
|
231
|
-
*/
|
|
232
|
-
public static constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
233
|
-
if (a.length !== b.length) return false;
|
|
234
|
-
let diff = 0;
|
|
235
|
-
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
236
|
-
return diff === 0;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Secure zero
|
|
241
|
-
* @param bytes {Uint8Array} bytes buffer
|
|
242
|
-
*/
|
|
243
|
-
public static secureZero(bytes: Uint8Array): void {
|
|
244
|
-
for (let i = 0; i < bytes.length; i++) bytes[i] = 0;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Read U32 from buffer
|
|
249
|
-
* @param arr {Uint8Array} buffer
|
|
250
|
-
* @param off {number} Offset
|
|
251
|
-
* @returns {number} U32
|
|
252
|
-
* @private
|
|
253
|
-
*/
|
|
254
|
-
public static readU32(arr:Uint8Array, off:number): number {
|
|
255
|
-
return (arr[off]|(arr[off+1]<<8)|(arr[off+2]<<16)|(arr[off+3]<<24))>>>0;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Write U32 to buffer
|
|
260
|
-
* @param v {number} U32
|
|
261
|
-
* @param arr {Uint8Array} Target buffer
|
|
262
|
-
* @param off {number} Offset
|
|
263
|
-
* @private
|
|
264
|
-
*/
|
|
265
|
-
public static writeU32(v:number, arr:Uint8Array, off:number){
|
|
266
|
-
arr[off]=v&0xFF; arr[off+1]=(v>>8)&0xFF; arr[off+2]=(v>>16)&0xFF; arr[off+3]=(v>>24)&0xFF;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Read Uint32 Value
|
|
271
|
-
* @param arr {Uint8Array} Bytes buffer
|
|
272
|
-
* @param off {number} Offset
|
|
273
|
-
* @returns {number} Uint32 Value
|
|
274
|
-
*/
|
|
275
|
-
public static readUint32(arr: Uint8Array, off: number): number {
|
|
276
|
-
return (arr[off] | (arr[off+1]<<8) | (arr[off+2]<<16) | (arr[off+3]<<24)) >>> 0;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Read Uint64 Value
|
|
281
|
-
* @param arr {Uint8Array} Bytes buffer
|
|
282
|
-
* @param off {number} offset
|
|
283
|
-
* @returns {bigint} Bigint
|
|
284
|
-
*/
|
|
285
|
-
public static readUint64(arr: Uint8Array, off: number): bigint {
|
|
286
|
-
let v = 0n;
|
|
287
|
-
for (let i = 0; i < 8; i++) v |= BigInt(arr[off+i]) << BigInt(i*8);
|
|
288
|
-
return v;
|
|
289
|
-
}
|
|
290
|
-
}
|
package/tsconfig.cjs.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"lib": ["ES2020", "DOM"],
|
|
7
|
-
"outDir": "./dist/esm",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": false,
|
|
14
|
-
"sourceMap": true
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*"],
|
|
17
|
-
"exclude": ["node_modules", "dist", "tests", "benchmarks"]
|
|
18
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|