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

Difference between GetHashCode() method and ComputeHash()

Hi,
I am trying to encrypt password for a user record in a sql database. I
want to know what is difference between or which is better and
efficient for encryption GetHashCode() method from object class or
ComputeHash() method from MD5CryptoServiceProvider class.
Jun 27 '08 #1
2 3526
On May 20, 11:12 am, Prasad <mahashabdepra...@gmail.comwrote:
Hi,
I am trying to encrypt password for a user record in a sql database. I
want to know what is difference between or which is better and
efficient for encryption GetHashCode() method from object class or
ComputeHash() method from MD5CryptoServiceProvider class.
GetHashCode is a simple hash, approriate for use in a Hashtable (or
generic Dictionary), but should not be considered cryptographically
secure.

ComputeHash of a HashAlgorithm is a secure hash (i.e. cannot be easily
reversed), approriate for storing passwords in a database.
SHA1 is preferred to MD5 (http://en.wikipedia.org/wiki/
Md5#Vulnerability)

So for passwords you should use something like:

using System.Security.Cryptography;
using System.Text;
//...
public static byte[] GetPasswordHash(string password)
{
HashAlgorithm algorithm = SHA1.Create();
byte[] data = Encoding.Unicode.GetBytes(password);
return algorithm.ComputeHash(data);
}

Terry.
Jun 27 '08 #2
On May 20, 3:56*pm, "rogers.te...@gmail.com" <rogers.te...@gmail.com>
wrote:
On May 20, 11:12 am, Prasad <mahashabdepra...@gmail.comwrote:
Hi,
I am trying to encrypt password for a user record in a sql database. I
want to know what is difference between or which is better and
efficient for encryption GetHashCode() method from object class or
ComputeHash() method from MD5CryptoServiceProvider class.

GetHashCode is a simple hash, approriate for use in a Hashtable (or
generic Dictionary), but should not be considered cryptographically
secure.

ComputeHash of a HashAlgorithm is a secure hash (i.e. cannot be easily
reversed), approriate for storing passwords in a database.
SHA1 is preferred to MD5 (http://en.wikipedia.org/wiki/
Md5#Vulnerability)

So for passwords you should use something like:

using System.Security.Cryptography;
using System.Text;
//...
public static byte[] GetPasswordHash(string password)
{
* HashAlgorithm algorithm = SHA1.Create();
* byte[] data = Encoding.Unicode.GetBytes(password);
* return algorithm.ComputeHash(data);

}

Terry.
Thank you Terry, I will try it.
Jun 27 '08 #3

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

Similar topics

7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
2
by: One Handed Man [ OHM# ] | last post by:
The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
3
by: =?Utf-8?B?QWJlUg==?= | last post by:
I am trying to look for change in an instanciated object that contains a generic list<>. I first get an int value of objA.GetHashCode(). I add a few objects to the list<collection in objA I...
4
by: Andrew Robinson | last post by:
I have a class that has three properties: two of type int and one of type string. Is this the best method when overriding the GetHashCode() ? I am guessing not... any thing better? public...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
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: 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...
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
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...
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
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.