Re: Hash MD5, Sha1 and Length

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sep 13, 7:29 pm, rossum <rossu...@xxxxxxxxxxxx> wrote:
On Sun, 13 Sep 2009 08:42:14 -0700 (PDT), shapper <mdmo...@xxxxxxxxx>
wrote:

So for saving user passwords on a database I should use Sha256
correct?

For saving passwords on a database you need to use SHA-256,
cryptographic salt (http://en.wikipedia.org/wiki/Salt_(cryptography))
and stretching (http://en.wikipedia.org/wiki/Key_strengthening).
These are standard cryptographic methods for protecting bulk passwords
in a database or similar.  See PKCS#5
(ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2-0.pdf), sections
4 and 5 for more detail.

If you are doing security then it is critical to get it right.



I was using MD5 or Sha1 because those were the options on the ASP.NET
Membership providers.
But now I creating my own custom Membership system based only on
FormsAuthentication.

I did a little search and I found a few more options and updated my
code with your suggestions:

  public static String Hash(this String value, HashType hashType) {

     HashAlgorithm algorithm;
     switch (hashType) {
       case HashType.MD5:
         algorithm = new MD5CryptoServiceProvider();
         break;
       case HashType.SHA1:
         algorithm = new SHA1CryptoServiceProvider();
         break;
       case HashType.SHA256:
         algorithm = new SHA256CryptoServiceProvider();
         break;
       case HashType.SHA384:
         algorithm = new SHA384CryptoServiceProvider();
         break;
       case HashType.SHA512:
         algorithm = new SHA512CryptoServiceProvider();
         break;
       default:
         throw new ArgumentException("Invalid hash type", "type");
     }
     UTF8Encoding encoder = new UTF8Encoding();
     Byte[] hash = algorithm.ComputeHash(encoder.GetBytes(value));

This is insufficient.  Use something more like:

  Byte[] hash = ComputeHash( password | salt );  // | = concatenate
  for (int 1 = 0; i < 10000; ++i)
    hash = ComputeHash( hash | salt );  // | = concatenate

The salt prevents two people using the same password having the same
hash and also stops an attacker pre-calculating hashes for commonly
used passwords.  The repeats (10,000 or whatever) increase the
workload for any attacker trying to guess passwords.  Aim to set the
number of repeats so that it takes about 0.25 seconds to calculate a
hash.  That way the attacker can only try guessing four possible
passwords per second per PC, while users only get a minor delay when
signing on.

You will need to store the salts alongside the hashes as they are all
different.  The repeat count is a single variable.  It theory it
should be reviewed every few years and increased in line with
computing power.  You should also review the hash function used to see
if it has become obsolete.  NIST is currently running a competition to
find a new hash algorithm to replace the SHA2 series, which includes
SHA-256.  Google "SHA 3 competition" if you are interested.

rossum

     return BitConverter.ToString(hash).Replace("-", String.Empty);

   } // Hash

If you notice something wrong, please, let me know.

Thank You,
Miguel

Hi,

What you mean is the following correct:
http://www.obviex.com/samples/hash.aspx

Please, if you know better code to do this let me know.
I am not very familiar with this but I have been reading the links you
sent me and searched for the competition.

Then for each user I save in the database along with the resulting
password hash, its salt to I can use it later to compare the passwords
on authentication, correct?

Thanks,
Miguel
.



Relevant Pages

  • Re: Decrypt fails
    ... I am creating a MD5 hash data and then using it to derive a key ... (CALG_RC2 encryption algorithm). ... My requirement concerns more with not storing passwords in plain ... > that he provided and compare it to the hash in the database. ...
    (microsoft.public.platformsdk.security)
  • Hash and Salt
    ... We have a .NET application that uses salt and hash to store encrypted ... we need to receive "new" passwords from ... an informix database which will be sending us the original password unhashed ...
    (microsoft.public.sqlserver.programming)
  • Re: UsernameTokenManager and a hashed password database
    ... > (receiving hashed passwords) to work if the password database itself ... > My password database is hashed with SHA-1 and the passwords are ... > combined with a salt value before hashing. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: Hash MD5, Sha1 and Length
    ... These are standard cryptographic methods for protecting bulk passwords ... algorithm = new MD5CryptoServiceProvider; ... The salt prevents two people using the same password having the same ... hash and also stops an attacker pre-calculating hashes for commonly ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Perl Script
    ... It uses a one way hash. ... AD> just store the encrypted result in the database. ... AD> extract it and reverse the encryption. ... Hashing passwords is much safer than reversible encryption (regardless ...
    (comp.lang.perl.misc)