@bsv/sdk 1.0.0 → 1.0.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.
Files changed (123) hide show
  1. package/README.md +25 -3
  2. package/package.json +9 -5
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  4. package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
  5. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
  6. package/CHANGELOG.md +0 -72
  7. package/CONTRIBUTING.md +0 -85
  8. package/ROADMAP.md +0 -3
  9. package/docs/getting-started/COMMONJS.md +0 -94
  10. package/docs/getting-started/REACT-TS.md +0 -131
  11. package/docs/getting-started/TS-NODE.md +0 -106
  12. package/docs/getting-started/VUE.md +0 -103
  13. package/jest.config.js +0 -6
  14. package/mod.ts +0 -8
  15. package/src/compat/BSM.ts +0 -51
  16. package/src/compat/ECIES.ts +0 -557
  17. package/src/compat/HD.ts +0 -348
  18. package/src/compat/Mnemonic.ts +0 -295
  19. package/src/compat/__tests/BSM.test.ts +0 -38
  20. package/src/compat/__tests/ECIES.test.ts +0 -90
  21. package/src/compat/__tests/HD.test.ts +0 -405
  22. package/src/compat/__tests/Mnemonic.test.ts +0 -177
  23. package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
  24. package/src/compat/bip-39-wordlist-en.ts +0 -2053
  25. package/src/compat/index.ts +0 -4
  26. package/src/messages/EncryptedMessage.ts +0 -70
  27. package/src/messages/SignedMessage.ts +0 -87
  28. package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
  29. package/src/messages/__tests/SignedMessage.test.ts +0 -53
  30. package/src/messages/index.ts +0 -2
  31. package/src/primitives/AESGCM.ts +0 -479
  32. package/src/primitives/BasePoint.ts +0 -21
  33. package/src/primitives/BigNumber.ts +0 -4619
  34. package/src/primitives/Curve.ts +0 -1163
  35. package/src/primitives/DRBG.ts +0 -102
  36. package/src/primitives/ECDSA.ts +0 -164
  37. package/src/primitives/Hash.ts +0 -1420
  38. package/src/primitives/JacobianPoint.ts +0 -410
  39. package/src/primitives/K256.ts +0 -116
  40. package/src/primitives/Mersenne.ts +0 -123
  41. package/src/primitives/MontgomoryMethod.ts +0 -160
  42. package/src/primitives/Point.ts +0 -852
  43. package/src/primitives/PrivateKey.ts +0 -195
  44. package/src/primitives/PublicKey.ts +0 -154
  45. package/src/primitives/Random.ts +0 -55
  46. package/src/primitives/ReductionContext.ts +0 -528
  47. package/src/primitives/Signature.ts +0 -235
  48. package/src/primitives/SymmetricKey.ts +0 -75
  49. package/src/primitives/TransactionSignature.ts +0 -189
  50. package/src/primitives/__tests/AESGCM.test.ts +0 -338
  51. package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
  52. package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
  53. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
  54. package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
  55. package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
  56. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
  57. package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
  58. package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
  59. package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
  60. package/src/primitives/__tests/Curve.unit.test.ts +0 -192
  61. package/src/primitives/__tests/DRBG.test.ts +0 -18
  62. package/src/primitives/__tests/DRBG.vectors.ts +0 -167
  63. package/src/primitives/__tests/ECDH.test.ts +0 -31
  64. package/src/primitives/__tests/ECDSA.test.ts +0 -58
  65. package/src/primitives/__tests/HMAC.test.ts +0 -59
  66. package/src/primitives/__tests/Hash.test.ts +0 -121
  67. package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
  68. package/src/primitives/__tests/PrivateKey.test.ts +0 -17
  69. package/src/primitives/__tests/PublicKey.test.ts +0 -66
  70. package/src/primitives/__tests/Random.test.ts +0 -14
  71. package/src/primitives/__tests/Reader.test.ts +0 -296
  72. package/src/primitives/__tests/ReductionContext.test.ts +0 -279
  73. package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
  74. package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
  75. package/src/primitives/__tests/Writer.test.ts +0 -198
  76. package/src/primitives/__tests/sighash.vectors.ts +0 -3503
  77. package/src/primitives/__tests/utils.test.ts +0 -108
  78. package/src/primitives/index.ts +0 -8
  79. package/src/primitives/utils.ts +0 -665
  80. package/src/script/LockingScript.ts +0 -30
  81. package/src/script/OP.ts +0 -219
  82. package/src/script/Script.ts +0 -426
  83. package/src/script/ScriptChunk.ts +0 -7
  84. package/src/script/ScriptTemplate.ts +0 -36
  85. package/src/script/Spend.ts +0 -1379
  86. package/src/script/UnlockingScript.ts +0 -30
  87. package/src/script/__tests/Script.test.ts +0 -369
  88. package/src/script/__tests/Spend.test.ts +0 -248
  89. package/src/script/__tests/script.invalid.vectors.ts +0 -925
  90. package/src/script/__tests/script.valid.vectors.ts +0 -1120
  91. package/src/script/__tests/scriptFromVector.ts +0 -42
  92. package/src/script/__tests/spend.valid.vectors.ts +0 -2288
  93. package/src/script/index.ts +0 -7
  94. package/src/script/templates/P2PKH.ts +0 -109
  95. package/src/script/templates/RPuzzle.ts +0 -140
  96. package/src/script/templates/index.ts +0 -2
  97. package/src/transaction/Broadcaster.ts +0 -42
  98. package/src/transaction/ChainTracker.ts +0 -22
  99. package/src/transaction/FeeModel.ts +0 -13
  100. package/src/transaction/MerklePath.ts +0 -259
  101. package/src/transaction/Transaction.ts +0 -602
  102. package/src/transaction/TransactionInput.ts +0 -63
  103. package/src/transaction/TransactionOutput.ts +0 -37
  104. package/src/transaction/__tests/MerklePath.test.ts +0 -181
  105. package/src/transaction/__tests/Transaction.test.ts +0 -413
  106. package/src/transaction/__tests/bigtx.vectors.ts +0 -4
  107. package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
  108. package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
  109. package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
  110. package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
  111. package/src/transaction/broadcasters/ARC.ts +0 -106
  112. package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
  113. package/src/transaction/broadcasters/index.ts +0 -1
  114. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
  115. package/src/transaction/fee-models/index.ts +0 -1
  116. package/src/transaction/index.ts +0 -6
  117. package/ts2md.json +0 -5
  118. package/tsconfig.base.json +0 -26
  119. package/tsconfig.cjs.json +0 -11
  120. package/tsconfig.eslint.json +0 -12
  121. package/tsconfig.esm.json +0 -9
  122. package/tsconfig.json +0 -17
  123. package/tsconfig.types.json +0 -11
