Skip to content

Instantly share code, notes, and snippets.

@grandsilence
Last active July 24, 2024 11:50
Show Gist options
  • Save grandsilence/e74d0fca294d0cbeda6baf581c32a306 to your computer and use it in GitHub Desktop.
Save grandsilence/e74d0fca294d0cbeda6baf581c32a306 to your computer and use it in GitHub Desktop.
C# SHA 256 to String
public static string Sha256(string text)
{
var sb = new StringBuilder();
using (var hash = SHA256.Create())
{
var result = hash.ComputeHash(Encoding.UTF8.GetBytes(text));
// ReSharper disable once ForCanBeConvertedToForeach
for (int i = 0; i < result.Length; i++)
sb.Append(result[i].ToString("x2"));
}
return sb.ToString();
}
@stephanhuewe
Copy link

Works great. One small glitch:
sbHash.Append => sb.Append

@grandsilence
Copy link
Author

@stephanhuewe Thank you. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment