forked from JanisEst/KeePassQuickUnlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
112 lines (98 loc) · 3.67 KB
/
Extensions.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
using KeePass.App.Configuration;
using KeePassLib.Keys;
using KeePassLib.Security;
using KeePassLib.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace KeePassQuickUnlock
{
public static class Extensions
{
/// <summary>A CompositeKey extension method that combines keys.</summary>
/// <remarks>Keep in sync with CompositeKey.CreateRawCompositeKey32().</remarks>
/// <param name="key">The CompositeKey to act on.</param>
/// <returns>A ProtectedBinary with the combined keys.</returns>
public static ProtectedBinary CombineKeys(this CompositeKey key)
{
var dataList = new List<byte[]>();
int dataLength = 0;
foreach (var pKey in key.UserKeys)
{
var b = pKey.KeyData;
if (b != null)
{
var keyData = b.ReadData();
dataList.Add(keyData);
dataLength += keyData.Length;
}
}
var allData = new byte[dataLength];
int p = 0;
foreach (var pbData in dataList)
{
Array.Copy(pbData, 0, allData, p, pbData.Length);
p += pbData.Length;
MemUtil.ZeroByteArray(pbData);
}
var pb = new ProtectedBinary(true, allData);
MemUtil.ZeroByteArray(allData);
return pb;
}
/// <summary>A ProtectedString extension method that gets the characters.</summary>
/// <param name="ps">The ProtectedString to act on.</param>
/// <returns>An array of characters.</returns>
public static char[] GetChars(this ProtectedString ps)
{
var pb = ps.ReadUtf8();
var chars = StrUtil.Utf8.GetChars(pb);
MemUtil.ZeroByteArray(pb);
return chars;
}
/// <summary>A char[] extension method that converts the chars to a protected string.</summary>
/// <param name="chars">The chars to act on.</param>
/// <returns>The given data converted to a ProtectedString.</returns>
public static ProtectedString ToProtectedString(this char[] chars)
{
return chars.ToProtectedString(0, chars.Length);
}
/// <summary>A char[] extension method that converts this object to a protected string.</summary>
/// <param name="chars">The chars to act on.</param>
/// <param name="charIndex">Zero-based index of the first character.</param>
/// <param name="charCount">Number of characters.</param>
/// <returns>The given data converted to a ProtectedString.</returns>
public static ProtectedString ToProtectedString(this char[] chars, int charIndex, int charCount)
{
var pb = StrUtil.Utf8.GetBytes(chars, charIndex, charCount);
var ps = new ProtectedString(true, pb);
MemUtil.ZeroByteArray(pb);
return ps;
}
/// <summary>An AceCustomConfig extension method that sets an enum.</summary>
/// <typeparam name="T">Generic enum parameter.</typeparam>
/// <param name="config">The AceCustomConfig instance to act on.</param>
/// <param name="strID">The key identifier.</param>
/// <param name="eValue">The value.</param>
public static void SetEnum<T>(this AceCustomConfig config, string strID, T eValue) where T : struct, IConvertible
{
Contract.Requires(typeof(T).IsEnum);
config.SetLong(strID, (int)(object)eValue);
}
/// <summary>An AceCustomConfig extension method that gets an enum.</summary>
/// <typeparam name="T">Generic enum parameter.</typeparam>
/// <param name="config">The AceCustomConfig instance to act on.</param>
/// <param name="strID">The key identifier.</param>
/// <param name="eDefault">The default value.</param>
/// <returns>The enum value.</returns>
public static T GetEnum<T>(this AceCustomConfig config, string strID, T eDefault) where T : struct, IConvertible
{
Contract.Requires(typeof(T).IsEnum);
var value = config.GetLong(strID, -1);
if (value == -1)
{
return eDefault;
}
return (T)(object)(int)value;
}
}
}