SHA1 Hash in C#


I recently found myself with a need to generate a SHA1 hash that was identical to the one outputted by the PHP function SHA1(), based on any input string.

After a bit of googling, and some jiggerypokery, I finally came up with this:

using System;
using System.Text;
using System.Security.Cryptography;
static string SHA1(string str)
{
byte[] buffer = Encoding.ASCII.GetBytes(str);
SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider();
buffer = hasher.ComputeHash(buffer);
string hash = "";
for (var i = 0; i < buffer.Length; i++)
{
hash = hash + buffer[i].ToString("X");
}
return hash.ToLower();
}

This generates a string exactly matching the ouput of the PHP function SHA1(), ie. 40 character lowercase hexadecimal number in a string.

I realise there are some optimisations that could be done, for instance the string concactenation in the loop isn't a brilliant way of building it, but it was quick and dirty, I may optimise it later. The above code is available to anyone for any reason, but please give credit where credit is due.

Here is a command line program that takes any number of strings as input, and outputs each one's SHA1 hash. This program is licensed under Creative Commons: Creative Commons License

SHA1.exe
MD5: 93058b515974406144a2ab7fed863fbd
SHA1: 0d3dc09ab773800ada682a4c590cf78f00e76f3e

Usage example
>sha1.exe this is a test
this
c2543fff3bfa6f144c2f6a7de6cd1cb65cae
is
b47f363e2b43c647f14deea3eced9bef30ce
a
86f7e437faa5a7fce15d1ddcb9eaeaea377667b8
test
a94a8fe5ccb19ba61c4c873d391e987982fbbd3

An altogether simpler way...


Most URL "prettification" techniques on Apache servers involve either mod_rewrite, which is good in theory, but really difficult in practice, or the use of PATH because of Apache's search behaviour, but usually it is complained that there is still say page.php in the url.

Today I offer varient on the PATH variety of "prettiness" using a single directive in a .htaccess

DefaultType application/x-httpd-php

This line means that if a file has no extension, or an unknown extension, it will be interpreted as PHP. Thus if we make a file called, say content, and give it no extension, it would be run as a PHP script, and we can then use the PATH method to add in parameters and such. Granted this doesn't really add anything new to the already existing technique, other than solving the horrible .php extension problem, but it does offer a simpler and equivalent solution to the mod_rewrite method.

Alpha v0.3.1 released


I've released Alpha 0.3.1 on Google Code. Sorry I didn't announce 0.3, I was a bit swamped. Either way, there are a few updates to make:

  • Winamp support
  • Now (nearly) fully .NET coding practices compliant (new in 0.3.1)
  • Major API change: Manager object no longer exists, controllers are public classes in the Controllers namespace
  • Unused exceptions removed
  • Interop.iTunesLib.dll is now merged into the main assembly for release (compiling from scratch will still produce the file)
  • Project Type is now "empty", so the My namespace is not created at all

And now a few thanks:
To the Microsoft FxCop team, for producing a fanatastic product that pointed out a lot of flaws in the library, such as poorly done exceptions, poor structure equality handling and so on.
And to Mike Barnett of Microsoft Research, for creating the brilliant ILMerge tool, which allowed me to finally do away with Interop.iTunesLib.dll and merge it with the main assembly, Alpha is now distributed as a single DLL due in whole to him!

And as a quick last note, the DLL is now labelled Alpha, rather than alphamedia (it was only alphamedia to reflect the Google Code project name)

Alpha version 0.2.0 released


I've just updated the source code for Alpha, and provided new binaries.

Changes:
  • Now linked against .NET 2.0 instead of 3.5
  • Major internal architectural changes, however very little change to existing API
  • Windows Media Player support
I'll be updating the documentation in the next few days, along with support for one or two more players.

Droooool....


....Windows 7....

It has to be said that it is the first thing that Microsoft has done right the first time in a long time. It is what Vista should have been. Words cannot describe how good I think it is at the moment, but a full review is to follow soon enough!

Page: 1 - 2 - 3