static void SHA256Final(ref SHA256_CTX ctx, byte[] hash) { uint i = ctx.datalen; // Pad whatever data is left in the buffer. if (ctx.datalen < 56) { ctx.data[i++] = 0x80; while (i < 56) { ctx.data[i++] = 0x00; } } else { ctx.data[i++] = 0x80; while (i < 64) { ctx.data[i++] = 0x00; } SHA256Transform(ref ctx, ctx.data); } // Append to the padding the total message's length in bits and transform. DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], ctx.datalen * 8); ctx.data[63] = (byte)(ctx.bitlen[0]); ctx.data[62] = (byte)(ctx.bitlen[0] >> 8); ctx.data[61] = (byte)(ctx.bitlen[0] >> 16); ctx.data[60] = (byte)(ctx.bitlen[0] >> 24); ctx.data[59] = (byte)(ctx.bitlen[1]); ctx.data[58] = (byte)(ctx.bitlen[1] >> 8); ctx.data[57] = (byte)(ctx.bitlen[1] >> 16); ctx.data[56] = (byte)(ctx.bitlen[1] >> 24); SHA256Transform(ref ctx, ctx.data); // Since this implementation uses little endian byte ordering and SHA uses big endian, // reverse all the bytes when copying the final state to the output hash. for (i = 0; i < 4; ++i) { hash[i] = (byte)(((ctx.state[0]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 4] = (byte)(((ctx.state[1]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 8] = (byte)(((ctx.state[2]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 12] = (byte)((ctx.state[3] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 16] = (byte)((ctx.state[4] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 20] = (byte)((ctx.state[5] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 24] = (byte)((ctx.state[6] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 28] = (byte)((ctx.state[7] >> (int)(24 - i * 8)) & 0x000000ff); } }
private static void SHA256Init(ref SHA256_CTX ctx) { ctx.datalen = 0u; ctx.bitlen[0] = 0u; ctx.bitlen[1] = 0u; ctx.state[0] = 1779033703u; ctx.state[1] = 3144134277u; ctx.state[2] = 1013904242u; ctx.state[3] = 2773480762u; ctx.state[4] = 1359893119u; ctx.state[5] = 2600822924u; ctx.state[6] = 528734635u; ctx.state[7] = 1541459225u; }
//Initialize static void SHA256Init(ref SHA256_CTX ctx) { ctx.datalen = 0; ctx.bitlen[0] = 0; ctx.bitlen[1] = 0; ctx.state[0] = 0x6a09e667; ctx.state[1] = 0xbb67ae85; ctx.state[2] = 0x3c6ef372; ctx.state[3] = 0xa54ff53a; ctx.state[4] = 0x510e527f; ctx.state[5] = 0x9b05688c; ctx.state[6] = 0x1f83d9ab; ctx.state[7] = 0x5be0cd19; }
private static void SHA256Update(ref SHA256_CTX ctx, byte[] data, uint len) { for (uint num = 0u; num < len; num++) { ctx.data[ctx.datalen] = data[num]; ctx.datalen++; if (ctx.datalen == 64) { SHA256Transform(ref ctx, ctx.data); DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], 512u); ctx.datalen = 0u; } } }
static void SHA256Init(ref SHA256_CTX ctx) { ctx.datalen = 0; ctx.bitlen[0] = 0; ctx.bitlen[1] = 0; ctx.state[0] = initalHashValue[0]; ctx.state[1] = initalHashValue[1]; ctx.state[2] = initalHashValue[2]; ctx.state[3] = initalHashValue[3]; ctx.state[4] = initalHashValue[4]; ctx.state[5] = initalHashValue[5]; ctx.state[6] = initalHashValue[6]; ctx.state[7] = initalHashValue[7]; }
static void SHA256Update(ref SHA256_CTX ctx, byte[] data, uint len) { for (uint i = 0; i < len; ++i) { ctx.data[ctx.datalen] = data[i]; ctx.datalen++; if (ctx.datalen == 64) { SHA256Transform(ref ctx, ctx.data); DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], 512); ctx.datalen = 0; } } }
static void SHA256Transform(ref SHA256_CTX ctx, byte[] data) { uint a, b, c, d, e, f, g, h, i, j, t1, t2; uint[] m = new uint[64]; for (i = 0, j = 0; i < 16; ++i, j += 4) { m[i] = (uint)((data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3])); } for (; i < 64; ++i) { m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; } a = ctx.state[0]; b = ctx.state[1]; c = ctx.state[2]; d = ctx.state[3]; e = ctx.state[4]; f = ctx.state[5]; g = ctx.state[6]; h = ctx.state[7]; for (i = 0; i < 64; ++i) { t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i]; t2 = EP0(a) + MAJ(a, b, c); h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; } ctx.state[0] += a; ctx.state[1] += b; ctx.state[2] += c; ctx.state[3] += d; ctx.state[4] += e; ctx.state[5] += f; ctx.state[6] += g; ctx.state[7] += h; }
static void SHA256Final(ref SHA256_CTX ctx, byte[] hash) { uint i = ctx.datalen; if (ctx.datalen < 56) { ctx.data[i++] = 0x80; while (i < 56) { ctx.data[i++] = 0x00; } } else { ctx.data[i++] = 0x80; while (i < 64) { ctx.data[i++] = 0x00; } SHA256Transform(ref ctx, ctx.data); } DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], ctx.datalen * 8); ctx.data[63] = (byte)(ctx.bitlen[0]); ctx.data[62] = (byte)(ctx.bitlen[0] >> 8); ctx.data[61] = (byte)(ctx.bitlen[0] >> 16); ctx.data[60] = (byte)(ctx.bitlen[0] >> 24); ctx.data[59] = (byte)(ctx.bitlen[1]); ctx.data[58] = (byte)(ctx.bitlen[1] >> 8); ctx.data[57] = (byte)(ctx.bitlen[1] >> 16); ctx.data[56] = (byte)(ctx.bitlen[1] >> 24); SHA256Transform(ref ctx, ctx.data); for (i = 0; i < 4; ++i) { hash[i] = (byte)(((ctx.state[0]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 4] = (byte)(((ctx.state[1]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 8] = (byte)(((ctx.state[2]) >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 12] = (byte)((ctx.state[3] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 16] = (byte)((ctx.state[4] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 20] = (byte)((ctx.state[5] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 24] = (byte)((ctx.state[6] >> (int)(24 - i * 8)) & 0x000000ff); hash[i + 28] = (byte)((ctx.state[7] >> (int)(24 - i * 8)) & 0x000000ff); } }
private static void SHA256Transform(ref SHA256_CTX ctx, byte[] data) { uint[] array = new uint[64]; uint num = 0u; uint num2 = 0u; while (num < 16) { array[num] = (uint)((data[num2] << 24) | (data[num2 + 1] << 16) | (data[num2 + 2] << 8) | data[num2 + 3]); num++; num2 += 4; } for (; num < 64; num++) { array[num] = SIG1(array[num - 2]) + array[num - 7] + SIG0(array[num - 15]) + array[num - 16]; } uint num3 = ctx.state[0]; uint num4 = ctx.state[1]; uint num5 = ctx.state[2]; uint num6 = ctx.state[3]; uint num7 = ctx.state[4]; uint num8 = ctx.state[5]; uint num9 = ctx.state[6]; uint num10 = ctx.state[7]; for (num = 0u; num < 64; num++) { uint num11 = num10 + EP1(num7) + CH(num7, num8, num9) + k[num] + array[num]; uint num12 = EP0(num3) + MAJ(num3, num4, num5); num10 = num9; num9 = num8; num8 = num7; num7 = num6 + num11; num6 = num5; num5 = num4; num4 = num3; num3 = num11 + num12; } ctx.state[0] += num3; ctx.state[1] += num4; ctx.state[2] += num5; ctx.state[3] += num6; ctx.state[4] += num7; ctx.state[5] += num8; ctx.state[6] += num9; ctx.state[7] += num10; }
public byte[] Crypt(byte[] input) { SHA256_CTX ctx = new SHA256_CTX(); ctx.data = new byte[64]; ctx.bitlen = new uint[2]; ctx.state = new uint[8]; byte[] hash = new byte[32]; string hashStr = string.Empty; SHA256Init(ref ctx); SHA256Update(ref ctx, input, (uint)input.Length); SHA256Final(ref ctx, hash); return(hash); }
public static string SHA256(string data) { SHA256_CTX ctx = default(SHA256_CTX); ctx.data = new byte[64]; ctx.bitlen = new uint[2]; ctx.state = new uint[8]; byte[] array = new byte[32]; string text = string.Empty; SHA256Init(ref ctx); SHA256Update(ref ctx, Encoding.Default.GetBytes(data), (uint)data.Length); SHA256Final(ref ctx, array); for (int i = 0; i < 32; i++) { text += $"{array[i]:X2}"; } return(text); }
private static void SHA256Final(ref SHA256_CTX ctx, byte[] hash) { uint num = ctx.datalen; if (ctx.datalen < 56) { ctx.data[num++] = 128; while (num < 56) { ctx.data[num++] = 0; } } else { ctx.data[num++] = 128; while (num < 64) { ctx.data[num++] = 0; } SHA256Transform(ref ctx, ctx.data); } DBL_INT_ADD(ref ctx.bitlen[0], ref ctx.bitlen[1], ctx.datalen * 8); ctx.data[63] = (byte)ctx.bitlen[0]; ctx.data[62] = (byte)(ctx.bitlen[0] >> 8); ctx.data[61] = (byte)(ctx.bitlen[0] >> 16); ctx.data[60] = (byte)(ctx.bitlen[0] >> 24); ctx.data[59] = (byte)ctx.bitlen[1]; ctx.data[58] = (byte)(ctx.bitlen[1] >> 8); ctx.data[57] = (byte)(ctx.bitlen[1] >> 16); ctx.data[56] = (byte)(ctx.bitlen[1] >> 24); SHA256Transform(ref ctx, ctx.data); for (num = 0u; num < 4; num++) { hash[num] = (byte)((ctx.state[0] >> (int)(24 - num * 8)) & 0xFF); hash[num + 4] = (byte)((ctx.state[1] >> (int)(24 - num * 8)) & 0xFF); hash[num + 8] = (byte)((ctx.state[2] >> (int)(24 - num * 8)) & 0xFF); hash[num + 12] = (byte)((ctx.state[3] >> (int)(24 - num * 8)) & 0xFF); hash[num + 16] = (byte)((ctx.state[4] >> (int)(24 - num * 8)) & 0xFF); hash[num + 20] = (byte)((ctx.state[5] >> (int)(24 - num * 8)) & 0xFF); hash[num + 24] = (byte)((ctx.state[6] >> (int)(24 - num * 8)) & 0xFF); hash[num + 28] = (byte)((ctx.state[7] >> (int)(24 - num * 8)) & 0xFF); } }
public static string HashSha256(string data) { SHA256_CTX ctx = new SHA256_CTX(); ctx.data = new byte[64]; ctx.bitlen = new uint[2]; ctx.state = new uint[8]; byte[] hash = new byte[32]; string hashStr = string.Empty; SHA256Init(ref ctx); SHA256Update(ref ctx, Encoding.Default.GetBytes(data), (uint)data.Length); SHA256Final(ref ctx, hash); for (int i = 0; i < 32; i++) { hashStr += string.Format("{0:X2}", hash[i]); } return(hashStr); }
public string Compute(string filename) { string data = File.ReadAllText(filename); SHA256_CTX ctx = new SHA256_CTX(); ctx.data = new byte[64]; ctx.bitlen = new uint[2]; ctx.state = new uint[8]; byte[] hash = new byte[32]; string hashStr = string.Empty; SHA256Init(ref ctx); SHA256Update(ref ctx, File.ReadAllBytes(filename), File.ReadAllBytes(filename).Length); SHA256Final(ref ctx, hash); for (int i = 0; i < 32; i++) { hashStr += string.Format("{0:x2}", hash[i]); } return(hashStr); }
/*********************** FUNCTION DEFINITIONS ***********************/ public void sha256_transform(SHA256_CTX ctx, ReadOnlySpan <byte> data) { uint a, b, c, d, e, f, g, h, t1, t2; Span <uint> w = stackalloc uint[64]; ref byte wRef = ref Unsafe.As <uint, byte>(ref MemoryMarshal.GetReference(w));