Re: Hash MD5, Sha1 and Length
- From: rossum <rossum48@xxxxxxxxxxxx>
- Date: Sun, 13 Sep 2009 23:31:51 +0100
On Sun, 13 Sep 2009 13:34:30 -0700 (PDT), shapper <mdmoura@xxxxxxxxx>
wrote:
Hello,Salt must always be present, either generated or retrieved form the
I tried to create the extension with and without salt and with
optional encoding.
database. Reduce the number of options available, that way the users
have less chances of picking the wrong options.
I also made the salt as OUT so if the user does not provide a saltAlways generate the salt internally, the users never need to see it.
value then it will be generated and updated.
You generate it, you save it to the database and you retrieve it when
needed.
This is useful to save the Salt in a database.Unwise. A string encoding is not completely random and a salt should
1. Can the salt parameter be a String?
be as random as possible.
Then inside the extension I would convert the salt to bytes[] orPass.
create a random in bytes and at the end convert it to string.
I think it would be more user friendly.
2. What is the difference between using SHA256Managed() or
SHA256CryptoServiceProvider() ?
Which one should I use?You need to stretch the calculation by repeating it many times.
Could someone, please, help me improve my code?
I am not so familiar with Cryptography ...
This is the code I have so far:
public static String Hash(this String value, HashType type) {
return Hash(value, type, new UTF8Encoding());
} // Hash
public static String Hash(this String value, HashType type,
Encoding encoding) {
// Hash value
HashAlgorithm algorithm;
switch (type) {
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");
}
Byte[] hash = algorithm.ComputeHash(encoding.GetBytes(value));
return BitConverter.ToString(hash).Replace("-", String.Empty);Do not allow the user to select the hash function. They can have any
} // Hash
hash function they want as long as it is SHA256.
This is needless complication again. Short salts are dangerous. Pick
public static String Hash(this String value, HashType type, out
Byte[] salt) {
return Hash(value, type, out salt, new UTF8Encoding());
} // Hash
public static String Hash(this String value, HashType type, out
Byte[] salt, Encoding encoding) {
// Check salt
if (salt == null) {
// Define a random salt
Random random = new Random();
Int32 size = random.Next(4, 8);
salt = new Byte[size];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(salt);
a reasonable salt size and stick with it:
final static int SALT_SIZE = 16;
byte[] salt = new byte[SALT_SIZE];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(salt);
It is a minor error to use the GetNonZeroBytes() method in this
context as it reduces the randomness of the salt.
Again, give the user no choice. SHA256 only.
}
// Define salted
Byte[] valueBytes = encoding.GetBytes(value);
Byte[] valueSaltedBytes = new Byte[valueBytes.Length +
valueBytes.Length];
for (Int32 i = 0; i < valueBytes.Length; i++)
valueSaltedBytes[i] = valueBytes[i];
for (Int32 i = 0; i < salt.Length; i++)
valueSaltedBytes[valueBytes.Length + i] = salt[i];
// Hash value
HashAlgorithm algorithm;
switch (type) {
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");
}
Base64 increases both processing time and the storage space needed.
// Define hash
Byte[] hash = algorithm.ComputeHash(valueSaltedBytes);
Byte[] hashSalted = new Byte[hash.Length + salt.Length];
for (Int32 i = 0; i < hash.Length; i++)
hashSalted[i] = hash[i];
for (Int32 i = 0; i < salt.Length; i++)
hashSalted[hash.Length + i] = salt[i];
// Return hash
return Convert.ToBase64String(hashSalted);
Unless you need to transmit the data over a text-only link then stick
with byte arrays.
rossum
} // Hash
Thank You!
Miguel
.
- Follow-Ups:
- Re: Hash MD5, Sha1 and Length
- From: Arne Vajhøj
- Re: Hash MD5, Sha1 and Length
- From: shapper
- Re: Hash MD5, Sha1 and Length
- References:
- Hash MD5, Sha1 and Length
- From: shapper
- Re: Hash MD5, Sha1 and Length
- From: Tom Spink
- Re: Hash MD5, Sha1 and Length
- From: shapper
- Re: Hash MD5, Sha1 and Length
- From: rossum
- Re: Hash MD5, Sha1 and Length
- From: shapper
- Re: Hash MD5, Sha1 and Length
- From: shapper
- Hash MD5, Sha1 and Length
- Prev by Date: Re: Hash MD5, Sha1 and Length
- Next by Date: Re: Search through a (large) binary file.
- Previous by thread: Re: Hash MD5, Sha1 and Length
- Next by thread: Re: Hash MD5, Sha1 and Length
- Index(es):
Relevant Pages
|