Sample Codes - Java

OTCHandler.java

Run this script and you'll see the execution results.

bash-3.2$ java OTCHandler
>>> Exporting these params to HTML: 
APP_ID:Bitrue
signature:uTHvD4z3r+o1se/CVxqvp1E7TU80F/ers7y9wo6Kp1w=
APP_URL:https://www.bitrue.com/
APP_KEY:Aw4fHfWZwFFi9q2l
APP_UID:sdfSEEC293Bd1ZZ5VO98z8Gf/z39m6RjSxhdOV9nW+8+6j9j+ZV2OOfXWwyChSJ3+xzRZulDyi:OTg3NjVmZWRjYmE0MzIxMA==
timestamp:1587426337
import java.util.*;
import java.util.Map.*;
import java.time.Instant;
import java.io.*;


public class OTCHandler extends TBClient{

    /**
     *  Pass params to HTML
     *
     *  Prepare the params
     *
     */
    private static Map<String, String> htmlParams(){

        Map<String, String> html_params = new HashMap<String, String>();

        long unixTime = Instant.now().getEpochSecond();

        String signature = sign(unixTime);
        String UID = OTCHandler.getUID();
        String encryptedUID = OTCHandler.encryptUID(UID);

        html_params.put("timestamp", Long.toString(unixTime));
        html_params.put("signature", signature);
        html_params.put("APP_ID", APP_ID);
        html_params.put("APP_URL", APP_URL);
        html_params.put("APP_KEY", APP_KEY);
        html_params.put("APP_UID", encryptedUID);

        return html_params;

    }

    /**
     * Please implement this method to get the UID of your user
     *
     * e.g. sha1(user_id+salt)
     */
    private static String getUID(){

        String internalUID =  "d58e3582afa99040e27b92b13c8f2280";
        return internalUID;
    }

    //Test if the params are generated correctly
    public static void main (String[] args) {

        Map<String, String> htmlParams = OTCHandler.htmlParams();

        System.out.println(">>> Exporting these params to HTML: ");

        if(!htmlParams.isEmpty()) {
            Iterator it = htmlParams.entrySet().iterator();
            while(it.hasNext()) {
                Map.Entry obj = (Entry)it.next();
                System.out.println(obj.getKey() + ":" + obj.getValue());
            }
        }
    }
}

TBClient.java

This is the parent class with the algorithms to calculate the signature and encrypt the UID.

-The request signature is one-way encrypted,there's no way to restore the original contents.
-UID is two-way encrypted, so that we can decrypt it and make it readable again, when needed.

Run the script -

bash-3.2$ java TBClient
========== Encrypting UID ===========
Original UID: 
d58e3582afa99040e27b92b13c8f2280
Encrypted UID:
sdfSEEC293Bd1ZZ5VO98z8Gf/z39m6RjSxhdOV9nW+8+6j9j+ZV2OOfXWwyChSJ3+xzRZulDyi:OTg3NjVmZWRjYmE0MzIxMA==
========== Calculating Signature ===========
EvScdTj1K59+UOfNFK5P+YFx2jhSlb5oejIHGGtvRXE=
/**
 * Copyright (c) 2020 Legend Trading Inc.
 *
 * Java Client of Legend Gateway
 *
 */

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import javax.xml.bind.DatatypeConverter;
import java.util.Base64;

import java.time.Instant;

import java.util.*;
import java.util.Map.*;


public class TBClient {

    protected static final String APP_ID = "Bitrue";
    protected static final String APP_URL = "https://www.bitrue.com/";
    protected static final String APP_KEY = "Aw4fHfWZwFFi9q2l";
    protected static final String APP_SECRET = "5z5znf230itm1ygx";
    protected static final String APP_PASSPHRASE = "sdfSEEC293";


    private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";
    private static String CIPHER_VI = "98765fedcba43210";
    private static int CIPHER_KEY_LEN = 16; //128 bits


    /**
     * Generate the request signature.
     *
     * @return
     */
    public static String sign(long unixTime){

        try {

            String message = Long.toString(unixTime) + TBClient.APP_URL;

            Mac hasher = Mac.getInstance("HmacSHA256");
            hasher.init(new SecretKeySpec(TBClient.APP_SECRET.getBytes(), "HmacSHA256"));

            byte[] hash = hasher.doFinal(message.getBytes());

            String signature = DatatypeConverter.printBase64Binary(hash);

            return signature;
        }

        catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        catch (InvalidKeyException ex) {
            ex.printStackTrace();
        }

        return null;

    }

    /**
     * Encrypt UID
     *
     * @param UID
     * @return
     */
    public static String encryptUID(String UID){
        String encrytedUID = TBClient.encrypt(TBClient.APP_SECRET, TBClient.CIPHER_VI, UID);
        return TBClient.APP_PASSPHRASE+ encrytedUID;

    }


    /**
     * Data encryption
     *
     * @param key  - key to use should be 16 bytes long (128 bits)
     * @param iv - initialization vector
     * @param data - data to encrypt
     * @return encryptedData data in base64 encoding with iv attached at end after a :
     */
    public static String encrypt(String key, String iv, String data) {
        try {
            if (key.length() < TBClient.CIPHER_KEY_LEN) {
                int numPad = TBClient.CIPHER_KEY_LEN - key.length();

                for (int i = 0; i < numPad; i++) {
                    key += "0"; //0 pad to len 16 bytes
                }

            } else if (key.length() > TBClient.CIPHER_KEY_LEN) {
                key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }


            IvParameterSpec initVector = new IvParameterSpec(iv.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance(TBClient.CIPHER_NAME);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector);

            byte[] encryptedData = cipher.doFinal((data.getBytes()));

            String base64_EncryptedData = Base64.getEncoder().encodeToString(encryptedData);
            String base64_IV = Base64.getEncoder().encodeToString(iv.getBytes("UTF-8"));

            return base64_EncryptedData + ":" + base64_IV;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }


    public static void testUID () {

        System.out.println("========== Encrypting UID ===========");

        String uid = "d58e3582afa99040e27b92b13c8f2280";
        System.out.println("Original UID: ");
        System.out.println(uid);

        String uidEncrypted = TBClient.encryptUID(uid);

        System.out.println("Encrypted UID:");
        System.out.println(uidEncrypted);

    }

    public static void testSig () {

        System.out.println("========== Calculating Signature ===========");
        long unixTime = Instant.now().getEpochSecond();
        String signature = TBClient.sign(unixTime);
        System.out.println(signature);


    }

    public static void main (String[] args) {
        TBClient.testUID();
        TBClient.testSig();
    }
}