private int GenerateInternal(byte b, byte[] salt, ref bool isSaltEncrypted, bool doNotEncrypt = false) { var byteBuffer = new byte[salt.Length + 1]; try { //Decrypt salt if (isSaltEncrypted) { _memoryProtector.Unprotect(salt); isSaltEncrypted = false; } //Append salt + byte Array.Copy(salt, byteBuffer, salt.Length); byteBuffer[salt.Length] = b; //Hash it var result = _fastHasher.ComputeFast(byteBuffer); return(result); } finally { Array.Clear(byteBuffer, 0, byteBuffer.Length); //Encrypt the salt if (!isSaltEncrypted && !doNotEncrypt) { _memoryProtector.Protect(salt); isSaltEncrypted = true; } } }
/// <summary> /// Appends the specified <see cref="ISafeByte" /> instance to the inner encrypted collection. /// </summary> /// <param name="safeByte">The safe byte.</param> /// <exception cref="System.ArgumentNullException"><paramref name="safeByte" /> is <see langword="null" />.</exception> /// <exception cref="ObjectDisposedException">Throws if the <see cref="EncryptedSafeByteCollection" /> instance is disposed</exception> /// <seealso cref="ISafeByte" /> public void Append(ISafeByte safeByte) { EnsureNotDisposed(); if (safeByte == null) { throw new ArgumentNullException(nameof(safeByte)); } _memoryProtector.Unprotect(_encryptionKey); var list = DecryptAndDeserialize(_encryptedCollection, _encryptionKey); list.Add(safeByte.Id); _encryptedCollection = SerializeAndEncrypt(list, _encryptionKey); list.Clear(); _memoryProtector.Protect(_encryptionKey); Length++; }
/// <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; }
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); }
/// <exception cref="System.InvalidOperationException">Thrown when byte is already set</exception> public void Set(byte b) { EnsureByteIsNotSet(); RuntimeHelper.ExecuteCodeWithGuaranteedCleanup( () => { //Generate ID _id = _byteIdGenerator.Generate(b); //Encrypt Encrypt(b); IsByteSet = true; }, () => { _memoryProtector.Protect(_encryptionKey); _memoryProtector.Protect(_encryptedByte); }); }