-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyFileUtils.cpp
More file actions
82 lines (60 loc) · 2.36 KB
/
KeyFileUtils.cpp
File metadata and controls
82 lines (60 loc) · 2.36 KB
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
/*
==============================================================================
KeyFileUtils.cpp
Created: 23 Sep 2019 6:51:55pm
Author: Edwin Park
==============================================================================
*/
#include "KeyFileUtils.h"
using juce::BigInteger;
using juce::CharPointer_UTF8;
using juce::MemoryOutputStream;
KeyFileUtils::KeyFileData KeyFileUtils::getDataFromKeyFile(XmlElement xml) {
KeyFileData data;
data.user_name = xml.getStringAttribute("user_name");
data.user_email = xml.getStringAttribute("user_email");
data.product_id = xml.getStringAttribute("product_id");
data.product_version_range = xml.getStringAttribute("product_version_range");
if (xml.hasAttribute("product_expiry") &&
xml.hasAttribute("expiring_machine_ids")) {
data.keyFileExpires = true;
data.machine_ids.addArray(getMachineIDs(xml, "expiring_machine_ids"));
data.expiryTime =
Time(xml.getStringAttribute("product_expiry").getHexValue64());
} else {
data.keyFileExpires = false;
if (xml.hasAttribute("machine_ids"))
data.machine_ids.addArray(getMachineIDs(xml, "machine_ids"));
}
return data;
}
XmlElement KeyFileUtils::getXmlFromKeyFile(String keyFileText,
RSAKey rsaPublicKey) {
return decryptXML(keyFileText.fromLastOccurrenceOf("#", false, false).trim(),
rsaPublicKey);
}
// crypto
// =======================================================================================
String KeyFileUtils::encryptXML(const XmlElement &xml, RSAKey privateKey) {
MemoryOutputStream text;
xml.writeTo(text);
BigInteger val;
val.loadFromMemoryBlock(text.getMemoryBlock());
privateKey.applyToValue(val);
return val.toString(16);
}
XmlElement KeyFileUtils::decryptXML(String hexData, RSAKey rsaPublicKey) {
BigInteger val;
val.parseString(hexData, 16);
RSAKey key(rsaPublicKey);
jassert(key.isValid());
std::unique_ptr<XmlElement> xml;
if (!val.isZero()) {
key.applyToValue(val);
auto mb = val.toMemoryBlock();
if (CharPointer_UTF8::isValidString(static_cast<const char *>(mb.getData()),
(int)mb.getSize()))
xml = parseXML(mb.toString());
}
return xml != nullptr ? *xml : XmlElement("key");
}