Rainy.Db.DbEncryptedStorage.ReEncryptAllNotes C# (CSharp) Method

ReEncryptAllNotes() public method

Reencrypts all notes. This may happen i.e. if the user changes his password.
public ReEncryptAllNotes ( string new_encryption_key ) : void
new_encryption_key string The new key that is used for encryption.
return void
        public void ReEncryptAllNotes(string new_encryption_key)
        {
            var old_notes = GetDBNotes ();
            old_notes.ForEach (n => DecryptNoteBody (n));

            this.encryptionMasterKey = new_encryption_key;
            foreach (var note in old_notes) {
                SaveDBNote (note);
            }
        }

Usage Example

        public void StorageCanBeReusedAfterReEncryptNotes()
        {
            DbEncryptedStorage storage = new DbEncryptedStorage (this.connFactory, this.testUser, key);
            var sample_notes = DbStorageTests.GetSampleNotes ();
            foreach(var note in sample_notes) {
                storage.SaveNote (note);
            }
            storage.Dispose ();

            storage = new DbEncryptedStorage (connFactory, testUser, key);
            var new_key = CryptoHelper.Create256BitLowerCaseHexKey (new RNGCryptoServiceProvider ());

            storage.ReEncryptAllNotes (new_key);
            var decrypted_notes = storage.GetNotes ();

            foreach (var note in sample_notes) {
                Assert.AreEqual (note.Text, decrypted_notes.Values.First (n => n.Guid == note.Guid).Text);
            }
        }