Created
October 26, 2018 10:24
-
-
Save huihut/7d85e0f43c49394b1b64a117b6a33cc8 to your computer and use it in GitHub Desktop.
C# MD5 and SHA1 encrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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