Пример #1
0
        public byte[] ComputeHash(byte[] data)
        {
            byte[] ipad = new byte[64];
            byte[] opad = new byte[64];

            var hash = new Sha256();
            Array.Copy(_key, ipad, _key.Length);
            Array.Copy(_key, opad, _key.Length);

            for (int i = 0; i < 64 ; ++i)
            {
                ipad[i] ^= 0x36;
                opad[i] ^= 0x5c;
            }

            hash.AddData(ipad, 0, (uint)ipad.Length);
            if (data.Length>0)
                hash.AddData(data, 0, (uint)data.Length);
            var ipadHash = hash.GetHash();

            hash = new Sha256();

            hash.AddData(opad, 0, (uint)opad.Length);
            hash.AddData(ipadHash, 0, (uint)ipadHash.Length);

            return hash.GetHash();
        }
Пример #2
0
        public static byte[] HashFile(Stream fs)
        {
            Sha256 sha = new Sha256();
            byte[] buf = new byte[8196];

            uint bytes_read;
            do
            {
                bytes_read = (uint)fs.Read(buf, 0, buf.Length);
                if (bytes_read == 0)
                    break;

                sha.AddData(buf, 0, bytes_read);
            }
            while (bytes_read == 8196);

            return sha.GetHash();
        }
Пример #3
0
 private void Initialize(byte[] keyData)
 {
     Array.Clear(_key,0,_key.Length);
     // If the key is too long, use the hash of the key
     if (keyData.Length>_key.Length)
     {
         var hashAlgo = new Sha256();
         hashAlgo.AddData(keyData, 0, (uint)keyData.Length);
         keyData = hashAlgo.GetHash();
     }
     Array.Copy(keyData, _key, keyData.Length);
 }