Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Dear all,

I have started on creating hashing password and I am little unsure, how do i go about creating and storing the hash password in the database. I am trying to encrypt my username and password, when i send them over a web-service.

User class:
C#
     public string CalculateHash(string text)
{
    MD5 md5 = new MD5CryptoServiceProvider();

    //compute hash from the bytes of text
    md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

    //get hash result after compute it
    byte[] result = md5.Hash;

    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < result.Length; i++)
    {
        //change it into 2 hexadecimal digits
        //for each byte
        strBuilder.Append(result[i].ToString("x2"));
    }

    return strBuilder.ToString();
}

public api_login Validate2(string userName, string Password)
{
    // Find a user that matches that username and password (this will only validate if both match)
    return db.api_login.FirstOrDefault(u => u.username == userName && u.password == Password);

}



if anyone would suggest any feedback, into how can i get the password to hash in the database, when the user login, do I do that in the basic auth class or user class.

Many thanks

Many thanks
Posted
Updated 20-Mar-14 8:00am
v3
Comments
Sergey Alexandrovich Kryukov 19-Mar-14 12:10pm    
You are doing gibberish. Why would you use base64, ever. You are not even trying to calculate cryptographic hash.
—SA
miss786 19-Mar-14 12:15pm    
hi, I currently new into this encryption code, if you can advise a better alternative, then I happy to look into and update my code. any feedback is much welcomed.
Sergey Alexandrovich Kryukov 19-Mar-14 12:17pm    
I just did; did you notice Solution 2? The answer is pretty comprehensive; this is all you need.
Will you accept it formally (green "Accept" button)?
—SA
ZurdoDev 19-Mar-14 21:07pm    
I looked at both solutions and am not convinced either one actually answers your question. If not, please respond and elaborate on what exactly you are looking for.
miss786 20-Mar-14 6:33am    
Thank you so much for your response ryan. I am trying to encrypt the password stored in my database, when i send them over using web api (http). From the suggestions below, I am currently looking into "SHA512 or SHA256:" hash algorithms. I manage to create crypto class using the following code as a framework:(http://www.java2s.com/Code/CSharp/Security/CreatePasswordHash.htm.)

I am still little unsure of the process of, how do I create hash password when i send the username and password over the web api, and when would i store the hash password in the database. I already has existing users in the database which do not have hash password, so is it possible to encrypt their password, when sending details through web api. I hope this clarifies any misunderstanding regarding my problem.

Any help much appreciated.

Please see my comment to the question. It is unclear what you are trying to do, but it's clear why. This is a good idea to use cryptographic hash function for authentication: http://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

First of all, you should not use a hash algorithm from SHA-1 family or MD5; they are considered as broken, not suitable for passwords. Better use one of the SHA-2 algorithms. Please see:
http://en.wikipedia.org/wiki/MD5[^],
http://en.wikipedia.org/wiki/SHA-1[^],
http://en.wikipedia.org/wiki/SHA-2[^].

The .NET FCL implementations are here: http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm%28v=vs.110%29.aspx[^].

Better use SHA512 or SHA256:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha512(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha256(v=vs.110).aspx[^].

I never heard that using any of these implementation created any problems.

—SA
 
Share this answer
 
v2
Don't encrypt usernames or passwords, and do remember that Base64 is not an encryption method, or a hashing algorithm, or anything useful in this context!

Have a look at this: Password Storage: How to do it.[^] - it should help!
 
Share this answer
 
Comments
miss786 19-Mar-14 13:39pm    
Thank you so much for your feedback. I am still little unclear, how do i go about storing the hash password or encrypting the passwords, when sending them over the http. if you get time, could you provide some guide or reference to any learning material to look into. Many thanks.
OriginalGriff 19-Mar-14 15:03pm    
You can't, in practice, unless you use HTTPS and for that you need to buy a certificate.
The problem is that encryption needs a key - and both ends need to know it. That means that the key needs to be transferred from the Server to the Client - which means the encryption is about as useful as a chocolate teapot.

If you need to secure logins, then you have to move to secure data r=transfer - and that means HTTPS just like the banks and shopping sites use. (And if you don't, then why would any user trust your login system to be secure anyway? If it isn't green in the address bar, it isn't secure!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900