-
Notifications
You must be signed in to change notification settings - Fork 7
/
FieldHelper.cs
60 lines (47 loc) · 1.98 KB
/
FieldHelper.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
using System.Linq;
using KeePass.Util.Spr;
using KeePassLib;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePassSubsetExport
{
public static class FieldHelper
{
private static readonly byte[] RefStringByteArray = new byte[]
{
123, 82, 69, 70, 58
};
public static ProtectedString GetFieldWRef(PwEntry entry, PwDatabase sourceDb, string fieldName)
{
ProtectedString orgValue = entry.Strings.GetSafe(fieldName);
byte[] orgValueByteArray = orgValue.ReadUtf8();
// Check if the protected string begins with the ref marker
bool isRef = orgValueByteArray.Take(5).SequenceEqual(RefStringByteArray);
MemUtil.ZeroByteArray(orgValueByteArray);
if (!isRef)
{
// The protected string is not a ref -> return the protected string directly
return orgValue;
}
SprContext ctx = new SprContext(entry, sourceDb,
SprCompileFlags.All, false, false);
// the protected string is a reference -> decode it and look it up
return new ProtectedString(true, SprEngine.Compile(
orgValue.ReadString(), ctx));
}
public static string GetFieldWRefUnprotected(PwEntry entry, PwDatabase sourceDb, string fieldName)
{
string orgValue = entry.Strings.ReadSafe(fieldName);
// Check if the string begins with the ref marker or contains a %
if (!orgValue.StartsWith("{REF") && !orgValue.Contains("%"))
{
// The string is not a ref -> return the string directly
return orgValue;
}
SprContext ctx = new SprContext(entry, sourceDb,
SprCompileFlags.All, false, false);
// the string is a reference -> decode it and look it up
return SprEngine.Compile(orgValue, ctx);
}
}
}