Calculate Checksum for Stream - CSharp System.IO

CSharp examples for System.IO:Stream

Description

Calculate Checksum for Stream

Demo Code


using System.IO;/*from  ww  w.  j  a v  a2  s  . c  o  m*/
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static string Calculate(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            SHA256Managed sha = new SHA256Managed();
            byte[] checksum = sha.ComputeHash(stream);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in checksum)
            {
                sb.Append(b.ToString("X2"));
            }

            return sb.ToString();
        }
}

Related Tutorials