Skip to content

Instantly share code, notes, and snippets.

@huihut
Created October 26, 2018 10:24
Show Gist options
  • Save huihut/7d85e0f43c49394b1b64a117b6a33cc8 to your computer and use it in GitHub Desktop.
Save huihut/7d85e0f43c49394b1b64a117b6a33cc8 to your computer and use it in GitHub Desktop.
C# MD5 and SHA1 encrypt
// MD5
public static string Md5(string str)
{
var algorithm = HashAlgorithmNames.Md5;
HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(algorithm);
var hash = provider.CreateHash();
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf16LE);
hash.Append(buffer);
var hashedBuffer = hash.GetValueAndReset();
return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16LE, hashedBuffer);
}
// SHA1
public static string Sha1(string str)
{
var algorithm = HashAlgorithmNames.Sha1;
HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(algorithm);
var hash = provider.CreateHash();
IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
hash.Append(buffer);
var hashedBuffer = hash.GetValueAndReset();
return CryptographicBuffer.EncodeToHexString(hashedBuffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment