public void ChangeKey(byte[] key) { lock (transactionLockObject) { if (isDisposed) { throw new ObjectDisposedException("Can't use ChangeKey() after Dispose() or Delete()"); } if (isFieldIndexesInUse) { throw new InvalidOperationException("Can't use ChangeKey() before disposing the enumerable from a FindDocumentIds call"); } if (DebugLogHandler != null) { DebugLogHandler("ChangeKey: key=" + ((key != null) ? Convert.ToBase64String(key) : "null")); } EnsureValidKey(key); byte[] initializationVector = CryptoRandomNumberGenerator.GenerateBytes(16u); Aes256Encryptor aes256Encryptor = new Aes256Encryptor(key, initializationVector); journalWriter.Start(); try { foreach (KeyValuePair <uint, byte[]> document2 in packedFile.Documents) { if (document2.Key != metadataDocumentId) { byte[] value = document2.Value; TDocument val = DecryptAndDeserialize(value, document2.Key); byte[] document = SerializeAndEncrypt(val, aes256Encryptor); packedFile.Update(val.Id, document); } } WriteMetadataDocument(initializationVector); encryptor = aes256Encryptor; fieldIndexes.ChangeEncryptor(encryptor); journalWriter.Finish(); journalPlayer.Play(); } catch (Exception) { journalWriter.Discard(); throw; } } }
public DocumentCollection(PackedFile packedFile, IndexFactory indexFactory, byte[] key, JournalPlayer journalPlayer, JournalWriter journalWriter) { transactionLockObject = new object(); EnsureValidKey(key); documentType = typeof(TDocument); try { journalPlayer.Play(); SerializerReflectionCache.AddTypes(typeof(MetadataDocument), documentType); this.packedFile = packedFile; this.journalPlayer = journalPlayer; this.journalWriter = journalWriter; byte[] initializationVector; if (packedFile.IsEmpty) { initializationVector = CryptoRandomNumberGenerator.GenerateBytes(16u); journalWriter.Start(); WriteMetadataDocument(initializationVector); journalWriter.Finish(); journalPlayer.Play(); } else { KeyValuePair <uint, byte[]> keyValuePair = packedFile.Documents.First(); metadataDocumentId = keyValuePair.Key; MetadataDocument metadataDocument = BinarySerializer.Deserialize <MetadataDocument>(keyValuePair.Value); initializationVector = metadataDocument.InitializationVector; } encryptor = new Aes256Encryptor(key, initializationVector); fieldIndexes = new FieldIndexes <TDocument>(indexFactory, encryptor); } catch (Exception) { journalWriter.Discard(); throw; } }