/// <summary> /// Recursive method that generates and returns a validated session salt. /// </summary> /// <seealso cref="IsSaltValid" /> private byte[] GenerateSessionSalt() { var salt = _safeRandom.GetBytes(SaltLength); if (!IsSaltValid(salt)) { return(GenerateSessionSalt()); //try until it's valid } _memoryProtector.Protect(salt); _isSaltEncrypted = true; return(salt); }
private async Task <IMemoryProtectedBytes> InitializeSaltAsync(IMemoryProtectedBytes sessionSalt) { const int maxRetries = 1000; var retryCount = 0; while (retryCount < maxRetries) { var salt = _safeRandom.GetBytes(SaltLength); if (!IsSaltValid(salt)) { retryCount++; continue; } await sessionSalt.InitializeAsync(salt) .ConfigureAwait(false); return(sessionSalt); } throw new ArgumentException("Could not initialize the session salt. Max retries exceeded."); }