public static void Main(string[] args) { TimeSpan validityDuration; int passwordDigits; string secret; if (args.Length < 3) { Console.WriteLine("usage: shared_secret password_validity_duration_seconds password_length"); return; } else { secret = args[0]; // Note: not validated. validityDuration = TimeSpan.FromSeconds(int.Parse(args[1])); passwordDigits = int.Parse(args[2]); } var key = Encoding.UTF8.GetBytes(secret); bool isExiting = false; Console.CancelKeyPress += (s, e) => isExiting = true; Console.Clear(); while (!isExiting) { Console.Write("Password to test: "); var codeToTest = Console.ReadLine().Trim(); var code = Topt.GetCode(key, DateTime.UtcNow, validityDuration, passwordDigits); Console.WriteLine((code == codeToTest) ? "✅ Valid " : "⛔ ️ Incorrect Password"); Console.WriteLine(code); } }
public void MoreThanThirtySecondDifferent() { var time = DateTime.Today; var code1 = Topt.GetCode(sharedKey, time, validity, digits); var code2 = Topt.GetCode(sharedKey, time + validity, validity, digits); Assert.That(code1, Is.Not.EqualTo(code2)); }
public void WithinThirtySecondSameCode() { var time = DateTime.Today; var code1 = Topt.GetCode(sharedKey, time, validity, digits); var code2 = Topt.GetCode(sharedKey, time + validity - TimeSpan.FromMilliseconds(1), validity, digits); Assert.That(code1, Is.EqualTo(code2)); }
public void SameTimeGeneratesSameCode() { var now = DateTime.UtcNow; var code1 = Topt.GetCode(sharedKey, now, validity, digits); var code2 = Topt.GetCode(sharedKey, now, validity, digits); Assert.That(code1, Is.EqualTo(code2)); }
public void ShorterValidityDifferent() { var shorterValidity = TimeSpan.FromSeconds(5); var time = DateTime.Today; var code1 = Topt.GetCode(sharedKey, time, validity, digits); var code2 = Topt.GetCode(sharedKey, time + shorterValidity, shorterValidity, digits); Assert.That(code1, Is.Not.EqualTo(code2)); }
public void DigitsControlsLength() { var time = DateTime.Today; var code1 = Topt.GetCode(sharedKey, time, validity, digits); var code2 = Topt.GetCode(sharedKey, time, validity, digits + 1); var code3 = Topt.GetCode(sharedKey, time, validity, digits + 2); Assert.That(code1.Length, Is.EqualTo(digits)); Assert.That(code2.Length, Is.EqualTo(digits + 1)); Assert.That(code3.Length, Is.EqualTo(digits + 2)); }
public void SameTimeDifferentKey() { var time = DateTime.Today; var code1 = Topt.GetCode(sharedKey, time, validity, digits); var key = new byte[64]; rng.GetBytes(key); var code2 = Topt.GetCode(key, time, validity, digits); Assert.That(code1, Is.Not.EqualTo(code2)); }