-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DicewarePwGenerator.cs
119 lines (92 loc) · 3.44 KB
/
DicewarePwGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
KeePassDiceware Plugin
Copyright (C) 2021-2022 cmd <https://github.com/cmdwtf>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics;
using System.Windows.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Cryptography;
using KeePassLib.Cryptography.PasswordGenerator;
using KeePassLib.Security;
namespace KeePassDiceware
{
public sealed class DicewarePwGenerator : CustomPwGenerator
{
public override PwUuid Uuid { get; } = new PwUuid(new byte[] {
0xBA, 0x8E, 0x96, 0xF8, 0x66, 0xAE, 0x41, 0xEB,
0x9E, 0xE3, 0x90, 0xC9, 0x9E, 0xAD, 0x86, 0x0B, });
public override string Name => nameof(Diceware);
public override bool SupportsOptions => true;
public IPluginHost Host { get; private set; }
private Options _options;
private static string OptionsKey { get; } = typeof(Options).FullName;
public DicewarePwGenerator(IPluginHost host)
{
Host = host;
// load (legacy) global options, if there are any.
LoadPluginOptions(null);
}
public override ProtectedString Generate(PwProfile profile, CryptoRandomStream random)
{
Debug.Assert(profile != null);
string uuidString = Convert.ToBase64String(Uuid.UuidBytes, Base64FormattingOptions.None);
Debug.Assert(profile.CustomAlgorithmUuid == uuidString);
LoadPluginOptions(profile.CustomAlgorithmOptions);
string result = Diceware.Generate(_options, profile, random);
return new ProtectedString(false, result);
}
private void LoadPluginOptions(string optionalSerializedOptions)
{
// try to load options based on given serialized options
if (!string.IsNullOrWhiteSpace(optionalSerializedOptions))
{
if (Options.TryDeserialize(optionalSerializedOptions, out _options))
{
_options ??= Options.Default();
return;
}
}
// no luck with profile-specific options, load 'global' ones
// that may have been used with a previous version of the plugin.
string optionsString = Host.CustomConfig.GetString(OptionsKey);
if (Options.TryDeserialize(optionsString, out _options))
{
_options ??= Options.Default();
return;
}
// failed to deserialize 'global' options, use new defaults.
_options = Options.Default();
}
public override string GetOptions(string strCurrentOptions)
{
if (Host == null)
{
return string.Empty;
}
// load the options given to us by KeePass as current
LoadPluginOptions(strCurrentOptions);
using var dof = new DicewareOptionsForm();
dof.Options = _options;
if (dof.ShowDialog(GlobalWindowManager.TopWindow) == DialogResult.OK)
{
_options = dof.Options;
return dof.Options.Serialize();
}
// user declined to save changes -- return original value.
return strCurrentOptions;
}
}
}