protected override byte[] FinalHash(ISha2State hashState) { var size = 8; if (this._bitLen == 384) { size = 6; } byte[] result = new byte[size * 8]; Sha512State state = (Sha512State)hashState; for (int h = 0, p = 0; h < size; h++, p += 8) { var hState = state.H[h]; result[p + 0] = (byte)(hState >> 56); result[p + 1] = (byte)(hState >> 48); result[p + 2] = (byte)(hState >> 40); result[p + 3] = (byte)(hState >> 32); result[p + 4] = (byte)(hState >> 24); result[p + 5] = (byte)(hState >> 16); result[p + 6] = (byte)(hState >> 8); result[p + 7] = (byte)(hState); } return(result); }
protected override void Compress(ISha2State hashState, byte[] buffer) { // Init Message Schedule array (W) UInt64[] W = new UInt64[80]; for (int t = 0, j = 0; t < 16; ++t, j += 8) { W[t] = ((UInt64)buffer[j] << 56) | ((UInt64)buffer[j + 1] << 48) | ((UInt64)buffer[j + 2] << 40) | ((UInt64)buffer[j + 3] << 32) | ((UInt64)buffer[j + 4] << 24) | ((UInt64)buffer[j + 5] << 16) | ((UInt64)buffer[j + 6] << 8) | ((UInt64)buffer[j + 7]); } for (int t = 16; t < 80; ++t) { W[t] = Wsigma1(W[t - 2]) + W[t - 7] + Wsigma0(W[t - 15]) + W[t - 16]; } Sha512State state = (Sha512State)hashState; // Init working variables UInt64 a = state.H[0]; UInt64 b = state.H[1]; UInt64 c = state.H[2]; UInt64 d = state.H[3]; UInt64 e = state.H[4]; UInt64 f = state.H[5]; UInt64 g = state.H[6]; UInt64 h = state.H[7]; // 80 rounds for (int r = 0; r < 80; ++r) { UInt64 T1 = h + Sigma1(e) + Choice(e, f, g) + K[r] + W[r]; UInt64 T2 = Sigma0(a) + Majority(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } // Update hash state state.H[0] += a; state.H[1] += b; state.H[2] += c; state.H[3] += d; state.H[4] += e; state.H[5] += f; state.H[6] += g; state.H[7] += h; }
protected override void Compress(ISha2State hashState, byte[] buffer) { // Init Message Schedule array (W) UInt32[] W = new UInt32[64]; for (int t = 0, j = 0; t < 16; ++t, j += 4) { W[t] = (UInt32)((buffer[j] << 24) | (buffer[j + 1] << 16) | (buffer[j + 2] << 8) | (buffer[j + 3])); } for (int t = 16; t < 64; ++t) { W[t] = Wsigma1(W[t - 2]) + W[t - 7] + Wsigma0(W[t - 15]) + W[t - 16]; } Sha256State state = (Sha256State)hashState; // Init working variables UInt32 a = state.H[0]; UInt32 b = state.H[1]; UInt32 c = state.H[2]; UInt32 d = state.H[3]; UInt32 e = state.H[4]; UInt32 f = state.H[5]; UInt32 g = state.H[6]; UInt32 h = state.H[7]; // 64 rounds for (int r = 0; r < 64; ++r) { UInt32 T1 = h + Sigma1(e) + Choice(e, f, g) + K[r] + W[r]; UInt32 T2 = Sigma0(a) + Majority(a, b, c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } // Update hash state state.H[0] += a; state.H[1] += b; state.H[2] += c; state.H[3] += d; state.H[4] += e; state.H[5] += f; state.H[6] += g; state.H[7] += h; }
protected override byte[] FinalHash(ISha2State hashState) { var size = 8; if (this._bitLen == 224) { size = 7; } byte[] result = new byte[size * 4]; Sha256State state = (Sha256State)hashState; for (int h = 0, p = 0; h < size; h++, p += 4) { var hState = state.H[h]; result[p + 0] = (byte)(hState >> 24); result[p + 1] = (byte)(hState >> 16); result[p + 2] = (byte)(hState >> 8); result[p + 3] = (byte)(hState); } return(result); }
/// <summary> /// Init hsha state /// </summary> /// <param name="hashState"></param> protected override void InitState(out ISha2State hashState) { if (this._bitLen == 512) { hashState = new Sha512State() { H = new UInt64[] { 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 } } } ; else { hashState = new Sha512State() { H = new UInt64[] { 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4 } } }; }
/// <summary> /// Init hsha state /// </summary> /// <param name="hashState"></param> protected override void InitState(out ISha2State hashState) { if (this._bitLen == 256) { hashState = new Sha256State() { H = new UInt32[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 } } } ; else { hashState = new Sha256State() { H = new UInt32[] { 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 } } }; }
protected abstract byte[] FinalHash(ISha2State hashState);
/// <summary> /// Compress data from buffer /// </summary> /// <param name="hashState"></param> /// <param name="buffer"></param> protected abstract void Compress(ISha2State hashState, byte[] buffer);
/// <summary> /// Initialize Hash State /// </summary> /// <param name="hashState">Hash State initialized</param> protected abstract void InitState(out ISha2State hashState);