Java

Maven

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.3</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20210307</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>1.5.30</version>
    </dependency>
</dependencies>

Gradle

dependencies {
    implementation 'org.projectlombok:lombok:RELEASE'
    implementation 'com.squareup.okhttp3:okhttp:4.9.3'
    implementation 'org.json:json:20210307'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30'
}

Java Example

Headers

Name
Value

Content-Type

application/json

Authorization

Key <token>

Body

Name
Description

licenseKey

License key provided by the bot.

productName

Name of the product.

version

Current product version.

userId

Customer discord user Id.

hwid

Hardware Id of the user.

Main.java

package org.example;

import org.json.JSONObject;
import java.net.URI;
import java.util.Arrays;
import java.utul.List;

public class Main {
    public static void main(String[] args) {
        URI host = URI.create("http://<DOMAIN>:<PORT>/api/v1/key");
        String licenseKey = "";
        String product = "";
        String version = "";
        String userId = "";
        String token = "";
        String hwid = HWID.getHWID();

        JSONObject obj = LicenseCheckUtil.CheckLicense(host, licenseKey, product, version, userId, hwid, secretKey);
        int status = obj.getInt("status");

        List<Integer> errorStatus = Arrays.asList(400, 403, 404);

        if (errorStatus.contains(status)) {
            System.out.println("Validation failed ⟶  " + "( " + licenseKey + " )");
            System.out.println(" ");
            System.out.println("Reason: " + "( " + obj.getString("message") + " )");
            System.exit(1);
        } else if (status == 200) {
            System.out.println("License validated ⟶  " + "( " + licenseKey + " )");
            System.out.println(" ");
            System.out.println("Message: " + "( ✅ You can now use: 'your app' )")
        } else {
            System.out.println("Validation failed ⟶  " + "( " + licenseKey + " )");
            System.out.println(" ");
            System.out.println("Reason: " + "( " + obj.getString("message") + " )");
            System.exit(1);
        }
    }
}

LicenseCheck.java

package org.example;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;

class LicenseCheck {
    public static JSONObject checkLicense(URI host, String licenseKey, String product, String version, String userId, String hwid, String token) {
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");

        JSONObject jsonBody = new JSONObject();
        jsonBody.put("licenseKey", licenseKey);
        jsonBody.put("product", product);
        jsonBody.put("version", version);
        jsonBody.put("hwid", hwid);
        jsonBody.put("userId", userId);

        RequestBody body = RequestBody.create(mediaType, jsonBody.toString());
        Request request = new Request.Builder()
                .url(host.toString())
                .post(body)
                .addHeader("Authorization", "Key " + token)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            String data = response.body().string();
            return new JSONObject(data);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

HWID.java

package org.example;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HWID {

    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String getHWID() {
        return bytesToHex(generateHWID());
    }

    public static byte[] generateHWID() {
        try {
            MessageDigest hash = MessageDigest.getInstance("MD5");

            String s = System.getProperty("os.name") + System.getProperty("os.arch") + System.getProperty("os.version")
                    + Runtime.getRuntime().availableProcessors() + System.getenv("PROCESSOR_IDENTIFIER")
                    + System.getenv("PROCESSOR_ARCHITECTURE") + System.getenv("PROCESSOR_ARCHITEW6432")
                    + System.getenv("NUMBER_OF_PROCESSORS");
            return hash.digest(s.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new Error("Algorithm wasn't found.", e);
        }

    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Last updated