Load() public method

Load a KDB file from a stream.
public Load ( Stream sSource, KdbxFormat kdbFormat, IStatusLogger slLogger ) : void
sSource Stream Stream to read the data from. Must contain /// a KDBX stream.
kdbFormat KdbxFormat Format specifier.
slLogger IStatusLogger Status logger (optional).
return void
Exemplo n.º 1
0
        internal static PwGroup ReadGroup(Stream msData, PwDatabase pdContext,
                                          bool bCopyIcons, bool bNewUuids, bool bSetCreatedNow)
        {
            PwDatabase pd = new PwDatabase();

            pd.New(new IOConnectionInfo(), new CompositeKey());

            KdbxFile f = new KdbxFile(pd);

            f.Load(msData, KdbxFormat.PlainXml, null);

            if (bCopyIcons)
            {
                PwDatabase.CopyCustomIcons(pd, pdContext, pd.RootGroup, true);
            }

            if (bNewUuids)
            {
                pd.RootGroup.Uuid = new PwUuid(true);
                pd.RootGroup.CreateNewItemUuids(true, true, true);
            }

            if (bSetCreatedNow)
            {
                pd.RootGroup.SetCreatedNow(true);
            }

            return(pd.RootGroup);
        }
Exemplo n.º 2
0
    public static void Main(string[] args)
    {
        CompositeKey key = new CompositeKey();
        KcpPassword pw = new KcpPassword("12345");
        key.AddUserKey(pw);
        byte[] pwdata = pw.KeyData.ReadData();
        Console.WriteLine("PW data:");
        Console.WriteLine(string.Join(",", pwdata.Select(x => "0x" + x.ToString("x"))));
        byte[] keydata = key.GenerateKey32(pwdata, 6000).ReadData();
        Console.WriteLine("Key data:");
        Console.WriteLine(string.Join(",", keydata.Select(x => "0x" + x.ToString("x"))));

        PwDatabase db = new PwDatabase();
        db.MasterKey = key;
        KdbxFile kdbx = new KdbxFile(db);
        kdbx.Load(@"..\resources\test.kdbx", KdbxFormat.Default, null);

        var groups = db.RootGroup.GetGroups(true);
        Console.WriteLine("Group count: " + groups.UCount);
        var entries = db.RootGroup.GetEntries(true);
        Console.WriteLine("Entry count: " + entries.UCount);

        CompositeKey key2 = new CompositeKey();
        key2.AddUserKey(pw);
        KcpKeyFile keyfile = new KcpKeyFile(@"..\resources\keyfile.key");
        key2.AddUserKey(keyfile);
        byte[] keyfiledata = keyfile.KeyData.ReadData();
        Console.WriteLine("Key file data:");
        Console.WriteLine(string.Join(",", keyfiledata.Select(x => "0x" + x.ToString("x"))));
        Console.WriteLine("Composite Key data:");
        byte[] key2data = key2.GenerateKey32(keyfiledata, 6000).ReadData();
        Console.WriteLine(string.Join(",", key2data.Select(x => "0x" + x.ToString("x"))));
    }
