public void AttachBinary(Entry entry, string key, ProtectedMemoryBinary protectedMemoryBinary) { lock(this.syncLock) { int id = this.Binaries.Count; this.Binaries.Add(new BinaryMapping() { Id = id, Key = key, Value = protectedMemoryBinary }); entry.Binaries.Add(new ProtectedBinary() { Key = key, Ref = id, Value = protectedMemoryBinary }); this.Meta.Binaries.Add(new Binary() { Compressed = this.HeaderInfo.DatabaseCompression == (byte)1, Id = id }); } }
public void WriteToStreamAndVerify() { var path = Env.ResolvePath("~/Resources/NewDatabase23.kdbx"); if (System.IO.File.Exists(path)) System.IO.File.Delete(path); try { var key = new MasterKey(); key.Add(new MasterKeyPassword("megatron")); // terrible pw. var package = new KeePassPackage(key); using (var fs = System.IO.File.OpenWrite(path)) { var group = new Group(); group.Name = "Sample"; package.Root.Groups.Add(group); var entry = new Entry(true); var fields = entry.AsEntryFields(); fields.Title = "Test"; fields.Password = "******"; group.Entries.Add(entry); package.Save(fs); using (var fs2 = System.IO.File.OpenRead(Env.ResolvePath("~/Resources/NewDatabase23.kdbx"))) { var package2 = new KeePassPackage(key); package2.Open(fs2); Assert.NotNull(package2); Assert.NotNull(package2.Root); Assert.NotNull(package2.Root.Groups); Assert.Equal(1, package2.Root.Groups.Count); Assert.NotNull(package2.Root.Groups[0].Entries); Assert.Equal(1, package2.Root.Groups[0].Entries.Count); var entry2 = package2.Root.Groups[0].Entries[0]; var titleString = entry2.Strings.SingleOrDefault(o => o.Key == "Title"); var passwordString = entry2.Strings.SingleOrDefault(o => o.Key == "Password"); Assert.Equal("Test", titleString.Value.Unprotect()); Assert.Equal("Password", passwordString.Value.Unprotect()); } } } finally { //if (System.IO.File.Exists(path)) // System.IO.File.Delete(path); } }