Last active
May 30, 2022 02:45
-
-
Save kenzauros/09377008ff036a730d0c7de7e6ecdb89 to your computer and use it in GitHub Desktop.
C# で SHA256 のハッシュ文字列を得る
This file contains 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
// .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}")); | |
} |
This file contains 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
// .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