Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        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;
        }
Example #5
0
        internal static string ToKeyUriValue(this OtpType type)
        {
            Contract.Requires <ArgumentOutOfRangeException>(Enum.IsDefined(typeof(OtpType), type));
            Contract.Requires <ArgumentOutOfRangeException>(type != OtpType.Unknown);
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result <string>()));

            switch (type)
            {
            case OtpType.Totp:
                return("totp");

            case OtpType.Hotp:
                return("hotp");

            default:
                throw new NotSupportedException();
            }
        }
Example #6
0
File: Otp.cs Project: kappa7194/otp
        internal static string GetKeyUri(
            OtpType type,
            string issuer,
            string account,
            byte[] secret,
            HashAlgorithm algorithm,
            int digits,
            long counter,
            int period)
        {
            Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(OtpType), type));
            Contract.Requires<ArgumentOutOfRangeException>(type != OtpType.Unknown);
            Contract.Requires<ArgumentNullException>(issuer != null);
            Contract.Requires<ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(issuer));
            Contract.Requires<ArgumentNullException>(account != null);
            Contract.Requires<ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(account));
            Contract.Requires<ArgumentNullException>(secret != null);
            Contract.Requires<ArgumentException>(secret.Length > 0);
            Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
            Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
            Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
            Contract.Requires<ArgumentOutOfRangeException>(counter >= 0);
            Contract.Requires<ArgumentOutOfRangeException>(period > 0);
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));

            return
                string.Format(
                    CultureInfo.InvariantCulture,
                    "otpauth://{0}/{1}:{2}?secret={3}&issuer={4}&algorithm={5}&digits={6}&counter={7}&period={8}",
                    type.ToKeyUriValue(),
                    HttpUtility.UrlEncode(issuer),
                    HttpUtility.UrlEncode(account),
                    Base32.Encode(secret),
                    HttpUtility.UrlEncode(issuer),
                    algorithm.ToKeyUriValue(),
                    digits,
                    counter,
                    period);
        }
Example #7
0
        private void comboBoxType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            bool timerEnabled = timerUpdateTotp.Enabled;

            timerUpdateTotp.Stop();
            timerUpdateTotp.Dispose();
            OtpType type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex];

            try
            {
                this.data = readData(true);
            }
            catch
            {
                this.data = new OtpAuthData();
            }

            if (type == OtpType.Totp)
            {
                this.data.Proprietary = true;
                this.data.Type        = OtpType.Totp;
            }
            else if (type == OtpType.Hotp)
            {
                this.data.Proprietary = true;
                this.data.Digits      = 6;
                this.data.Type        = OtpType.Hotp;
            }
            else if (type == OtpType.Steam)
            {
                this.data.Proprietary = false;
                this.data.Digits      = 5;
                this.data.KeeOtp1Mode = true;
                this.data.Type        = OtpType.Steam;
            }

            loadData();
            timerUpdateTotp.Enabled = timerEnabled;
        }
Example #8
0
        internal static string GetKeyUri(
            OtpType type,
            string issuer,
            string account,
            byte[] secret,
            HashAlgorithm algorithm,
            int digits,
            long counter,
            int period)
        {
            Contract.Requires <ArgumentOutOfRangeException>(Enum.IsDefined(typeof(OtpType), type));
            Contract.Requires <ArgumentOutOfRangeException>(type != OtpType.Unknown);
            Contract.Requires <ArgumentNullException>(issuer != null);
            Contract.Requires <ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(issuer));
            Contract.Requires <ArgumentNullException>(account != null);
            Contract.Requires <ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(account));
            Contract.Requires <ArgumentNullException>(secret != null);
            Contract.Requires <ArgumentException>(secret.Length > 0);
            Contract.Requires <ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
            Contract.Requires <ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
            Contract.Requires <ArgumentOutOfRangeException>(digits > 0);
            Contract.Requires <ArgumentOutOfRangeException>(counter >= 0);
            Contract.Requires <ArgumentOutOfRangeException>(period > 0);
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result <string>()));

            return
                (string.Format(
                     CultureInfo.InvariantCulture,
                     "otpauth://{0}/{1}:{2}?secret={3}&issuer={4}&algorithm={5}&digits={6}&counter={7}&period={8}",
                     type.ToKeyUriValue(),
                     HttpUtility.UrlEncode(issuer),
                     HttpUtility.UrlEncode(account),
                     Base32.Encode(secret),
                     HttpUtility.UrlEncode(issuer),
                     algorithm.ToKeyUriValue(),
                     digits,
                     counter,
                     period));
        }
Example #9
0
 internal static string GetKeyUri(
     OtpType type,
     string issuer,
     string account,
     byte[] secret,
     HashAlgorithm algorithm,
     int digits,
     long counter,
     int period)
 {
     return
         string.Format(
             CultureInfo.InvariantCulture,
             "otpauth://{0}/{1}:{2}?secret={3}&issuer={4}&algorithm={5}&digits={6}&counter={7}&period={8}",
             type.ToKeyUriValue(),
             WebUtility.UrlEncode(issuer),
             WebUtility.UrlEncode(account),
             Base32.Encode(secret),
             WebUtility.UrlEncode(issuer),
             algorithm.ToKeyUriValue(),
             digits,
             counter,
             period);
 }
Example #10
0
 public OtpAuthenticator(OtpType type, OtpAlgorithm algorithm = OtpAlgorithm.SHA1, byte[] key = null)
 {
     HMAC = GetHMAC(algorithm, key ?? GenerateKey(algorithm));
     Type = type;
 }
Example #11
0
 public void CancelTokens(OtpType type, IEnumerable <string> tokens)
 {
     Used.AddRange(tokens);
 }
Example #12
0
 public void CancelToken(OtpType type, string token)
 {
     Used.Add(token);
 }