Albireo.Otp.Otp.GetKeyUri C# (CSharp) Method

GetKeyUri() static private method

static private GetKeyUri ( OtpType type, string issuer, string account, byte secret, HashAlgorithm algorithm, int digits, long counter, int period ) : string
type OtpType
issuer string
account string
secret byte
algorithm System.Security.Cryptography.HashAlgorithm
digits int
counter long
period int
return string
        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);
        }

Usage Example

Esempio n. 1
0
        /// <summary>Build a URI for secret key provisioning.</summary>
        /// <param name="algorithm">The hashing algorithm for the HMAC computation.</param>
        /// <param name="issuer">The name of the entity issuing and maintaining the key.</param>
        /// <param name="account">The account name for which the one-time codes will work.</param>
        /// <param name="secret">The ASCII-encoded base32-encoded shared secret.</param>
        /// <param name="counter">The initial value for the counter.</param>
        /// <param name="digits">The number of digits of the one-time codes.</param>
        /// <returns>The provisioning URI.</returns>
        public static string GetKeyUri(
            HashAlgorithm algorithm,
            string issuer,
            string account,
            byte[] secret,
            long counter,
            int digits = Otp.DefaultDigits)
        {
            Contract.Requires <ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
            Contract.Requires <ArgumentOutOfRangeException>(algorithm != HashAlgorithm.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>(digits > 0);
            Contract.Requires <ArgumentOutOfRangeException>(counter >= 0);
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result <string>()));

            return
                (Otp.GetKeyUri(
                     OtpType.Hotp,
                     issuer,
                     account,
                     secret,
                     algorithm,
                     digits,
                     counter,
                     0));
        }