using System.Security.Cryptography;
public static void Main()
string userProvidedSecret = "";
string raw = string.IsNullOrEmpty(userProvidedSecret) ? GenerateSecret() : userProvidedSecret;
Console.WriteLine($"Secret Value {raw}, hashed {HashString(raw)}");
public static string HashString(string s) {
using(var sha = SHA256.Create()) {
byte[] hashed = sha.ComputeHash(Encoding.UTF8.GetBytes(s));
return Convert.ToBase64String(hashed);
public static string GenerateSecret() {
using(RandomNumberGenerator rng = new RNGCryptoServiceProvider()) {
byte[] tokenData = new byte[32];
return Convert.ToBase64String(tokenData);