473,836 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash String

Hello,

How can I hash a string in C#?
I suppose I should use MD5 ... at least is what is used in ASP.NET
membership.

I need to has a string and compare it to an already hashed string.

Thanks,
Miguel
Oct 8 '08 #1
6 3484
string me = "Pete";
int hashCode = me.GetHashCode( );

Am I missing something?

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Oct 8 '08 #2
"shapper" <md*****@gmail. comwrote in message
news:1f******** *************** ***********@8g2 000hse.googlegr oups.com...
How can I hash a string in C#?
I suppose I should use MD5 ... at least is what is used in ASP.NET
membership.

I need to has a string and compare it to an already hashed string.
I use this two functions to get the MD5 hash of either a string or an array
of bytes:

public static byte[] GetHash(byte[] data)
{
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash (data);
return result;
}
public static byte[] GetHash(string s)
{
byte[] myBytes = Encoding.UTF8.G etBytes(s);
return GetHash(myBytes );
}

Oct 8 '08 #3
If you want a one-way hash for passwords try this...

public static string EncodePassword( string userName, string password)
{
TripleDESCrypto ServiceProvider des;
MD5CryptoServic eProvider hashmd5;
byte[] pwdhash, buff;
hashmd5 = new MD5CryptoServic eProvider();
pwdhash = hashmd5.Compute Hash(ASCIIEncod ing.ASCII.GetBy tes(userName));
des = new TripleDESCrypto ServiceProvider ();
des.Key = pwdhash;
des.Mode = CipherMode.ECB; //CBC, CFB
buff = ASCIIEncoding.A SCII.GetBytes(p assword);
string result =
Convert.ToBase6 4String(des.Cre ateEncryptor(). TransformFinalB lock(buff, 0,
buff.Length));
return result;
}

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 8 '08 #4
Peter Morris wrote:
string me = "Pete";
int hashCode = me.GetHashCode( );

Am I missing something?
Yes. The result of GetHashCode is subject to change, even between
subsequent runs of the same program. Only while the program is still
running are you guaranteed consistent results. So it's useless for
comparing against a saved hash.
Oct 9 '08 #5
On Oct 9, 7:08*pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
Peter Morris wrote:
string me = "Pete";
int hashCode = me.GetHashCode( );
Am I missing something?

Yes. *The result of GetHashCode is subject to change, easicallyven between
subsequent runs of the same program. *Only while the program is still
running are you guaranteed consistent results. *So it's useless for
comparing against a saved hash.
Basically, in this case I am sending the user a confirmation email
after she/he registers on a web site.
On the email is the url. Something has follows:

www.mydomain.com/ConfirmPassword/?key=...

where key is an hash of a string created by joining the user UserName,
Accoount CreateDate, ...

So when the user goes to that URL I hash the same string again and
confirm the two to activate the account.

Isn't this possible?

Thanks,
Miguel
Oct 9 '08 #6
shapper <md*****@gmail. comwrote:
Basically, in this case I am sending the user a confirmation email
after she/he registers on a web site.
On the email is the url. Something has follows:

www.mydomain.com/ConfirmPassword/?key=...

where key is an hash of a string created by joining the user UserName,
Accoount CreateDate, ...

So when the user goes to that URL I hash the same string again and
confirm the two to activate the account.

Isn't this possible?
Yup, and MD5 is probably the best bet for that.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 9 '08 #7

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

Similar topics

3
3345
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to substitute the set<Data*> with a hash_set<Data*>: typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet; // typedef set<Data*> DataPointerHashSet; // (see complete code below) But it doesn't work! Everything is fine...
47
5115
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
2
3798
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
7
17096
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the MD5 is computing the right Byte for the hash when compared to other MD5 hash generators online. However, when I attempt to convert it back to tring using the line String outputData = textConverter.GetString( result ) ; I essentially get...
24
4322
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
21
3234
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
1
3601
by: Wayne Deleersnyder | last post by:
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. ....
15
37603
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code Different-2 string.
2
5835
by: joe shoemaker | last post by:
I would like to convert url into md5 hash. My question is that md5 hash will create collision at 2^64. If you do long(value,16), where value is the md5 hash string, would value returned from long(value, 16) be unique as long as md5 hashed string is unique? when you move md5 hashed string to long, where will the collision occur, at anything hash = md5.new() hash.update("some_url_") value = hash.digest() value_in_int = long(value, 16)...
8
2782
by: Jim Cobban | last post by:
I am writing a program in which I need a hash table implementation of a Map. The version of g++ available for Windo$e does not yet include the TR1 support for this. It just has the original SGI implementation. However the SGI implementation is so old that it predates the STL, so it does not, among other things, include hash support for std::basic_string. So I tried implementing the hash map from Stroustrup 3rd edition. At the least I...
0
9825
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9671
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,...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9379
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...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
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
3
3116
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.