-
Notifications
You must be signed in to change notification settings - Fork 7
/
KeyHelper.cs
108 lines (96 loc) · 3.79 KB
/
KeyHelper.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
using System;
using System.IO;
using KeePass;
using KeePass.Resources;
using KeePassLib.Keys;
using KeePassLib.Serialization;
using KeePassLib.Utility;
namespace KeePassSubsetExport
{
public static class KeyHelper
{
/// <summary>
/// Adds a password to a <see cref="CompositeKey"/>.
/// </summary>
/// <param name="passwordByteArray">The password as byte array to add to the <see cref="CompositeKey"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/> to add the password to.</param>
/// <returns>true if sucessfull, false otherwise.</returns>
public static bool AddPasswordToKey(byte[] passwordByteArray, CompositeKey key)
{
if (passwordByteArray.Length == 0)
{
return false;
}
key.AddUserKey(new KcpPassword(passwordByteArray));
return true;
}
/// <summary>
/// Adds a password to a <see cref="CompositeKey"/>.
/// </summary>
/// <param name="password">The password to add to the <see cref="CompositeKey"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/> to add the password to.</param>
/// <returns>true if sucessfull, false otherwise.</returns>
public static bool AddPasswordToKey(string password, CompositeKey key)
{
if (password == "")
{
return false;
}
key.AddUserKey(new KcpPassword(password));
return true;
}
/// <summary>
/// Adds a keyfile to a <see cref="CompositeKey"/>.
/// </summary>
/// <param name="keyFilePath">The path to the keyfile to add to the <see cref="CompositeKey"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/> to add the keyfile to.</param>
/// <param name="connectionInfo">The <see cref="IOConnectionInfo"/> object of the database (required for <see cref="KeyProviderQueryContext"/>).</param>
/// <returns>true if sucessfull, false otherwise.</returns>
public static bool AddKeyfileToKey(string keyFilePath, CompositeKey key, IOConnectionInfo connectionInfo)
{
bool success = false;
if (!File.Exists(keyFilePath))
{
return false;
}
bool bIsKeyProv = Program.KeyProviderPool.IsKeyProvider(keyFilePath);
if (!bIsKeyProv)
{
try
{
key.AddUserKey(new KcpKeyFile(keyFilePath, true));
success = true;
}
catch (InvalidDataException exId)
{
MessageService.ShowWarning(keyFilePath, exId);
}
catch (Exception exKf)
{
MessageService.ShowWarning(keyFilePath, KPRes.KeyFileError, exKf);
}
}
else
{
KeyProviderQueryContext ctxKp = new KeyProviderQueryContext(connectionInfo, true, false);
KeyProvider prov = Program.KeyProviderPool.Get(keyFilePath);
bool bPerformHash = !prov.DirectKey;
byte[] pbCustomKey = prov.GetKey(ctxKp);
if ((pbCustomKey != null) && (pbCustomKey.Length > 0))
{
try
{
key.AddUserKey(new KcpCustomKey(keyFilePath, pbCustomKey, bPerformHash));
success = true;
}
catch (Exception exCkp)
{
MessageService.ShowWarning(exCkp);
}
MemUtil.ZeroByteArray(pbCustomKey);
}
}
return success;
}
}
}