1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static string Crypter(string p_input)
{
// Source : http://msdn2.microsoft.com/fr-fr/library/system.security.cryptography.md5(VS.80).aspx
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(p_input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// Verify a hash against a string.
public static bool verifyMd5Hash(string p_input, string p_hash)
{
// Source : http://msdn2.microsoft.com/fr-fr/library/system.security.cryptography.md5(VS.80).aspx
// Hash the input.
string hashOfInput = Crypter(p_input);
// Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, p_hash))
{
return true;
}
else
{
return false;
}
} |
Partager