Skip to content

Commit 12ffc6b

Browse files
committed
Add support for machine code generation
1 parent c9f0188 commit 12ffc6b

File tree

8 files changed

+211
-109
lines changed

8 files changed

+211
-109
lines changed

.idea/artifacts/cryptolens_jar.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 112 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ Assuming you have referenced the `cryptolens.jar` file, the code below should ge
1111

1212
```java
1313
import io.cryptolens.Cryptolens;
14+
import io.cryptolens.Helpers;
1415
import io.cryptolens.LicenseKey;
1516

1617
public class Main {
1718

1819
public static void main(String[] args) {
19-
String RSAPubKey = "<RSAKeyValue><Modulus>khbyu3/vAEBHi339fTuo2nUaQgSTBj0jvpt5xnLTTF35FLkGI+5Z3wiKfnvQiCLf+5s4r8JB/Uic/i6/iNjPMILlFeE0N6XZ+2pkgwRkfMOcx6eoewypTPUoPpzuAINJxJRpHym3V6ZJZ1UfYvzRcQBD/lBeAYrvhpCwukQMkGushKsOS6U+d+2C9ZNeP+U+uwuv/xu8YBCBAgGb8YdNojcGzM4SbCtwvJ0fuOfmCWZvUoiumfE4x7rAhp1pa9OEbUe0a5HL+1v7+JLBgkNZ7Z2biiHaM6za7GjHCXU8rojatEQER+MpgDuQV3ZPx8RKRdiJgPnz9ApBHFYDHLDzDw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
20+
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
2021

2122
Cryptolens cryptolens = Cryptolens.getDefault();
22-
cryptolens.setRSAPubKey(RSAPubKey);
23+
cryptolens.setRSAPublicKey(RSAPubKey);
2324

2425
Cryptolens.ActivateResponse response =
25-
cryptolens.activate( "WyI0NjUiLCJBWTBGTlQwZm9WV0FyVnZzMEV1Mm9LOHJmRDZ1SjF0Vk52WTU0VzB2Il0="
26-
, 3646
27-
, "MPDWY-PQAOW-FKSCH-SGAAU"
28-
, "289jf2afs3"
26+
cryptolens.activate( "WyIyNTk1IiwidVVrQm94OGlYS3pHZlhTc0x6Rm9mN1piektrT0FSd0REaFZ0ZXZJMSJd"
27+
, 3349
28+
, "ICVLD-VVSZR-ZTICT-YKGXL"
29+
, Helpers.GetMachineCode()
2930
);
3031

3132
if (!response.successful()) {
@@ -51,6 +52,7 @@ public class Main {
5152
System.out.println(licenseKey.getF1());
5253
}
5354
}
55+
5456
```
5557

