Click here to Skip to main content
12,394,595 members (81,041 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

18.4K views
4 bookmarked
Posted

Sha1 Hash generation Tool using C#.NET

, 30 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The sha1 hash generation tool can be used to generate hashes on a file. The has generated can be outputted in the form of dig/Hex/Base64. You may use this utility to verify the integrity of the file transported over internet to make sure they are safe from tampering.
Using the code:
The complete code is given below. The enum EncodeStyle is used to determine what encoding should be applied on output hash value. SHA1HashEncode is the main utility function which uses SHA1CryptoServiceProvider class to provide hashing. This function takes two input arguments 1) path of file which need to be hashed and 2) encoding style of outputed hash value. Call this function from your main program.

using System;
using System.IO;
using System.Security.Cryptography;
 
public enum EncodeStyle
{
  Dig,
  Hex,
  Base64
};
 
static string ByteArrayToString(byte[] arrInput, EncodeStyle encode)
{
  int i;
  StringBuilder sOutput = new StringBuilder(arrInput.Length);
  if(EncodeStyle.Base64 == encode)
  {
     return Convert.ToBase64String(arrInput);
  }
  for (i = 0; i < arrInput.Length; i++)
  {
    switch(encode)
    {
       case EncodeStyle.Dig:
       //encode to decimal with 3 digits so 7 will be 007 (as range of 8 bit is 127 to -128)
       sOutput.Append(arrInput[i].ToString("D3"));
       break;
       case EncodeStyle.Hex:
       sOutput.Append(arrInput[i].ToString("X2"));
       break;
    }
  }
  return sOutput.ToString();
}
 
static string SHA1HashEncode(string filePath, EncodeStyle encode)
{
  SHA1 a = new SHA1CryptoServiceProvider();
  byte[] arr = new byte[60];
  string hash = "";
  using (StreamReader sr = new StreamReader(filePath))
  {
     arr = a.ComputeHash(sr.BaseStream);
     hash = ByteArrayToString(arr, encode);
  }
  return hash;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vpnchoudhary
Unknown
No Biography provided

You may also be interested in...

Comments and Discussions

 
Suggestionarr = a.ComputeHash(ASCIIEncoding.ASCII.GetBytes(filePath)); Pin
durdogu1230-Aug-13 1:10
memberdurdogu1230-Aug-13 1:10 
GeneralReason for my vote of 5 Compact and it works. Better than wr... Pin
DuffmanLight25-Jan-12 14:29
memberDuffmanLight25-Jan-12 14:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 30 Sep 2010
Article Copyright 2010 by vpnchoudhary
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid