Created
December 20, 2018 08:39
-
-
Save Regenhardt/2ca0bd855b3ffcadb47165007cc3e8eb to your computer and use it in GitHub Desktop.
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
public static class SecureStringExtensions | |
{ | |
public static string GetSHA512(this SecureString input) | |
{ | |
using (var sha512 = SHA512.Create()) | |
{ | |
byte[] password = Encoding.UTF8.GetBytes(ToNormalString(input)); | |
byte[] hash = sha512.ComputeHash(password); | |
return Encoding.UTF8.GetString(hash); | |
} | |
} | |
private static string ToNormalString(SecureString value) | |
{ | |
IntPtr valuePtr = IntPtr.Zero; | |
try | |
{ | |
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value); | |
return Marshal.PtrToStringUni(valuePtr); | |
} | |
finally | |
{ | |
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment