internal SafeByte( IFastEncryptor encryptor, IFastRandom fastRandom, IByteIdGenerator byteIdGenerator, IByteArrayProtector memoryProtector) { if (encryptor == null) { throw new ArgumentNullException(nameof(encryptor)); } if (fastRandom == null) { throw new ArgumentNullException(nameof(fastRandom)); } if (byteIdGenerator == null) { throw new ArgumentNullException(nameof(fastRandom)); } if (memoryProtector == null) { throw new ArgumentNullException(nameof(memoryProtector)); } _encryptor = encryptor; _fastRandom = fastRandom; _byteIdGenerator = byteIdGenerator; _memoryProtector = memoryProtector; }
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; }
internal EncryptedSafeByteCollection(IFastEncryptor encryptor, IByteArrayProtector memoryProtector, IFastRandom fastRandom, ISafeByteFactory safeByteFactory) { if (encryptor == null) { throw new ArgumentNullException(nameof(encryptor)); } if (memoryProtector == null) { throw new ArgumentNullException(nameof(memoryProtector)); } if (safeByteFactory == null) { throw new ArgumentNullException(nameof(safeByteFactory)); } _encryptor = encryptor; _memoryProtector = memoryProtector; _safeByteFactory = safeByteFactory; _encryptionKey = fastRandom.GetBytes(_memoryProtector.BlockSizeInBytes); _memoryProtector.Protect(_encryptionKey); }
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); }
/// <summary> /// Private constructor for creating identical instance of the <see cref="SafeByte" />. /// </summary> private SafeByte( int id, int realBytePosition, int encryptedByteLength, byte[] encryptionKey, byte[] encryptedByte, IFastEncryptor encryptor, IFastRandom fastRandom, IByteIdGenerator byteIdGenerator, IByteArrayProtector memoryProtector) { _encryptor = encryptor; _fastRandom = fastRandom; _byteIdGenerator = byteIdGenerator; _memoryProtector = memoryProtector; //Deep copy _id = id; _realBytePosition = realBytePosition; _encryptedByte = new byte[encryptedByte.Length]; _encryptionKey = new byte[encryptionKey.Length]; Buffer.BlockCopy(encryptedByte, 0, _encryptedByte, 0, encryptedByte.Length); Buffer.BlockCopy(encryptionKey, 0, _encryptionKey, 0, encryptionKey.Length); _memoryProtector.Protect(_encryptionKey); _memoryProtector.Protect(_encryptedByte); _encryptedByteLength = encryptedByteLength; IsByteSet = true; }
private static IByteArrayProtector GetSut(IFastEncryptor encryptor = null, IFastRandom random = null) => new MemoryProtector( encryptor: encryptor ?? Stubs.Get <IFastEncryptor>(), random: random ?? Stubs.Get <IFastRandom>());
/// <exception cref="ArgumentNullException"><paramref name="encryptor" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception> internal MemoryProtector(IFastEncryptor encryptor, ICryptoRandom random) { _encryptor = encryptor ?? throw new ArgumentNullException(nameof(encryptor)); _key = random.GetBytes(Encryptor.MinKeySizeInBits); _encryptor.Padding = PaddingMode.None; }
internal MemoryProtector(IFastEncryptor encryptor, ICryptoRandom random) { _encryptor = encryptor; _key = random.GetBytes(Encryptor.MinKeySize); }