Ensure Data Integrity Using a Keyed Hash Code - CSharp Security

CSharp examples for Security:Hash

Description

Ensure Data Integrity Using a Keyed Hash Code

Demo Code


using System;// www .j ava  2s  . com
using System.IO;
using System.Text;
using System.Security.Cryptography;

class MainClass
    {
        public static void Main(string[] args)
        {
            byte[] key = Encoding.Unicode.GetBytes(args[2]);

            using (KeyedHashAlgorithm hashAlg = KeyedHashAlgorithm.Create(args[1]))
            {
                // Assign the key.
                hashAlg.Key = key;

                using (Stream file = new FileStream(args[0], FileMode.Open,FileAccess.Read))
                {
                    byte[] hash = hashAlg.ComputeHash(file);

                    Console.WriteLine(BitConverter.ToString(hash));
                }
            }
        }
    }

Result


Related Tutorials