/// <summary> /// Gets a URL that conforms to the de-facto standard /// created and used by Google /// </summary> private static string GetBaseKeyUrl(byte[] key, string user, OtpType otpType, int size) { if (string.IsNullOrEmpty(user)) { throw new ArgumentNullException(nameof(user)); } if (key == null || key.Length == 0) { throw new ArgumentNullException(nameof(key)); } if (size != 6 && size != 8) { throw new ArgumentException("size must be 6 or 8"); } var url = $"otpauth://{otpType.ToString().ToLowerInvariant()}/{HttpUtility.UrlEncode(user)}?{UrlConstants.SecretParameter}={Base32Encoder.Encode(key)}"; if (size == 8) { url += CreateParameter(UrlConstants.DigitsParameter, size); } return(url); }
/// <summary> /// Gets a URL that conforms to the de-facto standard /// created and used by Google /// </summary> private static string GetBaseKeyUrl(byte[] key, string user, OtpType otpType, int size) { if (string.IsNullOrEmpty(user)) { throw new ArgumentNullException("user"); } if (key == null || key.Length == 0) { throw new ArgumentNullException("key"); } if (size != 6 && size != 8) { throw new ArgumentException("size must be 6 or 8"); } var url = string.Format("otpauth://{0}/{1}?{2}={3}", otpType.ToString().ToLowerInvariant(), WebUtility.UrlEncode(user), UrlConstants.SecretParameter, Base32Encoding.Standard.GetString(key)); if (size == 8) { url += CreateParameter(UrlConstants.DigitsParameter, size); } return(url); }
public static string GetUri(OtpType type, byte[] key, string accountName, string issuer = "", OtpAlgorithm algorithm = OtpAlgorithm.SHA1, int codeLength = 6, long counter = 0, int period = 30) { StringBuilder SB = new StringBuilder(); SB.AppendFormat("otpauth://{0}/", type.ToString().ToLower()); if (!string.IsNullOrEmpty(issuer)) { SB.AppendFormat("{0}:{1}?issuer={0}&", Uri.EscapeUriString(issuer), Uri.EscapeUriString(accountName)); } else { SB.AppendFormat("{0}?", Uri.EscapeUriString(accountName)); } SB.AppendFormat("secret={0}&algorithm={1}&digits={2}&", Base32.Encode(key), algorithm, codeLength); if (type == OtpType.HOTP) { SB.AppendFormat("counter={0}", counter); } else { SB.AppendFormat("period={0}", period); } return(SB.ToString()); }
/// <summary> /// Gets a URL that conforms to the de-facto standard /// created and used by Google /// </summary> private static string GetBaseKeyUrl(byte[] key, string user, OtpType otpType, int size) { if (string.IsNullOrEmpty(user)) throw new ArgumentNullException("user"); if (key == null || key.Length == 0) throw new ArgumentNullException("key"); if (size != 6 && size != 8) throw new ArgumentException("size must be 6 or 8"); var url = string.Format("otpauth://{0}/{1}?{2}={3}", otpType.ToString().ToLowerInvariant(), System.Web.HttpUtility.UrlEncode(user), UrlConstants.SecretParameter, Base32Encoder.Encode(key)); if (size == 8) url += CreateParameter(UrlConstants.DigitsParameter, size); return url; }