Skip to content

Instantly share code, notes, and snippets.

@kenzauros
Last active May 30, 2022 02:45
Show Gist options
  • Save kenzauros/09377008ff036a730d0c7de7e6ecdb89 to your computer and use it in GitHub Desktop.
Save kenzauros/09377008ff036a730d0c7de7e6ecdb89 to your computer and use it in GitHub Desktop.
C# で SHA256 のハッシュ文字列を得る
// .NET 5 まで
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public static class StringExtensions
{
static readonly SHA256CryptoServiceProvider hashProvider = new SHA256CryptoServiceProvider();
public static string GetSHA256HashedString(this string value)
=> string.Join("", hashProvider.ComputeHash(Encoding.UTF8.GetBytes(value)).Select(x => $"{x:x2}"));
}
// .NET 6 以降
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public static class StringExtensions
{
static readonly HashAlgorithm hashAlgorithm = SHA256.Create();
public static string GetSHA256HashedString(this string value)
=> string.Join("", hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(value)).Select(x => $"{x:x2}"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment