public async Task IncrementHotp(int position)
        {
            if (Authenticators.ElementAtOrDefault(position) == null)
            {
                return;
            }

            var info = Authenticators[position];
            var auth = GetAuthenticator(info);

            if (auth.Type != OtpType.Hotp)
            {
                return;
            }

            var secret = Base32.Decode(auth.Secret);
            var hotp   = new Hotp(secret, auth.Algorithm);

            auth.Counter++;
            auth.Code      = hotp.ComputeHotp(auth.Counter);
            auth.TimeRenew = DateTime.Now.AddSeconds(10);

            Authenticators[position] = auth;
            await _connection.UpdateAsync(auth);
        }
示例#2
0
        public void HotpPaddingTest()
        {
            var hotpCalculator = new Hotp(RfcTestKey);
            var hotp           = hotpCalculator.ComputeHotp(25193);

            Assert.AreEqual("000039", hotp);
        }
示例#3
0
        public void ComputeHOTPRfc4226Test(OtpHashMode hash, long counter, string expectedOtp)
        {
            Hotp   otpCalc = new Hotp(rfc4226Secret, hash, expectedOtp.Length);
            string otp     = otpCalc.ComputeHOTP(counter);

            Assert.That(otp, Is.EqualTo(expectedOtp));
        }
示例#4
0
        public void HotpAppendixDTests(Rfc4226AppendixDData data)
        {
            Assert.IsNotNull(data, "data was null");
            Hotp hotpCalculator = new Hotp(RfcTestKey);
            var  hotp           = hotpCalculator.ComputeHotp(data.Counter);

            Assert.AreEqual(data.Hotp, hotp);
        }
示例#5
0
        public void OtpAppendixDTests(Rfc4226AppendixDData data)
        {
            Assert.IsNotNull(data, "data was null");

            Hotp hotpCalculator = new Hotp(RfcTestKey);
            var  otp            = hotpCalculator.ComputeHotpDecimal(data.Counter, OtpHashMode.Sha1);

            Assert.AreEqual(data.Decimal, otp);
        }
示例#6
0
 public AuthRepository(UserManager <User> userManager, UserDataContext userDataContext, IConfiguration configuration, IJwtFactory jwtFactory, ISmsService sms)
 {
     UserManager     = userManager;
     UserDataContext = userDataContext;
     JwtFactory      = jwtFactory;
     Configuration   = configuration;
     Generator       = new Hotp(Encoding.ASCII.GetBytes(Configuration.GetSection("Authentication:Key").Value));
     SMS             = sms;
 }
示例#7
0
        public void Hotp_Sha1_Called()
        {
            var          mock = this.keyMock;
            IKeyProvider key  = mock.Object;

            var hotp = new Hotp(key, mode: OtpHashMode.Sha1);

            hotp.ComputeHotp(hotpCounter);

            mock.Verify(k => k.ComputeHmac(OtpHashMode.Sha1, hotpData));
        }
示例#8
0
        public void GenerateHotpWithCounter3()
        {
            byte[] secret       = System.Text.Encoding.ASCII.GetBytes("12345678901234567890");
            int    passwordSize = 6;
            int    counter      = 3;
            string expectedHotp = "969429";

            Hotp   otp           = new Hotp(secret, passwordSize);
            string generatedHotp = otp.GeneratePassword(counter);

            Assert.AreEqual(expectedHotp, generatedHotp);
        }
