using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; namespace Centron.Api.docuFORM.Helper { public static class OAuthHelper { // Can be used to generate a "state" or the codeVerifier for the "code challenge" public static string GenerateRandomBase64String(int byteLength = 32) { byte[] randomBytes = new byte[byteLength]; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(randomBytes); } // Base64 URL-safe encoding string state = Convert.ToBase64String(randomBytes) .Replace("+", "-") .Replace("/", "_") .Replace("=", ""); return state; } public static string GenerateCodeChallenge(string codeVerifier) { using (var sha256 = SHA256.Create()) { byte[] hash = sha256.ComputeHash(Encoding.ASCII.GetBytes(codeVerifier)); string codeChallenge = Convert.ToBase64String(hash) .Replace("+", "-") .Replace("/", "_") .Replace("=", ""); return codeChallenge; } } } }