473,382 Members | 1,369 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,382 software developers and data experts.

How to get a hashed string

ad
I want to hash a givien string to a hashed string.
How can I do witth c#?
Jun 27 '08 #1
3 1766
I'm assuming by "hashed" you mean for storage purposes - i.e. not
simply .GetHashCode(); anyway, picking the simplest option (MD5),
something like:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
static class Program
{
static void Main()
{
string text = "abcdefghijklmnopqrstuvwxyz";
MD5CryptoServiceProvider crypt = new
MD5CryptoServiceProvider();
byte[] inputBuffer = Encoding.UTF8.GetBytes(text);
byte[] hashBuffer = crypt.ComputeHash(inputBuffer);

// result as base-64 (easy & shorter result)
string hashBase64 = Convert.ToBase64String(hashBuffer);

// result as hex-string (more common)
StringBuilder sb = new StringBuilder(hashBuffer.Length * 2);
foreach (byte b in hashBuffer)
{
sb.Append(b.ToString("x2"));
}
string hashHex = sb.ToString();
}
}

Note, however, that MD5 is not very sucure; there are databases
dedicated to known MD5 values, so it shouldn't be used to hash
passwords or credit-card numbers, etc. For secure applications, you'd
want to look at a keyed algorithm - such as any of the HMACSHA[n]
implementations:

http://msdn2.microsoft.com/en-us/lib....hmacsha1.aspx

Marc
Jun 27 '08 #2
string s = "Hello";
int hash = s.GetHashCode();
Jun 27 '08 #3
ad wrote:
I want to hash a givien string to a hashed string.
How can I do witth c#?
private static readonly Encoding utf8 = new UTF8Encoding();
private static readonly MD5 md5 = new MD5CryptoServiceProvider();
private static string MD5Calc(string s)
{
byte[] binres = md5.ComputeHash(utf8.GetBytes(s));
StringBuilder res = new StringBuilder("");
for(int i = 0; i < binres.Length; i++)
{
res.Append(String.Format("{0:x2}", binres[i]));
}
return res.ToString();
}

eller

private static readonly Encoding utf8 = new UTF8Encoding();
private static readonly MD5 md5 = new MD5CryptoServiceProvider();
private static string MD5Calc(string s)
{
byte[] binres = md5.ComputeHash(utf8.GetBytes(s));
return Convert.ToBase64String(binres);
}

depending on whether you want Hex or Base64 encoding.

Note that MD5 is not particular secure any more and for
some purposes SHA-256 should be used instead.

Arne
Jun 27 '08 #4

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
1
by: Daniel | last post by:
By using this code I made an array from a hashed array to regain a table content and layout as received from a mysql database... my $sth = $dbh->prepare("SELECT * FROM `Groepen';");...
1
by: Jake | last post by:
I have a deadlock situation, part of the problem looks like this; spid ecid dbid ObjId IndId Type Resource Mode Status ------ ------ ------ ----------- ------ ----...
4
by: Bill Borg | last post by:
Hello, I am using hashpasswordforstoringinconfigfile with md5 to generate a 32-character hashed password. But, since that string is only hex characters and is really only 128 bits (16 bytes),...
4
by: ad | last post by:
I used the function below to convert a string to a hashed string. But I found whaterver string I input, I the output hashed sring always have a "=" character ending. Is there any problem about...
3
oranoos3000
by: oranoos3000 | last post by:
hi I have a page for registration in my site in this page after validation form I cashed password of user by function md5 and then this hashed password save in database I want to recycled the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.