473,793 Members | 2,927 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 3481
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
3341
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
5093
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
3792
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
17076
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
4313
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
3227
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
3599
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
37598
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
5829
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
2777
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
9670
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
9518
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
10430
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10159
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
10000
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...
1
7538
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...
1
4111
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
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
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.