Exemplo n.º 3
0
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        public static List <PwEntry> ReadEntries(Stream msData)
        {
            /* KdbxFile f = new KdbxFile(pwDatabase);
             * f.m_format = KdbxFormat.PlainXml;
             *
             * XmlDocument doc = new XmlDocument();
             * doc.Load(msData);
             *
             * XmlElement el = doc.DocumentElement;
             * if(el.Name != ElemRoot) throw new FormatException();
             *
             * List<PwEntry> vEntries = new List<PwEntry>();
             *
             * foreach(XmlNode xmlChild in el.ChildNodes)
             * {
             *      if(xmlChild.Name == ElemEntry)
             *      {
             *              PwEntry pe = f.ReadEntry(xmlChild);
             *              pe.Uuid = new PwUuid(true);
             *
             *              foreach(PwEntry peHistory in pe.History)
             *                      peHistory.Uuid = pe.Uuid;
             *
             *              vEntries.Add(pe);
             *      }
             *      else { Debug.Assert(false); }
             * }
             *
             * return vEntries; */

            PwDatabase pd = new PwDatabase();
            KdbxFile   f  = new KdbxFile(pd);

            f.Load(msData, KdbxFormat.PlainXml, null);

            List <PwEntry> vEntries = new List <PwEntry>();

            foreach (PwEntry pe in pd.RootGroup.Entries)
            {
                pe.SetUuid(new PwUuid(true), true);
                vEntries.Add(pe);
            }

            return(vEntries);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        public static List<PwEntry> ReadEntries(Stream msData)
        {
            /* KdbxFile f = new KdbxFile(pwDatabase);
            f.m_format = KdbxFormat.PlainXml;

            XmlDocument doc = new XmlDocument();
            doc.Load(msData);

            XmlElement el = doc.DocumentElement;
            if(el.Name != ElemRoot) throw new FormatException();

            List<PwEntry> vEntries = new List<PwEntry>();

            foreach(XmlNode xmlChild in el.ChildNodes)
            {
                if(xmlChild.Name == ElemEntry)
                {
                    PwEntry pe = f.ReadEntry(xmlChild);
                    pe.Uuid = new PwUuid(true);

                    foreach(PwEntry peHistory in pe.History)
                        peHistory.Uuid = pe.Uuid;

                    vEntries.Add(pe);
                }
                else { Debug.Assert(false); }
            }

            return vEntries; */

            PwDatabase pd = new PwDatabase();
            KdbxFile f = new KdbxFile(pd);
            f.Load(msData, KdbxFormat.PlainXml, null);

            List<PwEntry> vEntries = new List<PwEntry>();
            foreach(PwEntry pe in pd.RootGroup.Entries)
            {
                pe.SetUuid(new PwUuid(true), true);
                vEntries.Add(pe);
            }

            return vEntries;
        }
        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) { }
        }
        /// <summary>
        /// Read entries from a stream.
        /// </summary>
        /// <param name="msData">Input stream to read the entries from.</param>
        /// <returns>Extracted entries.</returns>
        /// <param name="pdContext">Context database (e.g. for storing icons).</param>
        /// <param name="bCopyIcons">If <c>true</c>, custom icons required by
        /// the loaded entries are copied to the context database.</param>
        /// <returns>Loaded entries.</returns>
        public static List <PwEntry> ReadEntries(Stream msData, PwDatabase pdContext,
                                                 bool bCopyIcons)
        {
            List <PwEntry> lEntries = new List <PwEntry>();

            /* KdbxFile f = new KdbxFile(pwDatabase);
             * if(msData == null) { Debug.Assert(false); return lEntries; }
             * f.m_format = KdbxFormat.PlainXml;
             *
             * XmlDocument doc = new XmlDocument();
             * doc.Load(msData);
             *
             * XmlElement el = doc.DocumentElement;
             * if(el.Name != ElemRoot) throw new FormatException();
             *
             * List<PwEntry> vEntries = new List<PwEntry>();
             *
             * foreach(XmlNode xmlChild in el.ChildNodes)
             * {
             *      if(xmlChild.Name == ElemEntry)
             *      {
             *              PwEntry pe = f.ReadEntry(xmlChild);
             *              pe.Uuid = new PwUuid(true);
             *
             *              foreach(PwEntry peHistory in pe.History)
             *                      peHistory.Uuid = pe.Uuid;
             *
             *              vEntries.Add(pe);
             *      }
             *      else { Debug.Assert(false); }
             * }
             *
             *
             * return vEntries; */

            PwDatabase pd = new PwDatabase();

            pd.New(new IOConnectionInfo(), new CompositeKey());

            KdbxFile f = new KdbxFile(pd);

            f.Load(msData, KdbxFormat.PlainXml, null);

            foreach (PwEntry pe in pd.RootGroup.Entries)
            {
                pe.SetUuid(new PwUuid(true), true);
                lEntries.Add(pe);

                if (bCopyIcons && (pdContext != null))
                {
                    PwUuid pu = pe.CustomIconUuid;
                    if (!pu.Equals(PwUuid.Zero))
                    {
                        int iSrc = pd.GetCustomIconIndex(pu);
                        int iDst = pdContext.GetCustomIconIndex(pu);

                        if (iSrc < 0)
                        {
                            Debug.Assert(false);
                        }
                        else if (iDst < 0)
                        {
                            pdContext.CustomIcons.Add(pd.CustomIcons[iSrc]);

                            pdContext.Modified          = true;
                            pdContext.UINeedsIconUpdate = true;
                        }
                    }
                }
            }

            return(lEntries);
        }
