Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1344x 1344x 1344x 1344x 1344x 1344x 1008x 1008x 1008x 1008x 1008x 1008x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 1x 12x 12x 12x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 336x 336x 336x 336x 336x 336x 21x 1008x 1008x 21x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 1344x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x | /* eslint-disable no-bitwise */ import { ch } from './ch.ts'; import { int32 } from './int32.ts'; import { maj } from './maj.ts'; import { ShaBase } from './sha-base.ts'; /** * Constants used in the SHA-224 and SHA-256 cryptographic hash functions. * * These 32-bit integer values are the first 32 bits of the fractional parts of the cube roots of the first 64 prime numbers. * They are used as round constants in the main compression function of the SHA-2 family of algorithms. * @see [FIPS PUB 180-4, Section 4.2.2](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) * @internal */ const K = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ]; /** * Computes the SHA-224/SHA-256 σ₀ (sigma0) function for a 32-bit integer. * * This function performs a bitwise rotation and XOR combination as defined in the SHA-2 specification: * σ₀(x) = ROTR²(x) ⊕ ROTR¹³(x) ⊕ ROTR²²(x) * @param x - The 32-bit integer input. * @returns The result of applying the σ₀ function to the input. * @internal */ function sigma0(x: number): number { return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10)); } /** * Computes the SHA-224/SHA-256 σ₁ (sigma1) function for a 32-bit integer. * * This function performs bitwise right rotations and XORs as defined in the SHA-2 specification: * σ₁(x) = ROTR⁶(x) ⊕ ROTR¹¹(x) ⊕ ROTR²⁵(x) * @param x - The 32-bit integer input. * @returns The result of applying the σ₁ function to the input. * @internal */ function sigma1(x: number): number { return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7)); } /** * Computes the SHA-224/SHA-256 σ₀ (gamma0) function for a 32-bit integer. * * This function performs a bitwise rotation and shift, then combines the results using XOR, * as defined in the SHA-2 specification: * σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x) * @param x - The 32-bit integer input. * @returns The result of the σ₀ transformation. * @internal */ function gamma0(x: number): number { return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3); } /** * Computes the SHA-224/SHA-256 σ₁ (gamma1) function for a 32-bit integer. * * This function performs bitwise operations as defined in the SHA-2 specification: * σ₁(x) = ROTR^17(x) XOR ROTR^19(x) XOR SHR^10(x) * where: * - ROTR^n(x) is the right rotation of x by n bits, * - SHR^n(x) is the right shift of x by n bits. * @param x - The 32-bit integer input. * @returns The result of applying the σ₁ function to the input. * @internal */ function gamma1(x: number): number { return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10); } /** * Secure Hash Algorithm, SHA2 SHA-224 * @example * ```typescript * const sha224 = new Sha224(); * sha224.update('hello world', 'utf8'); * sha224.digest('hex'); * // '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' * ``` * ```typescript * const sha224 = new Sha224(); * sha224.update(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64])); * sha224.digest('hex'); * // '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' * ``` * @group Binary * @category Hash */ export class Sha224 extends ShaBase { private a = 0xc1059ed8; private b = 0x367cd507; private c = 0x3070dd17; private d = 0xf70e5939; private e = 0xffc00b31; private f = 0x68581511; private g = 0x64f98fa7; private h = 0xbefa4fa4; private readonly w: number[]; /** * Creates a new SHA-224 hash instance and initializes its internal state. * * @remarks * The internal state variables are set to the initial SHA-224 constants as specified * in FIPS PUB 180-4. Use {@link update} to process data and {@link digest} to retrieve the hash. */ public constructor() { super(64, 56); this.w = Array.from({ length: 64 }); } protected override updateCounters(buffer: Uint8Array): void { const { w } = this; let a = int32(this.a); let b = int32(this.b); let c = int32(this.c); let d = int32(this.d); let e = int32(this.e); let f = int32(this.f); let g = int32(this.g); let h = int32(this.h); let i: number; for (i = 0; i < 16; ++i) { w[i] = (buffer[i * 4 + 0] << 24) | (buffer[i * 4 + 1] << 16) | (buffer[i * 4 + 2] << 8) | buffer[i * 4 + 3]; } for (; i < 64; ++i) { w[i] = int32(gamma1(w[i - 2]) + w[i - 7] + gamma0(w[i - 15]) + w[i - 16]); } for (let j = 0; j < 64; ++j) { const T1 = int32(h + sigma1(e) + ch(e, f, g) + int32(K[j]) + w[j]); const T2 = int32(sigma0(a) + maj(a, b, c)); h = g; g = f; f = e; e = int32(d + T1); d = c; c = b; b = a; a = int32(T1 + T2); } this.a = int32(a + this.a); this.b = int32(b + this.b); this.c = int32(c + this.c); this.d = int32(d + this.d); this.e = int32(e + this.e); this.f = int32(f + this.f); this.g = int32(g + this.g); this.h = int32(h + this.h); } protected override hash(): Uint8Array { return new Uint8Array([ (this.a & 0xff000000) >> 24, (this.a & 0x00ff0000) >> 16, (this.a & 0x0000ff00) >> 8, this.a & 0x000000ff, (this.b & 0xff000000) >> 24, (this.b & 0x00ff0000) >> 16, (this.b & 0x0000ff00) >> 8, this.b & 0x000000ff, (this.c & 0xff000000) >> 24, (this.c & 0x00ff0000) >> 16, (this.c & 0x0000ff00) >> 8, this.c & 0x000000ff, (this.d & 0xff000000) >> 24, (this.d & 0x00ff0000) >> 16, (this.d & 0x0000ff00) >> 8, this.d & 0x000000ff, (this.e & 0xff000000) >> 24, (this.e & 0x00ff0000) >> 16, (this.e & 0x0000ff00) >> 8, this.e & 0x000000ff, (this.f & 0xff000000) >> 24, (this.f & 0x00ff0000) >> 16, (this.f & 0x0000ff00) >> 8, this.f & 0x000000ff, (this.g & 0xff000000) >> 24, (this.g & 0x00ff0000) >> 16, (this.g & 0x0000ff00) >> 8, this.g & 0x000000ff, ]); } } |