Skip to content

Instantly share code, notes, and snippets.

@Regenhardt
Created December 20, 2018 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Regenhardt/2ca0bd855b3ffcadb47165007cc3e8eb to your computer and use it in GitHub Desktop.
Save Regenhardt/2ca0bd855b3ffcadb47165007cc3e8eb to your computer and use it in GitHub Desktop.
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