Skip to content

Commit 8805aaa

Browse files
committed
Add GetMachineCode that uses the UUID insead of relying on external dependency
1 parent 899c801 commit 8805aaa

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

src/main/java/io/cryptolens/methods/Helpers.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import oshi.hardware.HardwareAbstractionLayer;
1313
import oshi.software.os.OperatingSystem;
1414

15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStreamReader;
1518
import java.nio.charset.StandardCharsets;
1619
import java.security.MessageDigest;
1720

@@ -29,12 +32,72 @@ public class Helpers {
2932
* Returns a unique identifier of the device. Note, root access may be required.
3033
* Note, this method is not the same as the one used in our .NET client.
3134
* Also, this method only works on desktop computers.
35+
* @apiNote If you do not want to depend on slf4j or if you use the cryptolens-android
36+
* binary, please call GetMachineCode with v=2.
3237
*/
3338
public static String GetMachineCode() {
3439

3540
return SHA256(getRawDeviceID());
3641
}
3742

43+
/**
44+
* This method uses a special OS command to find the UUID of the device. In comparison
45+
* to the default method, GetMachineCode, this method does not depend on slf4j to compute
46+
* the device fingerprint, assuming that v=2. If v=2, the result of this method should be
47+
* the same as in the .NET SDK and Python on Windows.
48+
* @param v If set to 2, this method will use the UUID of the device instead of depending
49+
* on slf4j. Note, it currently only supports Windows. You can read more
50+
* here: https://help.cryptolens.io/faq/index#java.
51+
*/
52+
public static String GetMachineCode(int v) {
53+
54+
if (v == 1) {
55+
return GetMachineCode();
56+
}
57+
58+
String operSys = System.getProperty("os.name").toLowerCase();
59+
60+
if (operSys.contains("win")) {
61+
62+
try {
63+
Process process = null;
64+
65+
process = Runtime.getRuntime().exec("cmd.exe /C wmic csproduct get uuid\n");
66+
67+
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
68+
String line = null;
69+
70+
StringBuilder sb = new StringBuilder();
71+
while (true) {
72+
line = in.readLine();
73+
if (line == null) {
74+
break;
75+
}
76+
sb.append(line);
77+
}
78+
79+
String res = sb.toString();
80+
return SHA256(res.substring(res.indexOf("UUID:") + 6).toString().trim());
81+
82+
} catch(Exception e){
83+
84+
return null;
85+
}
86+
87+
88+
}else if (operSys.contains("nix") || operSys.contains("nux")
89+
|| operSys.contains("aix")) {
90+
return "This OS is not supported yet.";
91+
} else if (operSys.contains("mac")) {
92+
return "This OS is not supported yet.";
93+
94+
}
95+
else{
96+
return "This OS is not supported yet.";
97+
}
98+
99+
}
100+
38101
/**
39102
* Check if the device is registered with the license key.
40103
* @param license The license key object.
@@ -43,7 +106,7 @@ public static String GetMachineCode() {
43106
public static boolean IsOnRightMachine(LicenseKey license) {
44107
return IsOnRightMachine(license, false);
45108
}
46-
109+
47110
/**
48111
* Check if the device is registered with the license key.
49112
* @param license The license key object.
@@ -273,7 +336,7 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) {
273336
/**
274337
* Return the sha256 checksum of a string.
275338
*/
276-
private static String SHA256(String rawData) {
339+
public static String SHA256(String rawData) {
277340

278341
StringBuffer hexString = new StringBuffer();
279342

0 commit comments

Comments
 (0)