示例#1
0
文件: SHA256.cs 项目: bonomali/Ibasa
        public override void Final(ArraySegment <byte> hash)
        {
            if (Index > 55)
            {
                Block[Index++] = 0x80;
                while (Index < 64)
                {
                    Block[Index++] = 0;
                }

                DoBlock();

                while (Index < 56)
                {
                    Block[Index++] = 0;
                }
            }
            else
            {
                Block[Index++] = 0x80;
                while (Index < 56)
                {
                    Block[Index++] = 0;
                }
            }

            var length = BitConverter.SwapBigEndian(Length * 8);

            BitConverter.GetBytes(Block, 56, length);
            DoBlock();

            H0 = BitConverter.SwapBigEndian(H0);
            H1 = BitConverter.SwapBigEndian(H1);
            H2 = BitConverter.SwapBigEndian(H2);
            H3 = BitConverter.SwapBigEndian(H3);
            H4 = BitConverter.SwapBigEndian(H4);
            H5 = BitConverter.SwapBigEndian(H5);
            H6 = BitConverter.SwapBigEndian(H6);
            H7 = BitConverter.SwapBigEndian(H7);

            BitConverter.GetBytes(hash.Array, hash.Offset + 0, H0);
            BitConverter.GetBytes(hash.Array, hash.Offset + 4, H1);
            BitConverter.GetBytes(hash.Array, hash.Offset + 8, H2);
            BitConverter.GetBytes(hash.Array, hash.Offset + 12, H3);
            BitConverter.GetBytes(hash.Array, hash.Offset + 16, H4);
            BitConverter.GetBytes(hash.Array, hash.Offset + 20, H5);
            BitConverter.GetBytes(hash.Array, hash.Offset + 24, H6);
            BitConverter.GetBytes(hash.Array, hash.Offset + 28, H7);

            Reset();
        }
示例#2
0
        private void Iterate(int index)
        {
            var indexbytes = new byte[4];

            BitConverter.GetBytes(indexbytes, 0, index);
            BitConverter.SwapBigEndian(indexbytes);

            HashAlgorithm.Reset();
            HashAlgorithm.Key = Password;
            HashAlgorithm.Update(Salt);
            HashAlgorithm.Update(indexbytes);
            HashAlgorithm.Final(Hash);
            var U = Hash;

            for (int u = 1; u < Iterations; ++u)
            {
                HashAlgorithm.ComputeHash(U, U);

                for (int k = 0; k < Hash.Length; ++k)
                {
                    Hash[k] ^= U[k];
                }
            }
        }
示例#3
0
文件: SHA1.cs 项目: bonomali/Ibasa
        private void DoBlock()
        {
            for (int i = 0; i < 16; ++i)
            {
                W[i] = BitConverter.ToUInt32(Block, i * 4);
                W[i] = BitConverter.SwapBigEndian(W[i]);
            }

            for (int i = 16; i < 80; ++i)
            {
                W[i] = Ibasa.Numerics.Binary.RotateLeft(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
            }

            var a = H0;
            var b = H1;
            var c = H2;
            var d = H3;
            var e = H4;

            for (int i = 0; i < 20; ++i)
            {
                uint f = (b & c) | (~b & d);
                uint k = 0x5A827999;

                var temp = Ibasa.Numerics.Binary.RotateLeft(a, 5) + f + e + k + W[i];
                e = d;
                d = c;
                c = Ibasa.Numerics.Binary.RotateLeft(b, 30);
                b = a;
                a = temp;
            }
            for (int i = 20; i < 40; ++i)
            {
                uint f = b ^ c ^ d;
                uint k = 0x6ED9EBA1;

                var temp = Ibasa.Numerics.Binary.RotateLeft(a, 5) + f + e + k + W[i];
                e = d;
                d = c;
                c = Ibasa.Numerics.Binary.RotateLeft(b, 30);
                b = a;
                a = temp;
            }
            for (int i = 40; i < 60; ++i)
            {
                uint f = (b & c) | (b & d) | (c & d);
                uint k = 0x8F1BBCDC;

                var temp = Ibasa.Numerics.Binary.RotateLeft(a, 5) + f + e + k + W[i];
                e = d;
                d = c;
                c = Ibasa.Numerics.Binary.RotateLeft(b, 30);
                b = a;
                a = temp;
            }
            for (int i = 60; i < 80; ++i)
            {
                uint f = b ^ c ^ d;
                uint k = 0xCA62C1D6;

                var temp = Ibasa.Numerics.Binary.RotateLeft(a, 5) + f + e + k + W[i];
                e = d;
                d = c;
                c = Ibasa.Numerics.Binary.RotateLeft(b, 30);
                b = a;
                a = temp;
            }

            H0   += a;
            H1   += b;
            H2   += c;
            H3   += d;
            H4   += e;
            Index = 0;
        }
示例#4
0
文件: SHA256.cs 项目: bonomali/Ibasa
        private void DoBlock()
        {
            for (int i = 0; i < 16; ++i)
            {
                W[i] = BitConverter.ToUInt32(Block, i * 4);
                W[i] = BitConverter.SwapBigEndian(W[i]);
            }

            for (int i = 16; i < 80; ++i)
            {
                var s0 =
                    Binary.RotateRight(W[i - 15], 7) ^
                    Binary.RotateRight(W[i - 15], 18) ^
                    (W[i - 15] >> 3);

                var s1 =
                    Binary.RotateRight(W[i - 2], 17) ^
                    Binary.RotateRight(W[i - 2], 19) ^
                    (W[i - 2] >> 10);

                W[i] = W[i - 16] + s0 + W[i - 7] + s1;
            }

            var a = H0;
            var b = H1;
            var c = H2;
            var d = H3;
            var e = H4;
            var f = H5;
            var g = H6;
            var h = H7;

            for (int i = 0; i < 64; ++i)
            {
                var s1 =
                    Binary.RotateRight(e, 6) ^
                    Binary.RotateRight(e, 11) ^
                    Binary.RotateRight(e, 25);
                var ch    = (e & f) ^ (~e & g);
                var temp1 = h + s1 + ch + K[i] + W[i];
                var s0    =
                    Binary.RotateRight(a, 2) ^
                    Binary.RotateRight(a, 13) ^
                    Binary.RotateRight(a, 22);
                var maj   = (a & b) ^ (a & c) ^ (b & c);
                var temp2 = s0 + maj;

                h = g;
                g = f;
                f = e;
                e = d + temp1;
                d = c;
                c = b;
                b = a;
                a = temp1 + temp2;
            }

            H0   += a;
            H1   += b;
            H2   += c;
            H3   += d;
            H4   += e;
            H5   += f;
            H6   += g;
            H7   += h;
            Index = 0;
        }