@@ -1,1420 +0,0 @@
1
- const assert = (
2
- expression: unknown,
3
- message: string = 'Hash assertion failed'
4
- ): void => {
5
- if (!(expression as boolean)) {
6
- throw new Error(message)
7
- }
8
- }
9
-
10
- /**
11
- * The BaseHash class is an abstract base class for cryptographic hash functions.
12
- * It provides a common structure and functionality for hash function classes.
13
- *
14
- * @class BaseHash
15
- *
16
- * @property pending - Stores partially processed message segments.
17
- * @property pendingTotal - The total number of characters that are being stored in `pending`
18
- * @property blockSize - The size of each block to processed.
19
- * @property outSize - The size of the final hash output.
20
- * @property endian - The endianness used during processing, can either be 'big' or 'little'.
21
- * @property _delta8 - The block size divided by 8, useful in various computations.
22
- * @property _delta32 - The block size divided by 32, useful in various computations.
23
- * @property padLength - The length of padding to be added to finalize the computation.
24
- * @property hmacStrength - The HMAC strength value.
25
- *
26
- * @param blockSize - The size of the block to be hashed.
27
- * @param outSize - The size of the resulting hash.
28
- * @param hmacStrength - The strength of the HMAC.
29
- * @param padLength - The length of the padding to be added.
30
- *
31
- * @example
32
- * Sub-classes would extend this base BaseHash class like:
33
- * class RIPEMD160 extends BaseHash {
34
- * constructor () {
35
- * super(512, 160, 192, 64);
36
- * // ...
37
- * }
38
- * // ...
39
- * }
40
- */
41
- abstract class BaseHash {
42
- pending: number[] | null
43
- pendingTotal: number
44
- blockSize: number
45
- outSize: number
46
- endian: 'big' | 'little'
47
- _delta8: number
48
- _delta32: number
49
- padLength: number
50
- hmacStrength: number
51
-
52
- constructor(
53
- blockSize: number, outSize: number, hmacStrength: number, padLength: number
54
- ) {
55
- this.pending = null
56
- this.pendingTotal = 0
57
- this.blockSize = blockSize
58
- this.outSize = outSize
59
- this.hmacStrength = hmacStrength
60
- this.padLength = padLength / 8
61
- this.endian = 'big'
62
-
63
- this._delta8 = this.blockSize / 8
64
- this._delta32 = this.blockSize / 32
65
- }
66
-
67
- _update(msg: number[], start: number): void {
68
- throw new Error('Not implemented')
69
- }
70
-
71
- _digest(enc?: 'hex'): number[] | string {
72
- throw new Error('Not implemented')
73
- }
74
-
75
- /**
76
- * Converts the input message into an array, pads it, and joins into 32bit blocks.
77
- * If there is enough data, it tries updating the hash computation.
78
- *
79
- * @method update
80
- * @param msg - The message segment to include in the hashing computation.
81
- * @param enc - The encoding of the message. If 'hex', the string will be treated as such, 'utf8' otherwise.
82
- *
83
- * @returns Returns the instance of the object for chaining.
84
- *
85
- * @example
86
- * sha256.update('Hello World', 'utf8');
87
- */
88
- update(msg: number[] | string, enc?: 'hex'): this {
89
- // Convert message to array, pad it, and join into 32bit blocks
90
- msg = toArray(msg, enc)
91
- if (this.pending == null) {
92
- this.pending = msg
93
- } else {
94
- this.pending = this.pending.concat(msg)
95
- }
96
- this.pendingTotal += msg.length
97
-
98
- // Enough data, try updating
99
- if (this.pending.length >= this._delta8) {
100
- msg = this.pending
101
-
102
- // Process pending data in blocks
103
- const r = msg.length % this._delta8
104
- this.pending = msg.slice(msg.length - r, msg.length)
105
- if (this.pending.length === 0) { this.pending = null }
106
-
107
- msg = join32(msg, 0, msg.length - r, this.endian)
108
- for (let i = 0; i < msg.length; i += this._delta32) {
109
- this._update(msg, i)
110
- }
111
- }
112
-
113
- return this
114
- }
115
-
116
- /**
117
- * Finalizes the hash computation and returns the hash value/result.
118
- *
119
- * @method digest
120
- * @param enc - The encoding of the final hash. If 'hex' then a hex string will be provided, otherwise an array of numbers.
121
- *
122
- * @returns Returns the final hash value.
123
- *
124
- * @example
125
- * const hash = sha256.digest('hex');
126
- */
127
- digest(enc?: 'hex'): number[] | string {
128
- this.update(this._pad())
129
- assert(this.pending === null)
130
-
131
- return this._digest(enc)
132
- };
133
-
134
- /**
135
- * [Private Method] Used internally to prepare the padding for the final stage of the hash computation.
136
- *
137
- * @method _pad
138
- * @private
139
- *
140
- * @returns Returns an array denoting the padding.
141
- */
142
- private _pad(): number[] { //
143
- let len = this.pendingTotal
144
- const bytes = this._delta8
145
- const k = bytes - ((len + this.padLength) % bytes)
146
- const res = new Array(k + this.padLength)
147
- res[0] = 0x80
148
- let i
149
- for (i = 1; i < k; i++) { res[i] = 0 }
150
-
151
- // Append length
152
- len <<= 3
153
- let t
154
- if (this.endian === 'big') {
155
- for (t = 8; t < this.padLength; t++) { res[i++] = 0 }
156
-
157
- res[i++] = 0
158
- res[i++] = 0
159
- res[i++] = 0
160
- res[i++] = 0
161
- res[i++] = (len >>> 24) & 0xff
162
- res[i++] = (len >>> 16) & 0xff
163
- res[i++] = (len >>> 8) & 0xff
164
- res[i++] = len & 0xff
165
- } else {
166
- res[i++] = len & 0xff
167
- res[i++] = (len >>> 8) & 0xff
168
- res[i++] = (len >>> 16) & 0xff
169
- res[i++] = (len >>> 24) & 0xff
170
- res[i++] = 0
171
- res[i++] = 0
172
- res[i++] = 0
173
- res[i++] = 0
174
-
175
- for (t = 8; t < this.padLength; t++) { res[i++] = 0 }
176
- }
177
-
178
- return res
179
- }
180
- }
181
-
182
- function isSurrogatePair(msg: string, i: number): boolean {
183
- if ((msg.charCodeAt(i) & 0xFC00) !== 0xD800) {
184
- return false
185
- }
186
- if (i < 0 || i + 1 >= msg.length) {
187
- return false
188
- }
189
- return (msg.charCodeAt(i + 1) & 0xFC00) === 0xDC00
190
- }
191
-
192
- export function toArray(msg: number[] | string, enc?: 'hex'): number[] {
193
- if (Array.isArray(msg)) { return msg.slice() }
194
- if (!(msg as unknown as boolean)) { return [] }
195
- const res = []
196
- if (typeof msg === 'string') {
197
- if (enc !== 'hex') {
198
- // Inspired by stringToUtf8ByteArray() in closure-library by Google
199
- // https://github.com/google/closure-library/blob/8598d87242af59aac233270742c8984e2b2bdbe0/closure/goog/crypt/crypt.js#L117-L143
200
- // Apache License 2.0
201
- // https://github.com/google/closure-library/blob/master/LICENSE
202
- let p = 0
203
- for (let i = 0; i < msg.length; i++) {
204
- let c = msg.charCodeAt(i)
205
- if (c < 128) {
206
- res[p++] = c
207
- } else if (c < 2048) {
208
- res[p++] = (c >> 6) | 192
209
- res[p++] = (c & 63) | 128
210
- } else if (isSurrogatePair(msg, i)) {
211
- c = 0x10000 + ((c & 0x03FF) << 10) + (msg.charCodeAt(++i) & 0x03FF)
212
- res[p++] = (c >> 18) | 240
213
- res[p++] = ((c >> 12) & 63) | 128
214
- res[p++] = ((c >> 6) & 63) | 128
215
- res[p++] = (c & 63) | 128
216
- } else {
217
- res[p++] = (c >> 12) | 224
218
- res[p++] = ((c >> 6) & 63) | 128
219
- res[p++] = (c & 63) | 128
220
- }
221
- }
222
- } else {
223
- msg = msg.replace(/[^a-z0-9]+/ig, '')
224
- if (msg.length % 2 !== 0) {
225
- msg = '0' + msg
226
- }
227
- for (let i = 0; i < msg.length; i += 2) {
228
- res.push(parseInt(msg[i] + msg[i + 1], 16))
229
- }
230
- }
231
- } else {
232
- msg = msg as number[]
233
- for (let i = 0; i < msg.length; i++) { res[i] = msg[i] | 0 }
234
- }
235
- return res
236
- }
237
-
238
- function htonl(w: number): number {
239
- const res = (w >>> 24) |
240
- ((w >>> 8) & 0xff00) |
241
- ((w << 8) & 0xff0000) |
242
- ((w & 0xff) << 24)
243
- return res >>> 0
244
- }
245
-
246
- function toHex32(msg: number[], endian?: 'little' | 'big'): string {
247
- let res = ''
248
- for (let i = 0; i < msg.length; i++) {
249
- let w = msg[i]
250
- if (endian === 'little') { w = htonl(w) }
251
- res += zero8(w.toString(16))
252
- }
253
- return res
254
- }
255
-
256
- function zero8(word: string): string {
257
- if (word.length === 7) {
258
- return '0' + word
259
- } else if (word.length === 6) {
260
- return '00' + word
261
- } else if (word.length === 5) {
262
- return '000' + word
263
- } else if (word.length === 4) {
264
- return '0000' + word
265
- } else if (word.length === 3) {
266
- return '00000' + word
267
- } else if (word.length === 2) {
268
- return '000000' + word
269
- } else if (word.length === 1) {
270
- return '0000000' + word
271
- } else {
272
- return word
273
- }
274
- }
275
-
276
- function join32(msg, start, end, endian): number[] {
277
- const len = end - start
278
- assert(len % 4 === 0)
279
- const res = new Array(len / 4)
280
- for (let i = 0, k: number = start; i < res.length; i++, k += 4) {
281
- let w
282
- if (endian === 'big') {
283
- w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3]
284
- } else {
285
- w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k]
286
- }
287
- res[i] = w >>> 0
288
- }
289
- return res
290
- }
291
-
292
- function split32(msg: number[], endian: 'big' | 'little'): number[] {
293
- const res = new Array(msg.length * 4)
294
- for (let i = 0, k = 0; i < msg.length; i++, k += 4) {
295
- const m = msg[i]
296
- if (endian === 'big') {
297
- res[k] = m >>> 24
298
- res[k + 1] = (m >>> 16) & 0xff
299
- res[k + 2] = (m >>> 8) & 0xff
300
- res[k + 3] = m & 0xff
301
- } else {
302
- res[k + 3] = m >>> 24
303
- res[k + 2] = (m >>> 16) & 0xff
304
- res[k + 1] = (m >>> 8) & 0xff
305
- res[k] = m & 0xff
306
- }
307
- }
308
- return res
309
- }
310
-
311
- function rotr32(w: number, b: number): number {
312
- return (w >>> b) | (w << (32 - b))
313
- }
314
-
315
- function rotl32(w: number, b: number): number {
316
- return (w << b) | (w >>> (32 - b))
317
- }
318
-
319
- function sum32(a: number, b: number): number {
320
- return (a + b) >>> 0
321
- }
322
-
323
- function SUM32_3(a: number, b: number, c: number): number {
324
- return (a + b + c) >>> 0
325
- }
326
-
327
- function SUM32_4(a: number, b: number, c: number, d: number): number {
328
- return (a + b + c + d) >>> 0
329
- }
330
-
331
- function SUM32_5(
332
- a: number, b: number, c: number, d: number, e: number
333
- ): number {
334
- return (a + b + c + d + e) >>> 0
335
- }
336
-
337
- function FT_1(s, x, y, z): number {
338
- if (s === 0) { return ch32(x, y, z) }
339
- if (s === 1 || s === 3) { return p32(x, y, z) }
340
- if (s === 2) { return maj32(x, y, z) }
341
- }
342
-
343
- function ch32(x, y, z): number {
344
- return (x & y) ^ ((~x) & z)
345
- }
346
-
347
- function maj32(x, y, z): number {
348
- return (x & y) ^ (x & z) ^ (y & z)
349
- }
350
-
351
- function p32(x, y, z): number {
352
- return x ^ y ^ z
353
- }
354
-
355
- function S0_256(x): number {
356
- return rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22)
357
- }
358
-
359
- function S1_256(x): number {
360
- return rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25)
361
- }
362
-
363
- function G0_256(x): number {
364
- return rotr32(x, 7) ^ rotr32(x, 18) ^ (x >>> 3)
365
- }
366
-
367
- function G1_256(x): number {
368
- return rotr32(x, 17) ^ rotr32(x, 19) ^ (x >>> 10)
369
- }
370
-
371
- const r = [
372
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
373
- 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
374
- 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
375
- 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
376
- 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
377
- ]
378
-
379
- const rh = [
380
- 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
381
- 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
382
- 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
383
- 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
384
- 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
385
- ]
386
-
387
- const s = [
388
- 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
389
- 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
390
- 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
391
- 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
392
- 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
393
- ]
394
-
395
- const sh = [
396
- 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
397
- 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
398
- 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
399
- 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
400
- 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
401
- ]
402
-
403
- function f(j, x, y, z): number {
404
- if (j <= 15) { return x ^ y ^ z } else if (j <= 31) { return (x & y) | ((~x) & z) } else if (j <= 47) { return (x | (~y)) ^ z } else if (j <= 63) { return (x & z) | (y & (~z)) } else { return x ^ (y | (~z)) }
405
- }
406
-
407
- function K(j): number {
408
- if (j <= 15) { return 0x00000000 } else if (j <= 31) { return 0x5a827999 } else if (j <= 47) { return 0x6ed9eba1 } else if (j <= 63) { return 0x8f1bbcdc } else { return 0xa953fd4e }
409
- }
410
-
411
- function Kh(j): number {
412
- if (j <= 15) { return 0x50a28be6 } else if (j <= 31) { return 0x5c4dd124 } else if (j <= 47) { return 0x6d703ef3 } else if (j <= 63) { return 0x7a6d76e9 } else { return 0x00000000 }
413
- }
414
-
415
- function sum64(buf, pos, ah, al) {
416
- const bh = buf[pos];
417
- const bl = buf[pos + 1];
418
-
419
- const lo = (al + bl) >>> 0;
420
- const hi = (lo < al ? 1 : 0) + ah + bh;
421
- buf[pos] = hi >>> 0;
422
- buf[pos + 1] = lo;
423
- }
424
-
425
- function sum64_hi(ah, al, bh, bl) {
426
- const lo = (al + bl) >>> 0;
427
- const hi = (lo < al ? 1 : 0) + ah + bh;
428
- return hi >>> 0;
429
- }
430
-
431
- function sum64_lo(ah, al, bh, bl) {
432
- const lo = al + bl;
433
- return lo >>> 0;
434
- }
435
-
436
- function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) {
437
- let carry = 0;
438
- let lo = al;
439
- lo = (lo + bl) >>> 0;
440
- carry += lo < al ? 1 : 0;
441
- lo = (lo + cl) >>> 0;
442
- carry += lo < cl ? 1 : 0;
443
- lo = (lo + dl) >>> 0;
444
- carry += lo < dl ? 1 : 0;
445
-
446
- const hi = ah + bh + ch + dh + carry;
447
- return hi >>> 0;
448
- }
449
-
450
- function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) {
451
- const lo = al + bl + cl + dl;
452
- return lo >>> 0;
453
- }
454
-
455
- function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
456
- let carry = 0;
457
- let lo = al;
458
- lo = (lo + bl) >>> 0;
459
- carry += lo < al ? 1 : 0;
460
- lo = (lo + cl) >>> 0;
461
- carry += lo < cl ? 1 : 0;
462
- lo = (lo + dl) >>> 0;
463
- carry += lo < dl ? 1 : 0;
464
- lo = (lo + el) >>> 0;
465
- carry += lo < el ? 1 : 0;
466
-
467
- const hi = ah + bh + ch + dh + eh + carry;
468
- return hi >>> 0;
469
- }
470
-
471
- function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
472
- const lo = al + bl + cl + dl + el;
473
-
474
- return lo >>> 0;
475
- }
476
-
477
- function rotr64_hi(ah, al, num) {
478
- const r = (al << (32 - num)) | (ah >>> num);
479
- return r >>> 0;
480
- }
481
-
482
- function rotr64_lo(ah, al, num) {
483
- const r = (ah << (32 - num)) | (al >>> num);
484
- return r >>> 0;
485
- }
486
-
487
- function shr64_hi(ah, al, num) {
488
- return ah >>> num;
489
- }
490
-
491
- function shr64_lo(ah, al, num) {
492
- const r = (ah << (32 - num)) | (al >>> num);
493
- return r >>> 0;
494
- }
495
-
496
- /**
497
- * An implementation of RIPEMD160 cryptographic hash function. Extends the BaseHash class.
498
- * It provides a way to compute a 'digest' for any kind of input data; transforming the data
499
- * into a unique output of fixed size. The output is deterministic; it will always be
500
- * the same for the same input.
501
- *
502
- * @class RIPEMD160
503
- * @param None
504
- *
505
- * @constructor
506
- * Use the RIPEMD160 constructor to create an instance of RIPEMD160 hash function.
507
- *
508
- * @example
509
- * const ripemd160 = new RIPEMD160();
510
- *
511
- * @property h - Array that is updated iteratively as part of hashing computation.
512
- */
513
- export class RIPEMD160 extends BaseHash {
514
- h: number[]
515
-
516
- constructor() {
517
- super(512, 160, 192, 64)
518
- this.endian = 'little'
519
-
520
- this.h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]
521
- this.endian = 'little'
522
- }
523
-
524
- _update(msg: number[], start: number): void {
525
- let A = this.h[0]
526
- let B = this.h[1]
527
- let C = this.h[2]
528
- let D = this.h[3]
529
- let E = this.h[4]
530
- let Ah = A
531
- let Bh = B
532
- let Ch = C
533
- let Dh = D
534
- let Eh = E
535
- let T
536
- for (let j = 0; j < 80; j++) {
537
- T = sum32(
538
- rotl32(
539
- SUM32_4(A, f(j, B, C, D), msg[r[j] + start], K(j)),
540
- s[j]),
541
- E)
542
- A = E
543
- E = D
544
- D = rotl32(C, 10)
545
- C = B
546
- B = T
547
- T = sum32(
548
- rotl32(
549
- SUM32_4(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)),
550
- sh[j]),
551
- Eh)
552
- Ah = Eh
553
- Eh = Dh
554
- Dh = rotl32(Ch, 10)
555
- Ch = Bh
556
- Bh = T
557
- }
558
- T = SUM32_3(this.h[1], C, Dh)
559
- this.h[1] = SUM32_3(this.h[2], D, Eh)
560
- this.h[2] = SUM32_3(this.h[3], E, Ah)
561
- this.h[3] = SUM32_3(this.h[4], A, Bh)
562
- this.h[4] = SUM32_3(this.h[0], B, Ch)
563
- this.h[0] = T
564
- }
565
-
566
- _digest(enc?: 'hex'): string | number[] {
567
- if (enc === 'hex') {
568
- return toHex32(this.h, 'little')
569
- } else {
570
- return split32(this.h, 'little')
571
- }
572
- }
573
- }
574
-
575
- /**
576
- * An implementation of SHA256 cryptographic hash function. Extends the BaseHash class.
577
- * It provides a way to compute a 'digest' for any kind of input data; transforming the data
578
- * into a unique output of fixed size. The output is deterministic; it will always be
579
- * the same for the same input.
580
- *
581
- * @class SHA256
582
- * @param None
583
- *
584
- * @constructor
585
- * Use the SHA256 constructor to create an instance of SHA256 hash function.
586
- *
587
- * @example
588
- * const sha256 = new SHA256();
589
- *
590
- * @property h - The initial hash constants
591
- * @property W - Provides a way to recycle usage of the array memory.
592
- * @property k - The round constants used for each round of SHA-256
593
- */
594
- export class SHA256 extends BaseHash {
595
- h: number[]
596
- W: number[]
597
- k: number[]
598
-
599
- constructor() {
600
- super(512, 256, 192, 64)
601
- this.h = [
602
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
603
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
604
- ]
605
- this.k = [
606
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
607
- 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
608
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
609
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
610
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
611
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
612
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
613
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
614
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
615
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
616
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
617
- 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
618
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
619
- 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
620
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
621
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
622
- ]
623
- this.W = new Array(64)
624
- }
625
-
626
- _update(msg: number[], start?: number): void {
627
- const W = this.W
628
-
629
- let i: number
630
- for (i = 0; i < 16; i++) {
631
- W[i] = msg[start + i]
632
- }
633
- for (; i < W.length; i++) {
634
- W[i] = SUM32_4(G1_256(W[i - 2]), W[i - 7], G0_256(W[i - 15]), W[i - 16])
635
- }
636
-
637
- let a = this.h[0]
638
- let b = this.h[1]
639
- let c = this.h[2]
640
- let d = this.h[3]
641
- let e = this.h[4]
642
- let f = this.h[5]
643
- let g = this.h[6]
644
- let h = this.h[7]
645
-
646
- assert(this.k.length === W.length)
647
- for (i = 0; i < W.length; i++) {
648
- const T1 = SUM32_5(h, S1_256(e), ch32(e, f, g), this.k[i], W[i])
649
- const T2 = sum32(S0_256(a), maj32(a, b, c))
650
- h = g
651
- g = f
652
- f = e
653
- e = sum32(d, T1)
654
- d = c
655
- c = b
656
- b = a
657
- a = sum32(T1, T2)
658
- }
659
-
660
- this.h[0] = sum32(this.h[0], a)
661
- this.h[1] = sum32(this.h[1], b)
662
- this.h[2] = sum32(this.h[2], c)
663
- this.h[3] = sum32(this.h[3], d)
664
- this.h[4] = sum32(this.h[4], e)
665
- this.h[5] = sum32(this.h[5], f)
666
- this.h[6] = sum32(this.h[6], g)
667
- this.h[7] = sum32(this.h[7], h)
668
- };
669
-
670
- _digest(enc?: 'hex'): number[] | string {
671
- if (enc === 'hex') {
672
- return toHex32(this.h, 'big')
673
- } else {
674
- return split32(this.h, 'big')
675
- }
676
- }
677
- }
678
-
679
- /**
680
- * An implementation of SHA1 cryptographic hash function. Extends the BaseHash class.
681
- * It provides a way to compute a 'digest' for any kind of input data; transforming the data
682
- * into a unique output of fixed size. The output is deterministic; it will always be
683
- * the same for the same input.
684
- *
685
- * @class SHA1
686
- * @param None
687
- *
688
- * @constructor
689
- * Use the SHA1 constructor to create an instance of SHA1 hash function.
690
- *
691
- * @example
692
- * const sha1 = new SHA1();
693
- *
694
- * @property h - The initial hash constants.
695
- * @property W - Provides a way to recycle usage of the array memory.
696
- * @property k - The round constants used for each round of SHA-1.
697
- */
698
- export class SHA1 extends BaseHash {
699
- h: number[]
700
- W: number[]
701
- k: number[]
702
-
703
- constructor() {
704
- super(512, 160, 80, 64)
705
- this.k = [
706
- 0x5A827999, 0x6ED9EBA1,
707
- 0x8F1BBCDC, 0xCA62C1D6
708
- ]
709
- this.h = [
710
- 0x67452301, 0xefcdab89, 0x98badcfe,
711
- 0x10325476, 0xc3d2e1f0]
712
- this.W = new Array(80)
713
- }
714
-
715
- _update(msg: number[], start?: number): void {
716
- const W = this.W
717
-
718
- let i: number
719
- for (i = 0; i < 16; i++) { W[i] = msg[start + i] }
720
-
721
- for (; i < W.length; i++) {
722
- W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1)
723
- }
724
-
725
- let a = this.h[0]
726
- let b = this.h[1]
727
- let c = this.h[2]
728
- let d = this.h[3]
729
- let e = this.h[4]
730
-
731
- for (i = 0; i < W.length; i++) {
732
- const s = ~~(i / 20)
733
- const t = SUM32_5(rotl32(a, 5), FT_1(s, b, c, d), e, W[i], this.k[s])
734
- e = d
735
- d = c
736
- c = rotl32(b, 30)
737
- b = a
738
- a = t
739
- }
740
-
741
- this.h[0] = sum32(this.h[0], a)
742
- this.h[1] = sum32(this.h[1], b)
743
- this.h[2] = sum32(this.h[2], c)
744
- this.h[3] = sum32(this.h[3], d)
745
- this.h[4] = sum32(this.h[4], e)
746
- }
747
-
748
- _digest(enc?: 'hex'): number[] | string {
749
- if (enc === 'hex') {
750
- return toHex32(this.h, 'big')
751
- } else {
752
- return split32(this.h, 'big')
753
- }
754
- }
755
- }
756
-
757
- /**
758
- * An implementation of SHA512 cryptographic hash function. Extends the BaseHash class.
759
- * It provides a way to compute a 'digest' for any kind of input data; transforming the data
760
- * into a unique output of fixed size. The output is deterministic; it will always be
761
- * the same for the same input.
762
- *
763
- * @class SHA512
764
- * @param None
765
- *
766
- * @constructor
767
- * Use the SHA512 constructor to create an instance of SHA512 hash function.
768
- *
769
- * @example
770
- * const sha512 = new SHA512();
771
- *
772
- * @property h - The initial hash constants.
773
- * @property W - Provides a way to recycle usage of the array memory.
774
- * @property k - The round constants used for each round of SHA-512.
775
- */
776
- export class SHA512 extends BaseHash {
777
- h: number[]
778
- W: number[]
779
- k: number[]
780
-
781
- constructor() {
782
- super(1024, 512, 192, 128)
783
- this.h = [
784
- 0x6a09e667, 0xf3bcc908,
785
- 0xbb67ae85, 0x84caa73b,
786
- 0x3c6ef372, 0xfe94f82b,
787
- 0xa54ff53a, 0x5f1d36f1,
788
- 0x510e527f, 0xade682d1,
789
- 0x9b05688c, 0x2b3e6c1f,
790
- 0x1f83d9ab, 0xfb41bd6b,
791
- 0x5be0cd19, 0x137e2179]
792
- this.k = [
793
- 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
794
- 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
795
- 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
796
- 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
797
- 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
798
- 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
799
- 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
800
- 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
801
- 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
802
- 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
803
- 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
804
- 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
805
- 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
806
- 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
807
- 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
808
- 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
809
- 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
810
- 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
811
- 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
812
- 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
813
- 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
814
- 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
815
- 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
816
- 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
817
- 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
818
- 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
819
- 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
820
- 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
821
- 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
822
- 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
823
- 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
824
- 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
825
- 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
826
- 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
827
- 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
828
- 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
829
- 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
830
- 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
831
- 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
832
- 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
833
- ]
834
- this.W = new Array(160)
835
- }
836
-
837
-
838
- _prepareBlock(msg, start) {
839
- const W = this.W;
840
-
841
- // 32 x 32bit words
842
- let i
843
- for (i = 0; i < 32; i++)
844
- W[i] = msg[start + i];
845
- for (; i < W.length; i += 2) {
846
- const c0_hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
847
- const c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
848
- const c1_hi = W[i - 14]; // i - 7
849
- const c1_lo = W[i - 13];
850
- const c2_hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
851
- const c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
852
- const c3_hi = W[i - 32]; // i - 16
853
- const c3_lo = W[i - 31];
854
-
855
- W[i] = sum64_4_hi(
856
- c0_hi, c0_lo,
857
- c1_hi, c1_lo,
858
- c2_hi, c2_lo,
859
- c3_hi, c3_lo);
860
- W[i + 1] = sum64_4_lo(
861
- c0_hi, c0_lo,
862
- c1_hi, c1_lo,
863
- c2_hi, c2_lo,
864
- c3_hi, c3_lo);
865
- }
866
- }
867
-
868
- _update(msg, start) {
869
- this._prepareBlock(msg, start);
870
-
871
- const W = this.W;
872
-
873
- let ah = this.h[0];
874
- let al = this.h[1];
875
- let bh = this.h[2];
876
- let bl = this.h[3];
877
- let ch = this.h[4];
878
- let cl = this.h[5];
879
- let dh = this.h[6];
880
- let dl = this.h[7];
881
- let eh = this.h[8];
882
- let el = this.h[9];
883
- let fh = this.h[10];
884
- let fl = this.h[11];
885
- let gh = this.h[12];
886
- let gl = this.h[13];
887
- let hh = this.h[14];
888
- let hl = this.h[15];
889
-
890
- assert(this.k.length === W.length);
891
- for (let i = 0; i < W.length; i += 2) {
892
- let c0_hi = hh;
893
- let c0_lo = hl;
894
- let c1_hi = s1_512_hi(eh, el);
895
- let c1_lo = s1_512_lo(eh, el);
896
- const c2_hi = ch64_hi(eh, el, fh, fl, gh, gl);
897
- const c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
898
- const c3_hi = this.k[i];
899
- const c3_lo = this.k[i + 1];
900
- const c4_hi = W[i];
901
- const c4_lo = W[i + 1];
902
-
903
- const T1_hi = sum64_5_hi(
904
- c0_hi, c0_lo,
905
- c1_hi, c1_lo,
906
- c2_hi, c2_lo,
907
- c3_hi, c3_lo,
908
- c4_hi, c4_lo);
909
- const T1_lo = sum64_5_lo(
910
- c0_hi, c0_lo,
911
- c1_hi, c1_lo,
912
- c2_hi, c2_lo,
913
- c3_hi, c3_lo,
914
- c4_hi, c4_lo);
915
-
916
- c0_hi = s0_512_hi(ah, al);
917
- c0_lo = s0_512_lo(ah, al);
918
- c1_hi = maj64_hi(ah, al, bh, bl, ch, cl);
919
- c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
920
-
921
- const T2_hi = sum64_hi(c0_hi, c0_lo, c1_hi, c1_lo);
922
- const T2_lo = sum64_lo(c0_hi, c0_lo, c1_hi, c1_lo);
923
-
924
- hh = gh;
925
- hl = gl;
926
-
927
- gh = fh;
928
- gl = fl;
929
-
930
- fh = eh;
931
- fl = el;
932
-
933
- eh = sum64_hi(dh, dl, T1_hi, T1_lo);
934
- el = sum64_lo(dl, dl, T1_hi, T1_lo);
935
-
936
- dh = ch;
937
- dl = cl;
938
-
939
- ch = bh;
940
- cl = bl;
941
-
942
- bh = ah;
943
- bl = al;
944
-
945
- ah = sum64_hi(T1_hi, T1_lo, T2_hi, T2_lo);
946
- al = sum64_lo(T1_hi, T1_lo, T2_hi, T2_lo);
947
- }
948
-
949
- sum64(this.h, 0, ah, al);
950
- sum64(this.h, 2, bh, bl);
951
- sum64(this.h, 4, ch, cl);
952
- sum64(this.h, 6, dh, dl);
953
- sum64(this.h, 8, eh, el);
954
- sum64(this.h, 10, fh, fl);
955
- sum64(this.h, 12, gh, gl);
956
- sum64(this.h, 14, hh, hl);
957
- }
958
-
959
- _digest(enc) {
960
- if (enc === 'hex')
961
- return toHex32(this.h, 'big');
962
- else
963
- return split32(this.h, 'big');
964
- }
965
- }
966
-
967
- function ch64_hi(xh, xl, yh, yl, zh, zl) {
968
- let r = (xh & yh) ^ ((~xh) & zh);
969
- if (r < 0)
970
- r += 0x100000000;
971
- return r;
972
- }
973
-
974
- function ch64_lo(xh, xl, yh, yl, zh, zl) {
975
- let r = (xl & yl) ^ ((~xl) & zl);
976
- if (r < 0)
977
- r += 0x100000000;
978
- return r;
979
- }
980
-
981
- function maj64_hi(xh, xl, yh, yl, zh, zl) {
982
- let r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
983
- if (r < 0)
984
- r += 0x100000000;
985
- return r;
986
- }
987
-
988
- function maj64_lo(xh, xl, yh, yl, zh, zl) {
989
- let r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
990
- if (r < 0)
991
- r += 0x100000000;
992
- return r;
993
- }
994
-
995
- function s0_512_hi(xh, xl) {
996
- const c0_hi = rotr64_hi(xh, xl, 28);
997
- const c1_hi = rotr64_hi(xl, xh, 2); // 34
998
- const c2_hi = rotr64_hi(xl, xh, 7); // 39
999
-
1000
- let r = c0_hi ^ c1_hi ^ c2_hi;
1001
- if (r < 0)
1002
- r += 0x100000000;
1003
- return r;
1004
- }
1005
-
1006
- function s0_512_lo(xh, xl) {
1007
- const c0_lo = rotr64_lo(xh, xl, 28);
1008
- const c1_lo = rotr64_lo(xl, xh, 2); // 34
1009
- const c2_lo = rotr64_lo(xl, xh, 7); // 39
1010
-
1011
- let r = c0_lo ^ c1_lo ^ c2_lo;
1012
- if (r < 0)
1013
- r += 0x100000000;
1014
- return r;
1015
- }
1016
-
1017
- function s1_512_hi(xh, xl) {
1018
- const c0_hi = rotr64_hi(xh, xl, 14);
1019
- const c1_hi = rotr64_hi(xh, xl, 18);
1020
- const c2_hi = rotr64_hi(xl, xh, 9); // 41
1021
-
1022
- let r = c0_hi ^ c1_hi ^ c2_hi;
1023
- if (r < 0)
1024
- r += 0x100000000;
1025
- return r;
1026
- }
1027
-
1028
- function s1_512_lo(xh, xl) {
1029
- const c0_lo = rotr64_lo(xh, xl, 14);
1030
- const c1_lo = rotr64_lo(xh, xl, 18);
1031
- const c2_lo = rotr64_lo(xl, xh, 9); // 41
1032
-
1033
- let r = c0_lo ^ c1_lo ^ c2_lo;
1034
- if (r < 0)
1035
- r += 0x100000000;
1036
- return r;
1037
- }
1038
-
1039
- function g0_512_hi(xh, xl) {
1040
- const c0_hi = rotr64_hi(xh, xl, 1);
1041
- const c1_hi = rotr64_hi(xh, xl, 8);
1042
- const c2_hi = shr64_hi(xh, xl, 7);
1043
-
1044
- let r = c0_hi ^ c1_hi ^ c2_hi;
1045
- if (r < 0)
1046
- r += 0x100000000;
1047
- return r;
1048
- }
1049
-
1050
- function g0_512_lo(xh, xl) {
1051
- const c0_lo = rotr64_lo(xh, xl, 1);
1052
- const c1_lo = rotr64_lo(xh, xl, 8);
1053
- const c2_lo = shr64_lo(xh, xl, 7);
1054
-
1055
- let r = c0_lo ^ c1_lo ^ c2_lo;
1056
- if (r < 0)
1057
- r += 0x100000000;
1058
- return r;
1059
- }
1060
-
1061
- function g1_512_hi(xh, xl) {
1062
- const c0_hi = rotr64_hi(xh, xl, 19);
1063
- const c1_hi = rotr64_hi(xl, xh, 29); // 61
1064
- const c2_hi = shr64_hi(xh, xl, 6);
1065
-
1066
- let r = c0_hi ^ c1_hi ^ c2_hi;
1067
- if (r < 0)
1068
- r += 0x100000000;
1069
- return r;
1070
- }
1071
-
1072
- function g1_512_lo(xh, xl) {
1073
- const c0_lo = rotr64_lo(xh, xl, 19);
1074
- const c1_lo = rotr64_lo(xl, xh, 29); // 61
1075
- const c2_lo = shr64_lo(xh, xl, 6);
1076
-
1077
- let r = c0_lo ^ c1_lo ^ c2_lo;
1078
- if (r < 0)
1079
- r += 0x100000000;
1080
- return r;
1081
- }
1082
-
1083
- /**
1084
- * The `SHA256HMAC` class is used to create Hash-based Message Authentication Code (HMAC) using the SHA-256 cryptographic hash function.
1085
- *
1086
- * HMAC is a specific type of MAC involving a cryptographic hash function and a secret cryptographic key. It may be used to simultaneously verify both the data integrity and the authenticity of a message.
1087
- *
1088
- * This class also uses the SHA-256 cryptographic hash algorithm that produces a 256-bit (32-byte) hash value.
1089
- *
1090
- * @property inner - Represents the inner hash of SHA-256.
1091
- * @property outer - Represents the outer hash of SHA-256.
1092
- * @property blockSize - The block size for the SHA-256 hash function, in bytes. It's set to 64 bytes.
1093
- * @property outSize - The output size of the SHA-256 hash function, in bytes. It's set to 32 bytes.
1094
- */
1095
- export class SHA256HMAC {
1096
- inner: SHA256
1097
- outer: SHA256
1098
- blockSize = 64
1099
- outSize = 32
1100
-
1101
- /**
1102
- * The constructor for the `SHA256HMAC` class.
1103
- *
1104
- * It initializes the `SHA256HMAC` object and sets up the inner and outer padded keys.
1105
- * If the key size is larger than the blockSize, it is digested using SHA-256.
1106
- * If the key size is less than the blockSize, it is padded with zeroes.
1107
- *
1108
- * @constructor
1109
- * @param key - The key to use to create the HMAC. Can be a number array or a string in hexadecimal format.
1110
- *
1111
- * @example
1112
- * const myHMAC = new SHA256HMAC('deadbeef');
1113
- */
1114
- constructor(key: number[] | string) {
1115
- key = toArray(key, 'hex')
1116
- // Shorten key, if needed
1117
- if (key.length > this.blockSize) {
1118
- key = new SHA256().update(key).digest() as number[]
1119
- }
1120
- assert(key.length <= this.blockSize)
1121
-
1122
- // Add padding to key
1123
- let i
1124
- for (i = key.length; i < this.blockSize; i++) { key.push(0) }
1125
-
1126
- for (i = 0; i < key.length; i++) { key[i] ^= 0x36 }
1127
- this.inner = new SHA256().update(key)
1128
-
1129
- // 0x36 ^ 0x5c = 0x6a
1130
- for (i = 0; i < key.length; i++) { key[i] ^= 0x6a }
1131
- this.outer = new SHA256().update(key)
1132
- }
1133
-
1134
- /**
1135
- * Updates the `SHA256HMAC` object with part of the message to be hashed.
1136
- *
1137
- * @method update
1138
- * @param msg - Part of the message to hash. Can be a number array or a string.
1139
- * @param enc - If 'hex', then the input is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
1140
- * @returns Returns the instance of `SHA256HMAC` for chaining calls.
1141
- *
1142
- * @example
1143
- * myHMAC.update('deadbeef', 'hex');
1144
- */
1145
- update(msg: number[] | string, enc?: 'hex'): SHA256HMAC {
1146
- this.inner.update(msg, enc)
1147
- return this
1148
- }
1149
-
1150
- /**
1151
- * Finalizes the HMAC computation and returns the resultant hash.
1152
- *
1153
- * @method digest
1154
- * @param enc - If 'hex', then the output is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
1155
- * @returns Returns the digest of the hashed data. Can be a number array or a string.
1156
- *
1157
- * @example
1158
- * let hashedMessage = myHMAC.digest('hex');
1159
- */
1160
- digest(enc?: 'hex'): number[] | string {
1161
- this.outer.update(this.inner.digest() as number[])
1162
- return this.outer.digest(enc)
1163
- }
1164
- }
1165
-
1166
- /**
1167
- * The `SHA512HMAC` class is used to create Hash-based Message Authentication Code (HMAC) using the SHA-512 cryptographic hash function.
1168
- *
1169
- * HMAC is a specific type of MAC involving a cryptographic hash function and a secret cryptographic key. It may be used to simultaneously verify both the data integrity and the authenticity of a message.
1170
- *
1171
- * This class also uses the SHA-512 cryptographic hash algorithm that produces a 512-bit (64-byte) hash value.
1172
- *
1173
- * @property inner - Represents the inner hash of SHA-512.
1174
- * @property outer - Represents the outer hash of SHA-512.
1175
- * @property blockSize - The block size for the SHA-512 hash function, in bytes. It's set to 128 bytes.
1176
- * @property outSize - The output size of the SHA-512 hash function, in bytes. It's set to 64 bytes.
1177
- */
1178
- export class SHA512HMAC {
1179
- inner: SHA512
1180
- outer: SHA512
1181
- blockSize = 128
1182
- outSize = 32
1183
-
1184
- /**
1185
- * The constructor for the `SHA512HMAC` class.
1186
- *
1187
- * It initializes the `SHA512HMAC` object and sets up the inner and outer padded keys.
1188
- * If the key size is larger than the blockSize, it is digested using SHA-512.
1189
- * If the key size is less than the blockSize, it is padded with zeroes.
1190
- *
1191
- * @constructor
1192
- * @param key - The key to use to create the HMAC. Can be a number array or a string in hexadecimal format.
1193
- *
1194
- * @example
1195
- * const myHMAC = new SHA512HMAC('deadbeef');
1196
- */
1197
- constructor(key: number[] | string) {
1198
- key = toArray(key, 'hex')
1199
- // Shorten key, if needed
1200
- if (key.length > this.blockSize) {
1201
- key = new SHA512().update(key).digest() as number[]
1202
- }
1203
- assert(key.length <= this.blockSize)
1204
-
1205
- // Add padding to key
1206
- let i
1207
- for (i = key.length; i < this.blockSize; i++) { key.push(0) }
1208
-
1209
- for (i = 0; i < key.length; i++) { key[i] ^= 0x36 }
1210
- this.inner = new SHA512().update(key)
1211
-
1212
- // 0x36 ^ 0x5c = 0x6a
1213
- for (i = 0; i < key.length; i++) { key[i] ^= 0x6a }
1214
- this.outer = new SHA512().update(key)
1215
- }
1216
-
1217
- /**
1218
- * Updates the `SHA512HMAC` object with part of the message to be hashed.
1219
- *
1220
- * @method update
1221
- * @param msg - Part of the message to hash. Can be a number array or a string.
1222
- * @param enc - If 'hex', then the input is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
1223
- * @returns Returns the instance of `SHA512HMAC` for chaining calls.
1224
- *
1225
- * @example
1226
- * myHMAC.update('deadbeef', 'hex');
1227
- */
1228
- update(msg: number[] | string, enc?: 'hex'): SHA512HMAC {
1229
- this.inner.update(msg, enc)
1230
- return this
1231
- }
1232
-
1233
- /**
1234
- * Finalizes the HMAC computation and returns the resultant hash.
1235
- *
1236
- * @method digest
1237
- * @param enc - If 'hex', then the output is encoded as hexadecimal. If undefined or not 'hex', then no encoding is performed.
1238
- * @returns Returns the digest of the hashed data. Can be a number array or a string.
1239
- *
1240
- * @example
1241
- * let hashedMessage = myHMAC.digest('hex');
1242
- */
1243
- digest(enc?: 'hex'): number[] | string {
1244
- this.outer.update(this.inner.digest() as number[])
1245
- return this.outer.digest(enc)
1246
- }
1247
- }
1248
-
1249
- /**
1250
- * Computes RIPEMD160 hash of a given message.
1251
- * @function ripemd160
1252
- * @param msg - The message to compute the hash for.
1253
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1254
- *
1255
- * @returns the computed RIPEMD160 hash of the message.
1256
- *
1257
- * @example
1258
- * const digest = ripemd160('Hello, world!');
1259
- */
1260
- export const ripemd160 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1261
- return new RIPEMD160().update(msg, enc).digest(enc)
1262
- }
1263
-
1264
- /**
1265
- * Computes SHA1 hash of a given message.
1266
- * @function sha1
1267
- * @param msg - The message to compute the hash for.
1268
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1269
- *
1270
- * @returns the computed SHA1 hash of the message.
1271
- *
1272
- * @example
1273
- * const digest = sha1('Hello, world!');
1274
- */
1275
- export const sha1 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1276
- return new SHA1().update(msg, enc).digest(enc)
1277
- }
1278
-
1279
- /**
1280
- * Computes SHA256 hash of a given message.
1281
- * @function sha256
1282
- * @param msg - The message to compute the hash for.
1283
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1284
- *
1285
- * @returns the computed SHA256 hash of the message.
1286
- *
1287
- * @example
1288
- * const digest = sha256('Hello, world!');
1289
- */
1290
- export const sha256 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1291
- return new SHA256().update(msg, enc).digest(enc)
1292
- }
1293
-
1294
- /**
1295
- * Computes SHA512 hash of a given message.
1296
- * @function sha512
1297
- * @param msg - The message to compute the hash for.
1298
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1299
- *
1300
- * @returns the computed SHA512 hash of the message.
1301
- *
1302
- * @example
1303
- * const digest = sha512('Hello, world!');
1304
- */
1305
- export const sha512 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1306
- return new SHA512().update(msg, enc).digest(enc)
1307
- }
1308
-
1309
- /**
1310
- * Performs a 'double hash' using SHA256. This means the data is hashed twice
1311
- * with SHA256. First, the SHA256 hash of the message is computed, then the
1312
- * SHA256 hash of the resulting hash is computed.
1313
- * @function hash256
1314
- * @param msg - The message to compute the hash for.
1315
- * @param enc - Encoding of the message.If 'hex', the message is decoded from hexadecimal.
1316
- *
1317
- * @returns the double hashed SHA256 output.
1318
- *
1319
- * @example
1320
- * const doubleHash = hash256('Hello, world!');
1321
- */
1322
- export const hash256 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1323
- const first = new SHA256().update(msg, enc).digest()
1324
- return new SHA256().update(first).digest(enc)
1325
- }
1326
-
1327
- /**
1328
- * Computes SHA256 hash of a given message and then computes a RIPEMD160 hash of the result.
1329
- *
1330
- * @function hash160
1331
- * @param msg - The message to compute the hash for.
1332
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal.
1333
- *
1334
- * @returns the RIPEMD160 hash of the SHA256 hash of the input message.
1335
- *
1336
- * @example
1337
- * const hash = hash160('Hello, world!');
1338
- */
1339
- export const hash160 = (msg: number[] | string, enc?: 'hex'): number[] | string => {
1340
- const first = new SHA256().update(msg, enc).digest()
1341
- return new RIPEMD160().update(first).digest(enc)
1342
- }
1343
-
1344
- /**
1345
- * Computes SHA256 HMAC of a given message with a given key.
1346
- * @function sha256hmac
1347
- * @param key - The key used to compute the HMAC
1348
- * @param msg - The message to compute the hash for.
1349
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1350
- *
1351
- * @returns the computed HMAC of the message.
1352
- *
1353
- * @example
1354
- * const digest = sha256hmac('deadbeef', 'ffff001d');
1355
- */
1356
- export const sha256hmac = (key: number[] | string, msg: number[] | string, enc?: 'hex'): number[] | string => {
1357
- return new SHA256HMAC(key).update(msg, enc).digest(enc)
1358
- }
1359
-
1360
- /**
1361
- * Computes SHA512 HMAC of a given message with a given key.
1362
- * @function sha512hmac
1363
- * @param key - The key used to compute the HMAC
1364
- * @param msg - The message to compute the hash for.
1365
- * @param enc - The encoding of the message. If 'hex', the message is decoded from hexadecimal first.
1366
- *
1367
- * @returns the computed HMAC of the message.
1368
- *
1369
- * @example
1370
- * const digest = sha512hmac('deadbeef', 'ffff001d');
1371
- */
1372
- export const sha512hmac = (key: number[] | string, msg: number[] | string, enc?: 'hex'): number[] | string => {
1373
- return new SHA512HMAC(key).update(msg, enc).digest(enc)
1374
- }
1375
-
1376
- /**
1377
- * Limited SHA-512-only PBKDF2 function for use in deprecated BIP39 code.
1378
- * @function pbkdf2
1379
- * @param password - The PBKDF2 password
1380
- * @param salt - The PBKDF2 salt
1381
- * @param iterations - The number of of iterations to run
1382
- * @param keylen - The length of the key
1383
- * @param digest - The digest (must be sha512 for this implementation)
1384
- *
1385
- * @returns The computed key
1386
- */
1387
- export function pbkdf2(password: number[], salt: number[], iterations: number, keylen: number, digest = 'sha512'): number[] {
1388
- if (digest !== 'sha512') {
1389
- throw new Error('Only sha512 is supported in this PBKDF2 implementation')
1390
- }
1391
- const DK = new Array(keylen)
1392
- const block1 = [...salt, 0, 0, 0, 0]
1393
- const s = Buffer.from('hello')
1394
-
1395
- let destPos = 0
1396
- const hLen = 64
1397
- const l = Math.ceil(keylen / hLen)
1398
-
1399
- for (let i = 1; i <= l; i++) {
1400
- block1[salt.length] = (i >> 24) & 0xFF; // MSB
1401
- block1[salt.length + 1] = (i >> 16) & 0xFF;
1402
- block1[salt.length + 2] = (i >> 8) & 0xFF;
1403
- block1[salt.length + 3] = i & 0xFF; // LSB
1404
-
1405
- const T = sha512hmac(password, block1) as number[]
1406
- let U = T
1407
-
1408
- for (let j = 1; j < iterations; j++) {
1409
- U = sha512hmac(password, U) as number[]
1410
- for (let k = 0; k < hLen; k++) T[k] ^= U[k]
1411
- }
1412
-
1413
- for (let i = 0; i < T.length; i++) {
1414
- DK[destPos + i] = T[i]
1415
- }
1416
- destPos += hLen
1417
- }
1418
-
1419
- return DK.slice(0, keylen)
1420
- }