private static string Encode(byte[] hash, HashStringEncoding encoding) { if (encoding == HashStringEncoding.Base64) { return(Convert.ToBase64String(hash)); } else if (encoding == HashStringEncoding.Base64Url) { return(Base64Url.Encode(hash)); } throw new ArgumentException("Invalid encoding"); }
private static string ToSha256(this string input, HashStringEncoding encoding = HashStringEncoding.Base64) { if (string.IsNullOrWhiteSpace(input)) { return(string.Empty); } using (var sha256 = SHA256.Create()) { var bytes = Encoding.ASCII.GetBytes(input); return(Encode(sha256.ComputeHash(bytes), encoding)); } }
private static string Encode(byte[] hash, HashStringEncoding encoding) { switch (encoding) { case HashStringEncoding.Base64: return(Convert.ToBase64String(hash)); case HashStringEncoding.Base64Url: return(Base64Url.Encode(hash)); default: throw new ArgumentException("Invalid encoding"); } }
public static string ToSha512(this string input, HashStringEncoding encoding = HashStringEncoding.Base64) { if (string.IsNullOrWhiteSpace(input)) { return(string.Empty); } using (var sha = SHA512.Create()) { var bytes = Encoding.ASCII.GetBytes(input); var hash = sha.ComputeHash(bytes); return(Encode(hash, encoding)); } }