/* * This file is part of Socialvoid.NET Project (https://github.com/Intellivoid/Socialvoid.NET). * Copyright (c) 2021 Socialvoid.NET Authors. * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this source code of library. * If not, see . */ /* * Credits to Devin Martin and the original OtpSharp library: * https://github.com/kspearrin/Otp.NET */ using System.Collections.Generic; namespace Socialvoid.Security.Otp { /// /// A verification window. /// since: v0.0.0 /// public sealed class VerificationWindow { //------------------------------------------------- #region static field's Region /// /// The verification window that accomodates network delay that is /// recommended in the RFC. /// since: v0.0.0 /// public static readonly VerificationWindow RfcSpecifiedNetworkDelay = new(1, 1); #endregion //------------------------------------------------- #region field's Region /// /// the previous value in the window. /// since: v0.0.0 /// private readonly int _previous; /// /// the future value in the window. /// since: v0.0.0 /// private readonly int _future; #endregion //------------------------------------------------- #region Constructor's Region /// /// Creates an instance of a verification window. /// since: v0.0.0 /// /// The number of previous frames to accept /// The number of future frames to accept public VerificationWindow(int previous = 0, int future = 0) { _previous = previous; _future = future; } #endregion //------------------------------------------------- #region Get Method's Region /// /// Gets an enumberable of all the possible validation candidates. /// since: v0.0.0 /// /// /// The initial frame to validate. /// /// /// Enumberable of all possible frames that need to be validated. /// public IEnumerable ValidationCandidates(long initialFrame) { yield return initialFrame; for(int i = 1; i <= _previous; i++) { var val = initialFrame - i; if(val < 0) break; yield return val; } for(int i = 1; i <= _future; i++) yield return initialFrame + i; } #endregion //------------------------------------------------- } }