-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplicationSigner.cs
106 lines (99 loc) · 5.17 KB
/
ApplicationSigner.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
namespace OpenKNX.Toolbox.Sign
{
class ApplicationProgramHasher
{
public ApplicationProgramHasher(
FileInfo applProgFile,
IDictionary<string, string> mapBaggageIdToFileIntegrity,
string basePath,
int nsVersion,
bool patchIds = true)
{
Assembly asm = Assembly.LoadFrom(Path.Combine(basePath, "Knx.Ets.XmlSigning.dll"));
System.Version lVersion = asm.GetName().Version ?? new System.Version("0.0.0");
if(lVersion >= new System.Version("6.2.0")) { //ab ETS6.2
Assembly objm = Assembly.LoadFrom(Path.Combine(basePath, "Knx.Ets.Common.dll"));
Type? schemaVersion = objm.GetType("Knx.Ets.Common.Schema.KnxXmlSchemaVersion");
if(schemaVersion == null)
throw new Exception("Could not create schemaVersion");
object knxSchemaVersion = Enum.ToObject(schemaVersion, nsVersion);
_type = asm.GetType("Knx.Ets.XmlSigning.Signer.ApplicationProgramHasher");
if(_type == null)
throw new Exception("Could not create ApplicationProgramHasher");
_instance = Activator.CreateInstance(_type, applProgFile, mapBaggageIdToFileIntegrity, patchIds, knxSchemaVersion);
} else if(lVersion >= new System.Version("6.0.0")) { //ab ETS6.0/6.1
Assembly objm = Assembly.LoadFrom(Path.Combine(basePath, "Knx.Ets.Xml.ObjectModel.dll"));
Type? schemaVersion = objm.GetType("Knx.Ets.Xml.ObjectModel.KnxXmlSchemaVersion");
if(schemaVersion == null)
throw new Exception("Could not create schemaVersion");
object knxSchemaVersion = Enum.ToObject(schemaVersion, nsVersion);
if (lVersion < new System.Version("6.1.0"))
_type = asm.GetType("Knx.Ets.XmlSigning.ApplicationProgramHasher");
else
_type = asm.GetType("Knx.Ets.XmlSigning.Signer.ApplicationProgramHasher");
if(_type == null)
throw new Exception("Could not create ApplicationProgramHasher");
_instance = Activator.CreateInstance(_type, applProgFile, mapBaggageIdToFileIntegrity, patchIds, knxSchemaVersion);
} else { //für ETS5 und früher
_type = asm.GetType("Knx.Ets.XmlSigning.ApplicationProgramHasher");
if(_type == null)
throw new Exception("Could not create ApplicationProgramHasher");
_instance = Activator.CreateInstance(_type, applProgFile, mapBaggageIdToFileIntegrity, patchIds);
}
}
public void HashFile()
{
if(_instance == null || _type == null)
throw new Exception("Could not hash file. _instance or _type is null");
MethodInfo? hashfile = _type.GetMethod("HashFile", BindingFlags.Instance | BindingFlags.Public);
if(hashfile == null)
throw new Exception("Could not hash file. hashfile method not found");
hashfile.Invoke(_instance, null);
}
public string OldApplProgId
{
get
{
if(_instance == null || _type == null)
throw new Exception("Could not get OldApplProgId. _instance or _type is null");
PropertyInfo? propertyInfo = _type.GetProperty("OldApplProgId", BindingFlags.Public | BindingFlags.Instance);
if(propertyInfo == null)
throw new Exception("Could not get OldApplProgId. property is null");
object? value = propertyInfo.GetValue(_instance);
if(value == null)
throw new Exception("Could not get OldApplProgId. value is null");
return value.ToString() ?? "";
}
}
public string NewApplProgId
{
get
{
if(_instance == null || _type == null)
throw new Exception("Could not get NewApplProgId. _instance or _type is null");
object? value = _type.GetProperty("NewApplProgId", BindingFlags.Public | BindingFlags.Instance)?.GetValue(_instance);
if(value == null)
throw new Exception("Could not get NewApplProgId. value is null");
return value.ToString() ?? "";
}
}
public string GeneratedHashString
{
get
{
if(_instance == null || _type == null)
throw new Exception("Could not get GeneratedHashString. _instance or _type is null");
object? value = _type.GetProperty("GeneratedHashString", BindingFlags.Public | BindingFlags.Instance)?.GetValue(_instance);
if(value == null)
throw new Exception("Could not get GeneratedHashString. value is null");
return value.ToString() ?? "";
}
}
private readonly object? _instance;
private readonly Type? _type;
}
}