473,765 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 =
FormsAuthentica tion.HashPasswo rdForStoringInC onfigFile(thePa ssword,
"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.C reate("SHA512") ;

// Do a hash using the SHA512 Algorithm
hashedPassword = alg_SHA512.Comp uteHash(UTF8enc odedPassword);
hashValue = BitConverter.To String(hashedPa ssword);
lbl_SHA512_Alg_ usingBitConvert er.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.C reate("MD5");
HashAlgorithm alg_SHA1 = HashAlgorithm.C reate("SHA1");
HashAlgorithm alg_SHA512 = HashAlgorithm.C reate("SHA512") ;

hashValue =
FormsAuthentica tion.HashPasswo rdForStoringInC onfigFile(thePa ssword,
"MD5");
lbl_MD5_Forms.T ext = "MD5: " + thePassword + " = " +
hashValue;

hashValue =
FormsAuthentica tion.HashPasswo rdForStoringInC onfigFile(thePa ssword,
"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.UTF 8Encoding textConverter = new
System.Text.UTF 8Encoding();
byte[] UTF8encodedPass word =
textConverter.G etBytes(thePass word);
// Do a hash using the MD5 Algorithm
hashedPassword = alg_MD5.Compute Hash(UTF8encode dPassword);
hashValue = convertByteToHe xString(hashedP assword);
lbl_MD5_Alg.Tex t = "MD5 Algoritm: " + thePassword + " = " +
hashValue;

// Do a hash using the SHA1 Algorithm
hashedPassword = alg_SHA1.Comput eHash(UTF8encod edPassword);
hashValue = convertByteToHe xString(hashedP assword);
lbl_SHA1_Alg.Te xt = "SHA1 Algoritm: " + thePassword + " = " +
hashValue;
// Do a hash using the SHA512 Algorithm
hashedPassword = alg_SHA512.Comp uteHash(UTF8enc odedPassword);
hashValue2 = BitConverter.To String(hashedPa ssword);
hashValue = convertByteToHe xString(hashedP assword);
lbl_SHA512_Alg. Text = "SHA512 Algoritm: " + thePassword + " =
" + hashValue;
lbl_SHA512_Alg_ usingBitConvert er.Text = "SHA512 using
BitConverter: " + thePassword +
" = " + hashValue2;
}
// convertByteToHe xString
// Description: calls the overloaded function with no delimiter
protected string convertByteToHe xString(byte[] byteArray)
{
return convertByteToHe xString(byteArr ay, "");
}
// convertByteToHe xString
// Description: converts a byte array to a Hex String, using a
delimiter
protected string convertByteToHe xString(byte[] byteArray, string
delimiter)
{
string str_HexString = ""; // the
string where the hex value is stored
int lengthOfByteArr ay = byteArray.Lengt h;
int count = 1;

foreach (byte x in byteArray)
{
str_HexString += x.ToString("X2" ); // convert the
string to display in hex format
if (count++ < lengthOfByteArr ay)
{
str_HexString += delimiter;
}
}
return str_HexString;
}
Which gives me output that looks something like this:
MD5: test1 = 5A105E8B9D40E13 29780D62EA2265D 8A
SHA1: test1 = B444AC06613FC8D 63795BE9AD0BEAF 55011936AC
MD5 Algoritm: test1 = 5A105E8B9D40E13 29780D62EA2265D 8A
SHA1 Algoritm: test1 = B444AC06613FC8D 63795BE9AD0BEAF 55011936AC
SHA512 Algoritm: test1 =
B16ED7D24B3ECBD 4164DCDAD374E08 C0AB7518AA07F9D 3683F34C2B3C67A 15830268CB4A56C 1FF6F54C8E54A79 5F5B87C08668B51 F82D0093F7BAEE7 D2981181
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 convertByteToHe xString 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 3597
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
1252
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 the sql strin i get: "INSERT INTO werkboek VALUES 7, 2,5....;" I've changed te code for the placeholder {1} to {1:#.00} in order to change te output to 2.5 instead of 2,5 but no luck. Is there a other way to change ste code to get te requered...
6
2979
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 output; all lines begin with a period are command lines. The task of the program is to collect words from input lines, fill output lines with those words, adjust the margins, and print the result. Your programs should handle the commands shown...
3
2317
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 five weeks of sales for each category by using a conditonal format to highlight these top five weeks. I've been trying to find a way to do this but have not been sucessful looking through many books on Access and in reading the user groups. If...
6
2743
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 output; all lines begin with a period are command lines. The task of the program is to collect words from input lines, fill output lines with those words, adjust the margins, and print the result. Your programs should handle the commands shown...
11
5002
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 coding, and he coded last week.' ('coding', 'coded', 'week') expanded (for better visual): ('coding', 'coded', 'week')
6
1885
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 called: tempInput.txt, and then the information is calculated, and it is output on a different file called tempOutput.txt. The tempInput.txt containg those numbers as follows:
6
4408
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. Would you please tell me if the System.Security.Cryptography.HashAlgorithm.ComputeHash() generate exact same hash code in both versions of .Net for the same given byte?
139
14220
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
3495
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 there will
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.