private HashedByteIdGenerator GetSut(IFastHasher hasher = null, ISafeRandom random = null, IMemoryProtectedBytes sessionSalt = null) { if (hasher == null) { var mockHasher = new Mock <IFastHasher>(); mockHasher.Setup(m => m.ComputeFast(It.IsAny <byte[]>())) #if !(NETCOREAPP3_1 || NETCOREAPP3_0) .Returns((byte[] b) => b[b.Length - 1]);
internal HashedByteIdGenerator(IFastHasher fastHasher, ISafeRandom safeRandom, IMemoryProtectedBytes sessionSalt) { if (sessionSalt == null) { throw new ArgumentNullException(nameof(sessionSalt)); } _fastHasher = fastHasher ?? throw new ArgumentNullException(nameof(fastHasher)); _safeRandom = safeRandom ?? throw new ArgumentNullException(nameof(safeRandom)); _sessionSalt = new AsyncLazy <IMemoryProtectedBytes>(() => InitializeSaltAsync(sessionSalt)); }
private static ISafeByte GetSut(IMemoryProtectedBytes encryptedByte = null, IMemoryProtectedBytes encryptionKey = null) { return(new SafeByte ( Stubs.Get <IFastEncryptor>(), Stubs.Get <IFastRandom>(), Stubs.Get <IByteIdGenerator>(), encryptedByte ?? Stubs.Get <IMemoryProtectedBytes>(), encryptionKey ?? Stubs.Get <IMemoryProtectedBytes>() )); }
internal SafeByte( IFastEncryptor encryptor, IFastRandom fastRandom, IByteIdGenerator byteIdGenerator, IMemoryProtectedBytes encryptedByte, IMemoryProtectedBytes encryptionKey) { _encryptor = encryptor ?? throw new ArgumentNullException(nameof(encryptor)); _fastRandom = fastRandom ?? throw new ArgumentNullException(nameof(fastRandom)); _byteIdGenerator = byteIdGenerator ?? throw new ArgumentNullException(nameof(fastRandom)); _encryptedByte = encryptedByte ?? throw new ArgumentNullException(nameof(encryptedByte)); _encryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey)); }
/// <summary> /// Private constructor for creating identical instance of the <see cref="SafeByte" />. /// </summary> private SafeByte( int id, int realBytePosition, int encryptedByteLength, IFastEncryptor encryptor, IFastRandom fastRandom, IByteIdGenerator byteIdGenerator, IMemoryProtectedBytes encryptedByte, IMemoryProtectedBytes encryptionKey) { _encryptor = encryptor; _fastRandom = fastRandom; _byteIdGenerator = byteIdGenerator; //Deep copy _id = id; _realBytePosition = realBytePosition; _encryptedByte = encryptedByte.DeepClone(); _encryptionKey = encryptionKey.DeepClone(); _encryptedByteLength = encryptedByteLength; IsByteSet = true; }
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."); }
internal EncryptedSafeByteCollection( IFastEncryptor encryptor, IMemoryProtectedBytes encryptionKey, ICryptoRandom fastRandom, ISafeByteFactory safeByteFactory, IByteIdListSerializer <int> serializer) { if (fastRandom == null) { throw new ArgumentNullException(nameof(fastRandom)); } _encryptionKey = new AsyncLazy <IMemoryProtectedBytes>(async() => { await encryptionKey.InitializeAsync(fastRandom.GetBytes(encryptionKey.BlockSizeInBytes)) .ConfigureAwait(false); return(encryptionKey); }); _encryptor = encryptor ?? throw new ArgumentNullException(nameof(encryptor)); _safeByteFactory = safeByteFactory ?? throw new ArgumentNullException(nameof(safeByteFactory)); _serializer = serializer; _salt = fastRandom.GetBytes(encryptor.BlockSizeInBits / 8); }