public void Decryption(string path, string password, bool hasKey, int count) { var key = new CompositeKey(); if (password != null) { key.AddUserKey(new KcpPassword("12345")); } if (hasKey) { var keyPath = $"{Path.GetFileNameWithoutExtension(path)}.key"; key.AddUserKey(new KcpKeyFile(File.ReadAllBytes(GetFullPath(keyPath)))); } var db = new PwDatabase { MasterKey = key }; var file = new KdbxFile(db); file.Load(GetFullPath(path), KdbxFormat.Default, null); Assert.Equal(count, (int)db.RootGroup.Entries.UCount); }
public override void Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger) { KdbxFile kdbx = new KdbxFile(pwStorage); kdbx.Load(sInput, KdbxFormat.Default, slLogger); }
public void TestSearch() { var database = new PwDatabase(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(TestDatabase))) { var file = new KdbxFile(database); file.Load(ms, KdbxFormat.PlainXml, null); } var sp = new SearchParameters() { SearchString = "sfsoiwsefsi" }; var listStorage = new PwObjectList <PwEntry>(); database.RootGroup.SearchEntries(sp, listStorage); Assert.Equal(0U, listStorage.UCount); var entry = new PwEntry(true, true); entry.Strings.Set("Title", new ProtectedString(false, "NaMe")); database.RootGroup.AddEntry(entry, true); sp.SearchString = "name"; database.RootGroup.SearchEntries(sp, listStorage); Assert.Equal(1U, listStorage.UCount); }
public void PopulateDatabaseFromStream(PwDatabase db, Stream s, IStatusLogger slLogger) { KdbxFile kdbx = new KdbxFile(db); kdbx.DetachBinaries = db.DetachBinaries; kdbx.Load(s, _format, slLogger); HashOfLastStream = kdbx.HashOfFileOnDisk; s.Close(); }
private async Task OpenFile(string fileName) { await using var fs = File.OpenRead(fileName); var database = new PwDatabase(); database.MasterKey = new CompositeKey(); database.MasterKey.AddUserKey(new KcpPassword("myPass")); var file = new KdbxFile(database); file.Load(fs, KdbxFormat.Default, loggerService); }
public override void Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger) { KdbxFile kdbx = new KdbxFile(pwStorage); // CappedByteStream s = new CappedByteStream(sInput, 64); kdbx.RepairMode = true; try { kdbx.Load(sInput, KdbxFormat.Default, slLogger); } catch (Exception) { } }
public void TestLoad() { var database = new PwDatabase(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(TestDatabase))) { var file = new KdbxFile(database); file.Load(ms, KdbxFormat.PlainXml, null); } //Assert.That(database.Color.ToArgb(), Is.EqualTo(Color.Red.ToArgb())); Assert.Equal(PwCompressionAlgorithm.GZip, database.Compression); //Assert.That (database.CustomData, Is.EqualTo ()); Assert.True(database.CustomIcons.Count == 0); }
public override async Task <IKeePassDatabase> UnlockAsync(IFile dbFile, KeePassCredentials credentials) { Log.Info("Unlocking {Database}", dbFile); try { var compositeKey = new CompositeKey(); if (credentials.Password != null) { compositeKey.AddUserKey(new KcpPassword(credentials.Password)); } if (credentials.KeyFile != null) { compositeKey.AddUserKey(new KcpKeyFile(await credentials.KeyFile.ReadFileBytesAsync())); } var db = new PwDatabase { MasterKey = compositeKey }; var kdbx = new KdbxFile(db); using (var fs = await dbFile.OpenReadAsync()) { await Task.Run(() => { kdbx.Load(fs, KdbxFormat.Default, null); }); return(new KdbxDatabase(dbFile, db, dbFile.IdFromPath())); } } catch (InvalidCompositeKeyException e) { throw new InvalidCredentialsException(e); } }
private static void PerformXmlReplace(PwDatabase pd, XmlReplaceOptions opt, IStatusLogger sl) { if (opt.SelectNodesXPath.Length == 0) { return; } if (opt.Operation == XmlReplaceOp.None) { return; } bool bRemove = (opt.Operation == XmlReplaceOp.RemoveNodes); bool bReplace = (opt.Operation == XmlReplaceOp.ReplaceData); bool bMatchCase = ((opt.Flags & XmlReplaceFlags.CaseSensitive) != XmlReplaceFlags.None); bool bRegex = ((opt.Flags & XmlReplaceFlags.Regex) != XmlReplaceFlags.None); Regex rxFind = null; if (bReplace && bRegex) { rxFind = new Regex(opt.FindText, (bMatchCase ? RegexOptions.None : RegexOptions.IgnoreCase)); } EnsureStandardFieldsExist(pd); KdbxFile kdbxOrg = new KdbxFile(pd); MemoryStream msOrg = new MemoryStream(); kdbxOrg.Save(msOrg, null, KdbxFormat.PlainXml, sl); byte[] pbXml = msOrg.ToArray(); msOrg.Close(); string strXml = StrUtil.Utf8.GetString(pbXml); XmlDocument xd = XmlUtilEx.CreateXmlDocument(); xd.LoadXml(strXml); XPathNavigator xpNavRoot = xd.CreateNavigator(); XPathNodeIterator xpIt = xpNavRoot.Select(opt.SelectNodesXPath); // XPathNavigators must be cloned to make them independent List <XPathNavigator> lNodes = new List <XPathNavigator>(); while (xpIt.MoveNext()) { lNodes.Add(xpIt.Current.Clone()); } if (lNodes.Count == 0) { return; } for (int i = lNodes.Count - 1; i >= 0; --i) { if ((sl != null) && !sl.ContinueWork()) { return; } XPathNavigator xpNav = lNodes[i]; if (bRemove) { xpNav.DeleteSelf(); } else if (bReplace) { ApplyReplace(xpNav, opt, rxFind); } else { Debug.Assert(false); } // Unknown action } MemoryStream msMod = new MemoryStream(); using (XmlWriter xw = XmlUtilEx.CreateXmlWriter(msMod)) { xd.Save(xw); } byte[] pbMod = msMod.ToArray(); msMod.Close(); PwDatabase pdMod = new PwDatabase(); msMod = new MemoryStream(pbMod, false); try { KdbxFile kdbxMod = new KdbxFile(pdMod); kdbxMod.Load(msMod, KdbxFormat.PlainXml, sl); } catch (Exception) { throw new Exception(KPRes.XmlModInvalid + MessageService.NewParagraph + KPRes.OpAborted + MessageService.NewParagraph + KPRes.DbNoModBy.Replace(@"{PARAM}", @"'" + KPRes.XmlReplace + @"'")); } finally { msMod.Close(); } PrepareModDbForMerge(pdMod, pd); pd.Modified = true; pd.UINeedsIconUpdate = true; pd.MergeIn(pdMod, PwMergeMethod.Synchronize, sl); }