/// <summary> /// Generate a Totp using provided binary data. /// </summary> /// <param name="key">Binary data.</param> /// <returns>Time-based One Time Password.</returns> public string Generate(byte[] key) { System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1(key, true); //Instanciates a new hash provider with a key. byte[] hash = hmac.ComputeHash(GetBytes((ulong)Counter)); //Generates hash from key using counter. hmac.Clear(); //Clear hash instance securing the key. int offset = hash[hash.Length - 1] & 0xf; //Math. int binary = //Math. ((hash[offset] & 0x7f) << 24) //Math. | ((hash[offset + 1] & 0xff) << 16) //Math. | ((hash[offset + 2] & 0xff) << 8) //Math. | (hash[offset + 3] & 0xff); //Math. int password = binary % (int)Math.Pow(10, _Length); //Math. return(password.ToString(new string('0', _Length))); //Math. }
/// <summary> /// Generate a Totp using provided binary data. /// </summary> /// <param name="key">Binary data.</param> /// <returns>Time-based One Time Password.</returns> public string Generate(byte[] key) { System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1(key, true); //Instanciates a new hash provider with a key. byte[] hash = hmac.ComputeHash(GetBytes((ulong)Counter)); //Generates hash from key using counter. hmac.Clear(); //Clear hash instance securing the key. int offset = hash[hash.Length - 1] & 0xf; //Math. int binary = //Math. ((hash[offset] & 0x7f) << 24) //Math. | ((hash[offset + 1] & 0xff) << 16) //Math. | ((hash[offset + 2] & 0xff) << 8) //Math. | (hash[offset + 3] & 0xff); //Math. int password = binary % (int)Math.Pow(10, _Length); //Math. return password.ToString(new string('0', _Length)); //Math. }