示例#1
0
        /// <summary>
        /// Save the contents of the current <c>PwDatabase</c> to a KDB file.
        /// </summary>
        /// <param name="strSaveToFile">Location to save the KDB file to.</param>
        public void Save(string strSaveToFile, PwGroup pgDataSource)
        {
            Debug.Assert(strSaveToFile != null);
            if (strSaveToFile == null)
            {
                throw new ArgumentNullException("strSaveToFile");
            }

            using (KdbManager mgr = new KdbManager())
            {
                KdbErrorCode e = KdbFile.SetDatabaseKey(mgr, m_pwDatabase.MasterKey);
                if (e != KdbErrorCode.Success)
                {
                    Debug.Assert(false);
                    throw new Exception(KLRes.InvalidCompositeKey);
                }

                if (m_slLogger != null)
                {
                    if (m_pwDatabase.MasterKey.ContainsType(typeof(KcpUserAccount)))
                    {
                        m_slLogger.SetText(KPRes.KdbWUA, LogStatusType.Warning);
                    }

                    if (m_pwDatabase.Name.Length != 0)
                    {
                        m_slLogger.SetText(KdbPrefix + KPRes.FormatNoDatabaseName, LogStatusType.Warning);
                    }
                    if (m_pwDatabase.Description.Length != 0)
                    {
                        m_slLogger.SetText(KdbPrefix + KPRes.FormatNoDatabaseDesc, LogStatusType.Warning);
                    }
                }

                // Set properties
                AesKdf kdf = new AesKdf();
                if (!kdf.Uuid.Equals(m_pwDatabase.KdfParameters.KdfUuid))
                {
                    mgr.KeyTransformationRounds = (uint)PwDefs.DefaultKeyEncryptionRounds;
                }
                else
                {
                    ulong uRounds = m_pwDatabase.KdfParameters.GetUInt64(
                        AesKdf.ParamRounds, PwDefs.DefaultKeyEncryptionRounds);
                    uRounds = Math.Min(uRounds, 0xFFFFFFFEUL);

                    mgr.KeyTransformationRounds = (uint)uRounds;
                }

                PwGroup pgRoot = (pgDataSource ?? m_pwDatabase.RootGroup);

                // Write groups and entries
                Dictionary <PwGroup, UInt32> dictGroups = WriteGroups(mgr, pgRoot);
                WriteEntries(mgr, dictGroups, pgRoot);

                e = mgr.SaveDatabase(strSaveToFile);
                if (e != KdbErrorCode.Success)
                {
                    throw new Exception(KLRes.FileSaveFailed);
                }
            }
        }