5658
In order to adjust this code to your products, several parameters needs to be changed, which we outline below:
@@ -59,6 +61,5 @@ In order to adjust this code to your products, several parameters needs to be ch
5961
* `token` - the access token (can be found [here](https://app.cryptolens.io/docs/api/v3/QuickStart#api-keys), in *API Keys* section).
6062
* `product_id` - the id of the product can be found on the product page (in the example above, it's 3646).
6163
* `key` - the license key to be verified (above it's MPDWY-PQAOW-FKSCH-SGAAU).
62-
* `machine_code` - the unique id of the device (we are working on adding a method similar to `Helpers.GetMachineCode()`). In the code above, we hard coded it to 289jf2afs3.
63-
64+
* `machine_code` - the unique id of the device, which may require root access. Note, this value is not the same as the one generated by the .NET client.
6465

example-app/src/Main.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import io.cryptolens.Cryptolens;
2+
import io.cryptolens.Helpers;
23
import io.cryptolens.LicenseKey;
34

45
public class Main {
56

67
public static void main(String[] args) {
7-
String RSAPubKey = "<RSAKeyValue><Modulus>khbyu3/vAEBHi339fTuo2nUaQgSTBj0jvpt5xnLTTF35FLkGI+5Z3wiKfnvQiCLf+5s4r8JB/Uic/i6/iNjPMILlFeE0N6XZ+2pkgwRkfMOcx6eoewypTPUoPpzuAINJxJRpHym3V6ZJZ1UfYvzRcQBD/lBeAYrvhpCwukQMkGushKsOS6U+d+2C9ZNeP+U+uwuv/xu8YBCBAgGb8YdNojcGzM4SbCtwvJ0fuOfmCWZvUoiumfE4x7rAhp1pa9OEbUe0a5HL+1v7+JLBgkNZ7Z2biiHaM6za7GjHCXU8rojatEQER+MpgDuQV3ZPx8RKRdiJgPnz9ApBHFYDHLDzDw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
8+
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
89

910
Cryptolens cryptolens = Cryptolens.getDefault();
10-
cryptolens.setRSAPubKey(RSAPubKey);
11+
cryptolens.setRSAPublicKey(RSAPubKey);
1112

1213
Cryptolens.ActivateResponse response =
13-
cryptolens.activate( "WyI0NjUiLCJBWTBGTlQwZm9WV0FyVnZzMEV1Mm9LOHJmRDZ1SjF0Vk52WTU0VzB2Il0="
14-
, 3646
15-
, "MPDWY-PQAOW-FKSCH-SGAAU"
16-
, "289jf2afs3"
14+
cryptolens.activate( "WyIyNTk1IiwidVVrQm94OGlYS3pHZlhTc0x6Rm9mN1piektrT0FSd0REaFZ0ZXZJMSJd"
15+
, 3349
16+
, "ICVLD-VVSZR-ZTICT-YKGXL"
17+
, Helpers.GetMachineCode()
1718
);
1819

1920
if (!response.successful()) {

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>io.cryptolens</groupId>
55
<artifactId>cryptolens</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.1</version>
7+
<version>1.2</version>
88
<name>cryptolens</name>
99
<description>A client api to access Cryptolens Licensing API.</description>
1010
<url>https://github.com/cryptolens/cryptolens-java</url>
@@ -24,5 +24,10 @@
2424
<version>3.8.1</version>
2525
<scope>test</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>com.github.oshi</groupId>
29+
<artifactId>oshi-core</artifactId>
30+
<version>LATEST</version>
31+
</dependency>
2732
</dependencies>
2833
</project>

src/main/java/io/cryptolens/Cryptolens.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private void setModulusBase64(String modulus) {
5252
* Sets the RSA Public key to verify the response from the server. You can
5353
* find it here: https://app.cryptolens.io/docs/api/v3/QuickStart#api-key.
5454
*/
55-
public void setRSAPubKey (String RSAPubKey) {
55+
public void setRSAPublicKey (String RSAPubKey) {
5656
int mstart = RSAPubKey.indexOf("<Modulus>");
5757
int mend = RSAPubKey.indexOf("</Modulus>");
5858

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.cryptolens;
2+
3+
import oshi.SystemInfo;
4+
import oshi.hardware.CentralProcessor;
5+
import oshi.hardware.ComputerSystem;
6+
import oshi.hardware.HardwareAbstractionLayer;
7+
import oshi.software.os.OperatingSystem;
8+
9+
import java.nio.charset.StandardCharsets;
10+
import java.security.MessageDigest;
11+
12+
public class Helpers {
13+
14+
/**
15+
* Returns a unique identifier of the device. Note, root access may be required.
16+
* Furthermore, this method is not the same as the one used in our .NET client.
17+
*/
18+
public static String GetMachineCode() {
19+
20+
return SHA256(getRawDeviceID());
21+
}
22+
23+
/**
24+
* Return the sha256 checksum of a string.
25+
*/
26+
private static String SHA256(String rawData) {
27+
28+
StringBuffer hexString = new StringBuffer();
29+
30+
try {
31+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
32+
byte[] hash = digest.digest(rawData.getBytes(StandardCharsets.UTF_8));
33+
34+
//thanks to https://stackoverflow.com/a/5470268.
35+
36+
for (int i = 0; i < hash.length; i++) {
37+
if ((0xff & hash[i]) < 0x10) {
38+
hexString.append("0"
39+
+ Integer.toHexString((0xFF & hash[i])));
40+
} else {
41+
hexString.append(Integer.toHexString(0xFF & hash[i]));
42+
}
43+
}
44+
45+
return hexString.toString();
46+
} catch (Exception e) {
47+
return null;
48+
}
49+
}
50+
51+
private static String getRawDeviceID()
52+
{
53+
//thanks to https://stackoverflow.com/a/37705082. may require root.
54+
SystemInfo systemInfo = new SystemInfo();
55+
OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
56+
HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
57+
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
58+
ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();
59+
60+
String vendor = operatingSystem.getManufacturer();
61+
String processorSerialNumber = computerSystem.getSerialNumber();
62+
String processorIdentifier = centralProcessor.getIdentifier();
63+
int processors = centralProcessor.getLogicalProcessorCount();
64+
65+
return vendor +
66+
processorSerialNumber +
67+
processorIdentifier +
68+
processors;
69+
}
70+
71+
}

src/test/java/io/cryptolens/AppTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ public static Test suite()
3434
public void testApp()
3535
{
3636
assertTrue( true );
37+
3738
}
3839
}

0 commit comments

Comments
 (0)