コード例 #1
0
ファイル: Sha2.cs プロジェクト: qrypu-project/qrypu
        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);
        }
コード例 #2
0
ファイル: Sha2.cs プロジェクト: qrypu-project/qrypu
        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;
        }
コード例 #3
0
ファイル: Sha2.cs プロジェクト: qrypu-project/qrypu
 /// <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
             }
         }
     };
 }