473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Formatting the output of hash values

Hi All,

I was going to write and ask if someone could help me fix the
formatting of my output for hash values, but I believe I got it right
now. But, because I couldn't find any website or tutorial to help me
with this issue I figured I'd make a post just in case someone else
runs into the same issue.

....

This past weekend I was just messing around learning about encryption
and hash values. I need hash values for a database I'm making. But,
along the way I came across MD5, SHA1, SHA2(256, 384, and 512
variants), plus others. I read that SHA2 is soon to be the new
defacto hash algorithm to use because of some identified weaknesses
(although from the looks of it, you'll need a pretty nice computer to
actually start cracking SHA1). Anyways, I figured I'd use SHA2
(SHA512).

C# has a nice function for creating a hash value all in one step:

string hashValue =
FormsAuthentication.HashPasswordForStoringInConfig File(thePassword,
"SHA1");

Although you can use "MD5" or "SHA1." But nothing else for the SHA2
stuff.

So eventually I started messing around and put together this little
snippet of code:
....
using System.Security.Cryptography;

string hashValue;
string thePassword = "test1";
byte[] hashedPassword;
HashAlgorithm alg_SHA512 = HashAlgorithm.Create("SHA512");

// Do a hash using the SHA512 Algorithm
hashedPassword = alg_SHA512.ComputeHash(UTF8encodedPassword);
hashValue = BitConverter.ToString(hashedPassword);
lbl_SHA512_Alg_usingBitConverter.Text = "SHA512 using BitConverter: "
+ thePassword + " = " + hashValue;

