Пример #1
0
        private string CreateAddress(IotaSetting settings)
        {
            var group = this.m_host.Database.RootGroup.FindCreateGroup(settings.FolderName, true);

            var entry = new KeePassLib.PwEntry(true, true);

            entry.Strings.Set(PwDefs.TitleField, new KeePassLib.Security.ProtectedString(false, settings.EntryTitle));
            KeePassLib.Security.ProtectedString protectedStringSeed = CreateSeed();
            entry.Strings.Set(PwDefs.PasswordField, protectedStringSeed);
            group.AddEntry(entry, true);

            if (protectedStringSeed.IsEmpty)
            {
                return("Seed value is empty!");
            }
            var seed = protectedStringSeed.ReadString();

            var addresses = new List <string>();

            for (int i = 0; i < settings.NoOfAddress; i++)
            {
                var address = this.CreateAddress(protectedStringSeed.ReadString(), i, settings.SecurityLevel);
                addresses.Add(address);

                if (this.ProgressChanged != null)
                {
                    this.ProgressChanged(this, new EventArgs());
                }
            }

            if (settings.Storagelocation == Storagelocation.Notes)
            {
                var notesString     = String.Join("\n", addresses.Select((address, i) => $"Address {i.ToString().PadLeft(3, '0')} {address}"));
                var protectedString = new KeePassLib.Security.ProtectedString(false, notesString);
                entry.Strings.Set(PwDefs.NotesField, protectedString);
            }
            else
            {
                for (int i = 0; i < addresses.Count; i++)
                {
                    var addressName     = "Address " + i.ToString().PadLeft(3, '0');
                    var protectedString = new KeePassLib.Security.ProtectedString(false, addresses[i]);
                    entry.Strings.Set(addressName, protectedString);
                }
            }

            try
            {
                CheckAndAddIotaIcon();
                entry.CustomIconUuid = new PwUuid(this.iconUuId);
            }
            catch { }

            this.m_host.Database.Modified = true;
            // Force the groups to refresh
            m_host.MainWindow.UpdateUI(false, null, true, m_host.Database.RootGroup, true, null, true);

            return("Addresses created");
        }
Пример #2
0
        private static bool Export(KeePassLib.PwDatabase database, Uri filePath, KeePassLib.Security.ProtectedString password, KeePassLib.Interfaces.IStatusLogger logger)
        {
            Exception argumentError = CheckArgument(database, filePath, password);

            if (!ReferenceEquals(argumentError, null))
            {
                throw argumentError;
            }

            if (string.Equals(database.IOConnectionInfo.Path, filePath.LocalPath, StringComparison.InvariantCultureIgnoreCase))
            {
                return(false); //Don't export myself
            }
            //Create new database in temporary file
            KeePassLib.PwDatabase exportedDatabase = new KeePassLib.PwDatabase();
            exportedDatabase.Compression = KeePassLib.PwCompressionAlgorithm.GZip;
            KeePassLib.Serialization.IOConnectionInfo connectionInfo = new KeePassLib.Serialization.IOConnectionInfo();
            string storageDirectory = Path.GetDirectoryName(filePath.LocalPath);
            string tmpPath          = Path.Combine(storageDirectory, string.Format("{0}{1}", Guid.NewGuid(), KeePassDatabaseExtension));

            connectionInfo.Path         = tmpPath;
            connectionInfo.CredSaveMode = KeePassLib.Serialization.IOCredSaveMode.SaveCred;
            KeePassLib.Keys.CompositeKey exportedKey = new KeePassLib.Keys.CompositeKey();
            exportedKey.AddUserKey(new KeePassLib.Keys.KcpPassword(password.ReadString()));
            exportedDatabase.New(connectionInfo, exportedKey);
            exportedDatabase.RootGroup.Name = database.RootGroup.Name;

            //Merge current database in temporary file
            exportedDatabase.MergeIn(database, KeePassLib.PwMergeMethod.OverwriteExisting, logger);
            exportedDatabase.Save(logger);
            exportedDatabase.Close();

            //Move temporary file into target backup path
            if (File.Exists(filePath.LocalPath))
            {
                File.Delete(filePath.LocalPath);
            }
            File.Move(tmpPath, filePath.LocalPath);

            return(true);
        }