using System; using System.Security.Cryptography; using System.Text; public static string GenerateHash(string input) { using (var sha256 = SHA256.Create()) { var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); var hash = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); return hash; } }
using System; using System.Security.Cryptography; using System.Text; public static bool VerifyHash(string input, string hash) { using (var sha256 = SHA256.Create()) { var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); var computedHash = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); return computedHash == hash; } }In this example, we are verifying whether a given hash matches the hash generated from a specified input string. We compute the hash for the input string using the same algorithm (SHA256), then compare it to the given hash. If the two hashes match, we return true; otherwise, we return false. Package/library: This code uses the System.Security.Cryptography namespace, which is part of the .NET Framework.