user2783972
user2783972

Reputation: 11

Calculating SHA1 From Hex/Binary String in C#

After 24 hours worth of programming I finally cracked, I want to make a C# WindowsForm application that shows you the value of a hashed hexstring input like in hashcalc. I could only make it for textstring input even after googling it. To demonstrate, the input 060201080808040602040909080909003583150369840500 should output d8f6b336a4df3336bf7de58a38b1189f6c5ce1e8 and not a6879cb4510b18e8f41b3491ce474fd2ff9e2979 Also this is for SHA1 Hashing so keep it only at that, Thanks!

Upvotes: 0

Views: 3183

Answers (4)

soft
soft

Reputation: 1

private void button1_Click(object sender, EventArgs e)
{
    string input= "060201080808040602040909080909003583150369840500";
    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
    byte[] hash = sha1.ComputeHash(ConvertHexStringToByteArray(input));
    string delimitedHexHash = BitConverter.ToString(hash);
    string hexHash = delimitedHexHash.Replace("-", "");

    MessageBox.Show(hexHash); 
}

public static byte[] ConvertHexStringToByteArray(string hexString)
{
    if (hexString.Length % 2 != 0)
    {
        throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
    }

    byte[] HexAsBytes = new byte[hexString.Length / 2];
    for (int index = 0; index < HexAsBytes.Length; index++)
    {
        string byteValue = hexString.Substring(index * 2, 2);
        HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
    }

    return HexAsBytes;
}

Upvotes: 0

user2802887
user2802887

Reputation: 11

For this 090505050509050009080003000605003569190380108300 I have 3b8d562adb792985a7393a6ab228aa6e7526410a, not 3b8d562adb792985a7393a6ab228aa6e752641a

I think that the last byte is wrong.

Upvotes: 1

Cristian Lupascu
Cristian Lupascu

Reputation: 40576

You need to import the namespace:

using System.Security.Cryptography

and call

var hash = new SHA1CryptoServiceProvider().ComputeHash(inputBytes);

to produce the hash.

If your problem is about converting the hex string to bytes, here's a full sample that shows how to parse the input and format the output:

var input = "060201080808040602040909080909003583150369840500";

// parse the input into a byte[]
var inputBytes = Enumerable.Range(0, input.Length/2)
                           .Select(i => input.Substring(i*2, 2))
                           .Select(s => byte.Parse(s, NumberStyles.HexNumber))
                           .ToArray();

var hash = new SHA1CryptoServiceProvider().ComputeHash(inputBytes);

var outputHexString = string.Join(" ", 
    hash.Select(b => b.ToString("X")).ToArray());

Console.WriteLine(outputHexString);

Here's how it works: http://ideone.com/BE7ecU

Upvotes: 0

HaMster
HaMster

Reputation: 543

I think I don't really understand your problem. You can hash any input and output it any way you want. To achieve this, you are likely to use the Encoding class with an encoding of your choice and call the GetBytes() method. Then you take the SHA1 class and let it calculate the hash value. And for user text you tell the string class to use hex formatting for numbers. And this not only applies to the SHA1 class ;)

Upvotes: 0

Related Questions