Exemplo n.º 7
0
        public override void Import(PwDatabase pwStorage, Stream sInput,
			IStatusLogger slLogger)
        {
            KdbxFile kdbx = new KdbxFile(pwStorage);
            kdbx.Load(sInput, KdbxFormat.PlainXml, slLogger);
        }
Exemplo n.º 8
0
		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 = new XmlDocument();
			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();
			XmlWriterSettings xws = new XmlWriterSettings();
			xws.Encoding = StrUtil.Utf8;
			xws.Indent = true;
			xws.IndentChars = "\t";
			XmlWriter xw = XmlWriter.Create(msMod, xws);
			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);
		}
Exemplo n.º 9
0
        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();
        }
Exemplo n.º 10
0
		/// <summary>
		/// Read entries from a stream.
		/// </summary>
		/// <param name="msData">Input stream to read the entries from.</param>
		/// <param name="pdContext">Context database (e.g. for storing icons).</param>
		/// <param name="bCopyIcons">If <c>true</c>, custom icons required by
		/// the loaded entries are copied to the context database.</param>
		/// <returns>Loaded entries.</returns>
		public static List<PwEntry> ReadEntries(Stream msData, PwDatabase pdContext,
			bool bCopyIcons)
		{
			List<PwEntry> lEntries = new List<PwEntry>();

			if(msData == null) { Debug.Assert(false); return lEntries; }
			// pdContext may be null

			/* KdbxFile f = new KdbxFile(pwDatabase);
			f.m_format = KdbxFormat.PlainXml;

			XmlDocument doc = new XmlDocument();
			doc.Load(msData);

			XmlElement el = doc.DocumentElement;
			if(el.Name != ElemRoot) throw new FormatException();

			List<PwEntry> vEntries = new List<PwEntry>();

			foreach(XmlNode xmlChild in el.ChildNodes)
			{
				if(xmlChild.Name == ElemEntry)
				{
					PwEntry pe = f.ReadEntry(xmlChild);
					pe.Uuid = new PwUuid(true);

					foreach(PwEntry peHistory in pe.History)
						peHistory.Uuid = pe.Uuid;

					vEntries.Add(pe);
				}
				else { Debug.Assert(false); }
			}

			return vEntries; */

			PwDatabase pd = new PwDatabase();
			pd.New(new IOConnectionInfo(), new CompositeKey());

			KdbxFile f = new KdbxFile(pd);
			f.Load(msData, KdbxFormat.PlainXml, null);

			foreach(PwEntry pe in pd.RootGroup.Entries)
			{
				pe.SetUuid(new PwUuid(true), true);
				lEntries.Add(pe);

				if(bCopyIcons && (pdContext != null))
				{
					PwUuid pu = pe.CustomIconUuid;
					if(!pu.Equals(PwUuid.Zero))
					{
						int iSrc = pd.GetCustomIconIndex(pu);
						int iDst = pdContext.GetCustomIconIndex(pu);

						if(iSrc < 0) { Debug.Assert(false); }
						else if(iDst < 0)
						{
							pdContext.CustomIcons.Add(pd.CustomIcons[iSrc]);

							pdContext.Modified = true;
							pdContext.UINeedsIconUpdate = true;
						}
					}
				}
			}

			return lEntries;
		}