示例#9
0
        public void ContructorWithKeyProviderTest()
        {
            //Mock a key provider which always returns an all-zero HMAC (causing an all-zero OTP)
            Mock <IKeyProvider> keyMock = new Mock <IKeyProvider>();

            keyMock.Setup(key => key.ComputeHmac(It.Is <OtpHashMode>(m => m == OtpHashMode.Sha1), It.IsAny <byte[]>())).Returns(new byte[20]);

            var otp = new Hotp(keyMock.Object, OtpHashMode.Sha1, 6);

            Assert.That(otp.ComputeHOTP(0), Is.EqualTo("000000"));
            Assert.That(otp.ComputeHOTP(1), Is.EqualTo("000000"));
        }
 public UserController(ISmsSender smsSender,
                       AppDbContext appDbContext,
                       IOptions <TokenSettings> tokenSettings,
                       IOptions <SMSSettings> smsSettings,
                       JwtSettings jwtSettings)
 {
     _smsSender     = smsSender;
     _dbContext     = appDbContext;
     _tokenSettings = tokenSettings.Value;
     _jwtSettings   = jwtSettings;
     _SMSSettings   = smsSettings.Value;
     hotp           = new Hotp(Encoding.ASCII.GetBytes(_SMSSettings.OTPSecretKey), mode: OtpHashMode.Sha256, hotpSize: 6);
 }
示例#11
0
        public async Task <IActionResult> AddDevice()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            using (var context = _context){
                user = context.Users.Include(ApplicationUser => ApplicationUser.Devices)
                       .Single(u => u.Id == user.Id);
            }

            var model = new AddDeviceViewModel();

            //TODO: Redo with TOTP? or better implement HOTP in AddDeviceViewModel
            var hotp = new Hotp(Encoding.ASCII.GetBytes(user.Email), mode: OtpHashMode.Sha512);

            model.newToken = hotp.GetHashCode().ToString();
            return(View(model));
        }
示例#12
0
        public string GetCode()
        {
            if (Type == AuthenticatorType.Totp && TimeRenew <= DateTime.Now)
            {
                var secret = Base32Encoding.ToBytes(Secret);
                var totp   = new Totp(secret, Period, Algorithm, Digits);

                _code     = totp.ComputeTotp();
                TimeRenew = DateTime.Now.AddSeconds(totp.RemainingSeconds());
            }
            else if (Type == AuthenticatorType.Hotp && _lastCounter != Counter)
            {
                var secret = Base32Encoding.ToBytes(Secret);
                var hotp   = new Hotp(secret, Algorithm);

                _code     = hotp.ComputeHOTP(Counter);
                TimeRenew = DateTime.Now.AddSeconds(10);

                _lastCounter = Counter;
            }

            return(_code);
        }
示例#13
0
 public void Vector8()
 {
     Assert.Equal(162583, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 7));
 }
示例#14
0
        public static string GenerateHotpCode(long counter)
        {
            var otp = new Hotp(rfcSecret);

            return(otp.ComputeHOTP(counter));
        }
示例#15
0
 public HotpTest()
 {
     _computeTestHotp = new Hotp("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", HashAlgorithm.Sha1, 6);
 }
示例#16
0
文件: OtpBase.cs 项目: tiuub/KeeOtp2
 public OtpHotp(OtpAuthData data) : base(data)
 {
     this.hotp = new Hotp(data.Key.ReadData(), data.Algorithm, data.Digits);
 }
示例#17
0
        public static bool VerifyHotpCode(string hotpCode, long counter)
        {
            var otp = new Hotp(rfcSecret);

            return(otp.VerifyHotp(hotpCode, counter));
        }
示例#18
0
 public void Vector1()
 {
     Assert.Equal(755224, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 0));
 }
示例#19
0
 public void Vector2()
 {
     Assert.Equal(287082, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 1));
 }
示例#20
0
 public void Vector3()
 {
     Assert.Equal(359152, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 2));
 }
示例#21
0
 public void Vector4()
 {
     Assert.Equal(969429, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 3));
 }
示例#22
0
 public void Vector10()
 {
     Assert.Equal(520489, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 9));
 }
示例#23
0
 public void Vector9()
 {
     Assert.Equal(399871, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 8));
 }
示例#24
0
 public void Vector5()
 {
     Assert.Equal(338314, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 4));
 }
示例#25
0
 public void Vector7()
 {
     Assert.Equal(287922, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 6));
 }
示例#26
0
 public void Vector6()
 {
     Assert.Equal(254676, Hotp.GetCode(HashAlgorithm.Sha1, this.secret, 5));
 }