|
| 1 | +// TypeScript's DOM library has to be referenced for `TextEncoder` to be recognized as a type |
| 2 | +/// <reference lib="dom" /> |
| 3 | + |
| 4 | +export abstract class BaseHashAlgorithm { |
| 5 | + private textEncoder: TextEncoder; |
| 6 | + |
| 7 | + constructor() { |
| 8 | + this.textEncoder = new TextEncoder(); |
| 9 | + } |
| 10 | + |
| 11 | + getDigest(input: string): string { |
| 12 | + const message = this.textEncoder.encode(input); |
| 13 | + |
| 14 | + const paddedMessage = this.padMessage(message); |
| 15 | + const hash = this.computeHash(paddedMessage); |
| 16 | + const digest = this.hashToString(hash); |
| 17 | + |
| 18 | + return digest; |
| 19 | + } |
| 20 | + |
| 21 | + protected abstract padMessage(message: Uint8Array): ArrayBuffer; |
| 22 | + |
| 23 | + protected abstract computeHash(paddedMessage: ArrayBuffer): ArrayBuffer; |
| 24 | + |
| 25 | + protected abstract hashToString(hash: ArrayBuffer): string; |
| 26 | + |
| 27 | + protected rotl(n: number, x: number): number; |
| 28 | + protected rotl(n: bigint, x: bigint): bigint; |
| 29 | + protected rotl(n: number | bigint, x: number | bigint): number | bigint { |
| 30 | + if (typeof n === 'number' && typeof x === 'number') return (x << n) | (x >>> (32 - n)); |
| 31 | + if (typeof n === 'bigint' && typeof x === 'bigint') return (x << n) | (x >> (64n - n)); |
| 32 | + |
| 33 | + throw new Error('Both arguments must be of the same type'); |
| 34 | + } |
| 35 | + |
| 36 | + protected rotr(n: number, x: number): number; |
| 37 | + protected rotr(n: bigint, x: bigint): bigint; |
| 38 | + protected rotr(n: number | bigint, x: number | bigint): number | bigint { |
| 39 | + if (typeof n === 'number' && typeof x === 'number') return (x >>> n) | (x << (32 - n)); |
| 40 | + if (typeof n === 'bigint' && typeof x === 'bigint') return (x >> n) | (x << (64n - n)); |
| 41 | + |
| 42 | + throw new Error('Both arguments must be of the same type'); |
| 43 | + } |
| 44 | + |
| 45 | + protected ch(x: number, y: number, z: number): number; |
| 46 | + protected ch(x: bigint, y: bigint, z: bigint): bigint; |
| 47 | + protected ch(x: number | bigint, y: number | bigint, z: number | bigint): number | bigint { |
| 48 | + if (typeof x === typeof y && typeof y === typeof z) { |
| 49 | + // @ts-expect-error TypeScript can't disambiguate between number and bigint |
| 50 | + return (x & y) ^ (~x & z); |
| 51 | + } |
| 52 | + |
| 53 | + throw new Error('All arguments must be of the same type'); |
| 54 | + } |
| 55 | + |
| 56 | + protected parity(x: number, y: number, z: number): number; |
| 57 | + protected parity(x: bigint, y: bigint, z: bigint): bigint; |
| 58 | + protected parity(x: number | bigint, y: number | bigint, z: number | bigint): number | bigint { |
| 59 | + if (typeof x === typeof y && typeof y === typeof z) { |
| 60 | + // @ts-expect-error TypeScript can't disambiguate between number and bigint |
| 61 | + return x ^ y ^ z; |
| 62 | + } |
| 63 | + |
| 64 | + throw new Error('All arguments must be of the same type'); |
| 65 | + } |
| 66 | + |
| 67 | + protected maj(x: number, y: number, z: number): number; |
| 68 | + protected maj(x: bigint, y: bigint, z: bigint): bigint; |
| 69 | + protected maj(x: number | bigint, y: number | bigint, z: number | bigint): number | bigint { |
| 70 | + if (typeof x === typeof y && typeof y === typeof z) { |
| 71 | + // @ts-expect-error TypeScript can't disambiguate between number and bigint |
| 72 | + return (x & y) ^ (x & z) ^ (y & z); |
| 73 | + } |
| 74 | + |
| 75 | + throw new Error('All arguments must be of the same type'); |
| 76 | + } |
| 77 | + |
| 78 | + protected bigSigma0(x: number): number; |
| 79 | + protected bigSigma0(x: bigint): bigint; |
| 80 | + protected bigSigma0(x: number | bigint): number | bigint { |
| 81 | + if (typeof x === 'number') { |
| 82 | + // number version used by SHA1 and SHA256 |
| 83 | + return this.rotr(2, x) ^ this.rotr(13, x) ^ this.rotr(22, x); |
| 84 | + } |
| 85 | + |
| 86 | + // BigInt version used by SHA384 and SHA512 |
| 87 | + return this.rotr(28n, x) ^ this.rotr(34n, x) ^ this.rotr(39n, x); |
| 88 | + } |
| 89 | + |
| 90 | + protected bigSigma1(x: number): number; |
| 91 | + protected bigSigma1(x: bigint): bigint; |
| 92 | + protected bigSigma1(x: number | bigint): number | bigint { |
| 93 | + if (typeof x === 'number') { |
| 94 | + // number version used by SHA1 and SHA256 |
| 95 | + return this.rotr(6, x) ^ this.rotr(11, x) ^ this.rotr(25, x); |
| 96 | + } |
| 97 | + |
| 98 | + // BigInt version used by SHA384 and SHA512 |
| 99 | + return this.rotr(14n, x) ^ this.rotr(18n, x) ^ this.rotr(41n, x); |
| 100 | + } |
| 101 | + |
| 102 | + protected smallSigma0(x: number): number; |
| 103 | + protected smallSigma0(x: bigint): bigint; |
| 104 | + protected smallSigma0(x: number | bigint): number | bigint { |
| 105 | + if (typeof x === 'number') { |
| 106 | + // number version used by SHA1 and SHA256 |
| 107 | + return this.rotr(7, x) ^ this.rotr(18, x) ^ (x >>> 3); |
| 108 | + } |
| 109 | + |
| 110 | + // BigInt version used by SHA384 and SHA512 |
| 111 | + return this.rotr(1n, x) ^ this.rotr(8n, x) ^ (x >> 7n); |
| 112 | + } |
| 113 | + |
| 114 | + protected smallSigma1(x: number): number; |
| 115 | + protected smallSigma1(x: bigint): bigint; |
| 116 | + protected smallSigma1(x: number | bigint): number | bigint { |
| 117 | + if (typeof x === 'number') { |
| 118 | + // number version used by SHA1 and SHA256 |
| 119 | + return this.rotr(17, x) ^ this.rotr(19, x) ^ (x >>> 10); |
| 120 | + } |
| 121 | + |
| 122 | + // BigInt version used by SHA384 and SHA512 |
| 123 | + return this.rotr(19n, x) ^ this.rotr(61n, x) ^ (x >> 6n); |
| 124 | + } |
| 125 | +} |
0 commit comments