예제 #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
        public ExportItem(string uuid, Uri path, DateTime?lastExportTimestamp, KeePassLib.Security.ProtectedString password)
        {
            _uuid = uuid;
            _path = path;
            _lastExportTimeStamp = lastExportTimestamp;
            _password            = password;

            if (ReferenceEquals(path, null) || (!path.IsFile && !path.IsUnc))
            {
                throw new ArgumentNullException("path");
            }
        }
예제 #3
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);
        }
예제 #4
0
        private static Exception CheckArgument(KeePassLib.PwDatabase database, Uri filePath, KeePassLib.Security.ProtectedString password)
        {
            if (ReferenceEquals(database, null))
            {
                return(new ArgumentNullException("database"));
            }
            if (ReferenceEquals(filePath, null))
            {
                return(new ArgumentNullException("filePath"));
            }
            if (!filePath.IsFile)
            {
                return(new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Path [{0}] is not a file", filePath.LocalPath), "filePath"));
            }
            if (!Path.GetExtension(filePath.LocalPath).Equals(KeePassDatabaseExtension, StringComparison.InvariantCultureIgnoreCase))
            {
                return(new ArgumentException(string.Format(CultureInfo.InvariantCulture, "File [{0}] must be a KeePass database (*.{1})", filePath.LocalPath, KeePassDatabaseExtension), "filePath"));
            }
            if (ReferenceEquals(password, null) || password.IsEmpty)
            {
                return(new ArgumentNullException("password"));
            }

            return(null);
        }