Post: How To Produce A Hash In C#
02-28-2013, 05:48 AM #1
Pichu
RIP PICHU.
(adsbygoogle = window.adsbygoogle || []).push({}); How To Produce A Hash In C#

Fee; free to view the tutorial and more tutorials on my site: https://www.tutorialsmadesimple.org/


You are going to need to import System.Security.Cryptography. This will allow you to use classes for getting hash values of data.

    using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Hashing
{
class Program
{
static void Main(string[] args)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString()))));
}
Console.ReadKey();//Stops the command prompt from exiting
} //End Main
} //End Class
}


You must login or register to view this content.



This source uses 8 bit encoding along with the MD5 hashing method. You can replace MD5 with SHA1 and get the same affect but different outputs.

You should notice that with the outputs, the hashes have this format:
05-AF-83

If you want to remove the dashes, you will need to do this:




    using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Hashing
{
class Program
{
static void Main(string[] args)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
for (int i = 0; i < 10; i++)
{
string hash, cleanhash;
hash = cleanhash = "";
hash = BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString())));
foreach (char a in hash)
{
if (a.ToString() != "-")
{
cleanhash += a;
}
}
Console.WriteLine(cleanhash);
}
Console.ReadKey();//Stops the command prompt from exiting
} //End Main
} //End Class
}


You must login or register to view this content.

The following 5 users say thank you to Pichu for this useful post:

ResistTheMoon, HonuCinema, iMCSx, Darth Saul
03-02-2013, 01:16 AM #2
Originally posted by Pichu View Post
How To Produce A Hash In C#

Fee; free to view the tutorial and more tutorials on my site: https://www.tutorialsmadesimple.org/


You are going to need to import System.Security.Cryptography. This will allow you to use classes for getting hash values of data.

    using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Hashing
{
class Program
{
static void Main(string[] args)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString()))));
}
Console.ReadKey();//Stops the command prompt from exiting
} //End Main
} //End Class
}


You must login or register to view this content.



This source uses 8 bit encoding along with the MD5 hashing method. You can replace MD5 with SHA1 and get the same affect but different outputs.

You should notice that with the outputs, the hashes have this format:
05-AF-83

If you want to remove the dashes, you will need to do this:




    using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Hashing
{
class Program
{
static void Main(string[] args)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
for (int i = 0; i < 10; i++)
{
string hash, cleanhash;
hash = cleanhash = "";
hash = BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString())));
foreach (char a in hash)
{
if (a.ToString() != "-")
{
cleanhash += a;
}
}
Console.WriteLine(cleanhash);
}
Console.ReadKey();//Stops the command prompt from exiting
} //End Main
} //End Class
}


You must login or register to view this content.




you could just do this since the string will always be in the same win format:

BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString()))).Replace("-","");
03-02-2013, 04:42 AM #3
Pichu
RIP PICHU.
Originally posted by Jakes625PS3 View Post
you could just do this since the string will always be in the same win format:

BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(i.ToString()))).Replace("-","");


True, I actually didn't think of that. :P Well, at least I am showing users how to do a foreach loop.

The following user thanked Pichu for this useful post:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo