A Dutch software developer living in Chile
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 26 | 27 | 28 | 29 | 30 | 31 | 1 | | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 23 | 24 | 25 | 26 | 27 | 28 | 29 | | 30 | 31 | 1 | 2 | 3 | 4 | 5 |
Search
Navigation
Categories
Blogroll
|

Monday, August 03, 2009
How to get a SHA1 hash from a file in C#.Net
A simple way to get a hash code from a file:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Sha1
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: SHA1 <filename>");
return;
}
string fileName = args[0];
if (!File.Exists(fileName))
{
Console.WriteLine("File not found: {0}", fileName);
return;
}
using (Stream s = File.OpenRead(fileName))
{
SHA1Managed sha = new SHA1Managed();
byte[] result = sha.ComputeHash(s);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.AppendFormat("{0:x2}", result[i]);
}
Console.WriteLine("SHA1 Hash: {0}", sb);
}
}
}
}
Monday, August 03, 2009 9:22:14 AM (Pacific SA Standard Time, UTC-04:00)