But this would output a value something like this:
SHA512 using BitConverter: test1 = B1-6E-D7-D2-4B-3E-CB-D4-16-4D-CD-
AD-37-4E-08-C0-AB-75-18-AA-07-F9-D3-68-3F-34-C2-B3-C6-7A-15-83-02-68-
CB-4A-56-C1-FF-6F-54-C8-E5-4A-79-5F-5B-87-C0-86-68-B5-1F-82-
D0-09-3F-7B-AE-E7-D2-98-11-81
I didn't like the hyphens, so after doing some digging around, I ended
up with this...
....
using System.Security.Cryptography;
protected void hashThis()
{

string hashValue;
string hashValue2 = "";
string thePassword = "test1";
byte[] hashedPassword;

HashAlgorithm alg_MD5 = HashAlgorithm.Create("MD5");
HashAlgorithm alg_SHA1 = HashAlgorithm.Create("SHA1");
HashAlgorithm alg_SHA512 = HashAlgorithm.Create("SHA512");

hashValue =
FormsAuthentication.HashPasswordForStoringInConfig File(thePassword,
"MD5");
lbl_MD5_Forms.Text = "MD5: " + thePassword + " = " +
hashValue;

hashValue =
FormsAuthentication.HashPasswordForStoringInConfig File(thePassword,
"SHA1");
lbl_SHA1_Forms.Text = "SHA1: " + thePassword + " = " +
hashValue;
// Convert the password into a byte array so it can be
handled by the hash algorithm(s)
System.Text.UTF8Encoding textConverter = new
System.Text.UTF8Encoding();
byte[] UTF8encodedPassword =
textConverter.GetBytes(thePassword);
// Do a hash using the MD5 Algorithm
hashedPassword = alg_MD5.ComputeHash(UTF8encodedPassword);
hashValue = convertByteToHexString(hashedPassword);
lbl_MD5_Alg.Text = "MD5 Algoritm: " + thePassword + " = " +
hashValue;

// Do a hash using the SHA1 Algorithm
hashedPassword = alg_SHA1.ComputeHash(UTF8encodedPassword);
hashValue = convertByteToHexString(hashedPassword);
lbl_SHA1_Alg.Text = "SHA1 Algoritm: " + thePassword + " = " +
hashValue;
// Do a hash using the SHA512 Algorithm
hashedPassword = alg_SHA512.ComputeHash(UTF8encodedPassword);
hashValue2 = BitConverter.ToString(hashedPassword);
hashValue = convertByteToHexString(hashedPassword);
lbl_SHA512_Alg.Text = "SHA512 Algoritm: " + thePassword + " =
" + hashValue;
lbl_SHA512_Alg_usingBitConverter.Text = "SHA512 using
BitConverter: " + thePassword +
" = " + hashValue2;
}
// convertByteToHexString
// Description: calls the overloaded function with no delimiter
protected string convertByteToHexString(byte[] byteArray)
{
return convertByteToHexString(byteArray, "");
}
// convertByteToHexString
// Description: converts a byte array to a Hex String, using a
delimiter
protected string convertByteToHexString(byte[] byteArray, string
delimiter)
{
string str_HexString = ""; // the
string where the hex value is stored
int lengthOfByteArray = byteArray.Length;
int count = 1;

foreach (byte x in byteArray)
{
str_HexString += x.ToString("X2"); // convert the
string to display in hex format
if (count++ < lengthOfByteArray)
{
str_HexString += delimiter;
}
}
return str_HexString;
}
Which gives me output that looks something like this:
MD5: test1 = 5A105E8B9D40E1329780D62EA2265D8A
SHA1: test1 = B444AC06613FC8D63795BE9AD0BEAF55011936AC
MD5 Algoritm: test1 = 5A105E8B9D40E1329780D62EA2265D8A
SHA1 Algoritm: test1 = B444AC06613FC8D63795BE9AD0BEAF55011936AC
SHA512 Algoritm: test1 =
B16ED7D24B3ECBD4164DCDAD374E08C0AB7518AA07F9D3683F 34C2B3C67A15830268CB4A56C1FF6F54C8E54A795F5B87C086 68B51F82D0093F7BAEE7D2981181
SHA512 using BitConverter: test1 = B1-6E-D7-D2-4B-3E-CB-D4-16-4D-CD-
AD-37-4E-08-C0-AB-75-18-AA-07-F9-D3-68-3F-34-C2-B3-C6-7A-15-83-02-68-
CB-4A-56-C1-FF-6F-54-C8-E5-4A-79-5F-5B-87-C0-86-68-B5-1F-82-
D0-09-3F-7B-AE-E7-D2-98-11-81
So in the end, the convertByteToHexString method creates a string
without the hyphens. Actually I can set my own delimiters this way if
I wish. Anyways... that's what I ended up with. If that helps
anyone, great. If you see errors, or I made a BIG booboo, please let
me know.

Later,
Wayne D.

Mar 11 '07 #1
1 3574
2 points; first: this would be a good candidate for StringBuilder
rather than concatenation (otherwise you end up with a lot of
intermediary string objects floating around waiting to be GCd)
second: hex encoding is fine, but you may also wish to look at base
64; this would save you a bit of space (as if that mattered these
days) and complexity (more important); look at:

http://msdn2.microsoft.com/en-us/lib...e64string.aspx
and
http://msdn2.microsoft.com/en-us/library/dhx0d524.aspx

Marc

Mar 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Highpotech IT | last post by:
i'am using the following statement to format a aql string sql=string.format("INSERT INTO werkboek VALUES {0}, {1}....;" m_WNR, m_Aantal...) Aantal is a decimal and evaluates into 2.5D. Evalutating...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
3
by: Chuck Reed | last post by:
I am working on a sales report where I show weekly sales by category for each of the 52 weeks in the year. Each record in my table/report has the 52 weeks of sales in it. I want to highlight to top...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
6
by: Hongbo | last post by:
Hi, I use System.Security.Cryptography.HashAlgorithm.ComputeHash() method with SHA512 to encrypt password. I recently upgrade my website from .Net 1.1 to .Net 2.0. The passwords stop working....
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
8
by: victor.herasme | last post by:
Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.