package acquire.sdk;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.newland.nsdk.core.api.common.ModuleType;
import com.newland.nsdk.core.api.common.crypto.AsymAlgorithmParameters;
import com.newland.nsdk.core.api.common.crypto.MessageDigestType;
import com.newland.nsdk.core.api.common.exception.NSDKException;
import com.newland.nsdk.core.api.common.keymanager.AsymKeyType;
import com.newland.nsdk.core.api.common.keymanager.AsymKeyUsage;
import com.newland.nsdk.core.api.common.keymanager.AsymmetricKey;
import com.newland.nsdk.core.api.common.keymanager.KeyGenerateMethod;
import com.newland.nsdk.core.api.common.keymanager.KeyUsage;
import com.newland.nsdk.core.api.internal.NSDKModuleManager;
import com.newland.nsdk.core.api.internal.crypto.Crypto;
import com.newland.nsdk.core.api.internal.keymanager.KeyManager;
import com.newland.nsdk.core.external.ExtNSDKModuleManagerImpl;
import com.newland.nsdk.core.internal.NSDKModuleManagerImpl;
import com.newland.nsdk.plugin.rkl.common.DeviceType;
import com.newland.nsdk.plugin.rkl.external.ExternalRKLAPI;
import com.newland.nsdk.plugin.rkl.internal.InternalRKLAPI;
import com.newland.rkl.KeyInfo;
import com.newland.rkl.RKLErrorCode;
import com.newland.rkl.RKLProcessor;
import com.newland.rkl.RKLStatus;
import com.newland.rkl.api.RKLAPI;
import com.newland.rkl.exception.DeviceException;
import com.newland.rkl.exception.RKLException;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import acquire.base.utils.LoggerUtils;
import acquire.base.utils.file.FileUtils;

/**
 * A tool for connecting FlyKey Service. FlyKey is a service for download key
 * remotely.
 *
 * @author Janson
 * @date 2022/5/24 17:08
 */
public class FlyKeyHelper {

    private final static String CONFIG_PRO = "RKLCONF_PRO";
    private final static String CONFIG_DEV = "RKLCONF_DEV";
    private final static boolean DEBUG = true;

    /**
     * load key remotely (fly key)
     */
    /**
     * load key remotely (fly key)
     */
    public static void downloadMsterKey(Context context, boolean isExternalPinpad, FlyKeyListener listener) {
        dependenciesDetect();
        try {
            String configFile;
            RKLAPI rklapi;
            LoggerUtils.d("start fly key");
            if (DEBUG) {
                // Debug
                configFile = CONFIG_DEV;
                rklapi = new DemoRKLAPI(context, NSDKModuleManagerImpl.getInstance());
                ((DemoRKLAPI) rklapi).installDefaultDeviceCertKey();
            } else {
                // Product
                configFile = CONFIG_PRO;
                if (isExternalPinpad) {
                    rklapi = new ExternalRKLAPI(context, ExtNSDKModuleManagerImpl.getInstance(), DeviceType.PRO);
                } else {
                    rklapi = new InternalRKLAPI(context, NSDKModuleManagerImpl.getInstance());
                }
            }
            String filePath = context.getExternalFilesDir(null) + File.separator + configFile;
            if (new File(filePath).exists()) {
                new File(filePath).delete();
            }
            try (InputStream inputStream = context.getAssets().open(configFile)) {
                FileUtils.copyFileByInputStream(inputStream, new File(filePath));
            }
            RKLProcessor.getInstance().setAPI(rklapi);
            RKLProcessor.getInstance().start(filePath, (status, errorCode, message) -> {
                LoggerUtils.d("status:" + status + ",error code:" + errorCode + ",message:" + message);
                if (status != RKLStatus.ENDED) {
                    return;
                }
                if (errorCode != RKLErrorCode.SUCCESS) {
                    // failed
                    FlyKeyErrorBean errorBean;
                    try {
                        errorBean = new Gson().fromJson(message, FlyKeyErrorBean.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        errorBean = new FlyKeyErrorBean();
                        errorBean.setMessage(context.getString(R.string.sdk_helper_fly_key_error_unavailable_message));
                        errorBean.setDetailErrorCode(-999);
                    }
                    listener.onFailed(errorBean);
                    return;
                }
                // success
                try {
                    int keyCount = RKLProcessor.getInstance().getInstalledKeyNum();
                    LoggerUtils.d("response key count: " + keyCount);
                    if (keyCount == 0) {
                        // NO new key
                        listener.onSuccess(new int[0]);
                        return;
                    }
                    // installation completed
                    List<KeyInfo> keys = RKLProcessor.getInstance().getInstalledKeyInfo();
                    int[] indexes = new int[keys.size()];
                    for (int i = 0; i < keys.size(); i++) {
                        KeyInfo key = keys.get(i);
                        indexes[i] = key.getIndex();
                        LoggerUtils.d("installed key: " + key);
                    }
                    listener.onSuccess(indexes);
                } catch (RKLException e) {
                    listener.onException(e);
                }

            });
        } catch (Exception e) {
            LoggerUtils.e("Request Error");
            listener.onException(e);
        }
    }

    /**
     * Load custom keys from Map data
     */
    /**
     * Load custom keys from Map data
     */
    public static void loadCustomKeys(Context context, java.util.Map<String, Object> keyData, FlyKeyListener listener) {
        try {
            LoggerUtils.d("Starting custom key load");
            KeyManager keyManager = (KeyManager) NSDKModuleManagerImpl.getInstance().getModule(ModuleType.KEY_MANAGER);

            if (keyData.containsKey("keys")) {
                Object keysObj = keyData.get("keys");
                if (keysObj instanceof List) {
                    List<?> keysList = (List<?>) keysObj;
                    int[] successIndexes = new int[keysList.size()];
                    int count = 0;

                    for (Object item : keysList) {
                        if (item instanceof java.util.Map) {
                            java.util.Map<?, ?> keyMap = (java.util.Map<?, ?>) item;
                            int index = ((Double) keyMap.get("index")).intValue();
                            String value = (String) keyMap.get("value");

                            byte[] keyBytes = hexStringToByteArray(value);

                            com.newland.nsdk.core.api.common.keymanager.SymmetricKey symmetricKey = new com.newland.nsdk.core.api.common.keymanager.SymmetricKey();
                            symmetricKey.setKeyID((byte) index);
                            symmetricKey.setKeyType(com.newland.nsdk.core.api.common.keymanager.KeyType.DES); // Use
                                                                                                              // KeyType.DES
                            symmetricKey.setKeyUsage(com.newland.nsdk.core.api.common.keymanager.KeyUsage.KEK); // Use
                                                                                                                // KeyUsage.KEK
                                                                                                                // for
                                                                                                                // Master
                                                                                                                // Key
                            symmetricKey.setKeyData(keyBytes);
                            symmetricKey.setKeyLen(keyBytes.length);

                            // Use generateKey to load clear key
                            keyManager.generateKey(KeyGenerateMethod.CLEAR, null, null, symmetricKey);
                            successIndexes[count++] = index;
                            LoggerUtils.d("Loaded key at index: " + index);
                        }
                    }
                    listener.onSuccess(successIndexes);
                } else {
                    throw new Exception("Invalid 'keys' format");
                }
            } else {
                throw new Exception("Missing 'keys' field");
            }
        } catch (Exception e) {
            LoggerUtils.e("Custom key load failed", e);
            FlyKeyErrorBean error = new FlyKeyErrorBean();
            error.setMessage(e.getMessage());
            listener.onFailed(error);
        }
    }

    private 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;
    }

    private static boolean hasDependencies;

    private static void dependenciesDetect() {
        if (hasDependencies) {
            return;
        }
        try {
            Class.forName("com.google.gson.Gson");
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("Make sure to add the ’Gson’ library to your dependencies.");
        }
        try {
            Class.forName("retrofit2.Retrofit");
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("Make sure to add the ’retrofit’ library to your dependencies.");
        }
        try {
            Class.forName("retrofit2.converter.gson.GsonConverterFactory");
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("Make sure to add the ’converter-gson’ library to your dependencies.");
        }
        hasDependencies = true;
    }

    /**
     * Fly key error information
     *
     * @author Janson
     * @date 2022/5/25 11:48
     */
    public static class FlyKeyErrorBean {
        private int detailErrorCode;
        private String errorType;
        private String message;

        public int getDetailErrorCode() {
            return detailErrorCode;
        }

        public void setDetailErrorCode(int detailErrorCode) {
            this.detailErrorCode = detailErrorCode;
        }

        public String getErrorType() {
            return errorType;
        }

        public void setErrorType(String errorType) {
            this.errorType = errorType;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    public interface FlyKeyListener {
        /**
         * download and install success.
         *
         * @param indexes key indexes。
         */
        void onSuccess(@NonNull int[] indexes);

        /**
         * download failed
         *
         * @param errorBean Fly Key error
         */
        void onFailed(FlyKeyErrorBean errorBean);

        /**
         * caught an exception when installing keys.
         */
        void onException(Exception e);
    }

    /**
     * demo rkl api. It's just used for debug mode.
     *
     * @author Janson
     * @date 2022/5/24 17:40
     */
    private static class DemoRKLAPI extends InternalRKLAPI {
        public static final int CERT_PRIVATE_KEY_ID = 249;

        public static final byte[] CERT_PRIVATE_KEY = {
                (byte) 0x30, (byte) 0x82, (byte) 0x04, (byte) 0xA3, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0x02,
                (byte) 0x82, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0xDE, (byte) 0xE3, (byte) 0x32, (byte) 0x80,
                (byte) 0x0C, (byte) 0x7B, (byte) 0x0A, (byte) 0x1C, (byte) 0xF1, (byte) 0x12, (byte) 0x80, (byte) 0x2D,
                (byte) 0xA1, (byte) 0x84, (byte) 0x74, (byte) 0x99, (byte) 0xFE, (byte) 0x0C, (byte) 0x83, (byte) 0x09,
                (byte) 0xC4, (byte) 0x0D, (byte) 0x4C, (byte) 0x3D, (byte) 0x28, (byte) 0xE8, (byte) 0x5A, (byte) 0x53,
                (byte) 0x4F, (byte) 0xD6, (byte) 0xB4, (byte) 0xE3, (byte) 0x14, (byte) 0x20, (byte) 0xB6, (byte) 0x3C,
                (byte) 0x88, (byte) 0x20, (byte) 0x9E, (byte) 0x47, (byte) 0x7B, (byte) 0x5F, (byte) 0xCB, (byte) 0x87,
                (byte) 0xD4, (byte) 0x7B, (byte) 0x88, (byte) 0x57, (byte) 0x42, (byte) 0xD1, (byte) 0xE5, (byte) 0x7E,
                (byte) 0xCB, (byte) 0xA8, (byte) 0xF9, (byte) 0xAD, (byte) 0x9D, (byte) 0x61, (byte) 0x9F, (byte) 0xAC,
                (byte) 0xFA, (byte) 0xB0, (byte) 0x58, (byte) 0x41, (byte) 0x49, (byte) 0x88, (byte) 0xF8, (byte) 0xCA,
                (byte) 0xDB, (byte) 0x4F, (byte) 0x4B, (byte) 0xC5, (byte) 0xDA, (byte) 0xDB, (byte) 0x7A, (byte) 0xD9,
                (byte) 0x4B, (byte) 0x63, (byte) 0x35, (byte) 0x24, (byte) 0x50, (byte) 0xAE, (byte) 0xF3, (byte) 0x4B,
                (byte) 0x64, (byte) 0x4B, (byte) 0x1D, (byte) 0xA3, (byte) 0xE0, (byte) 0xEF, (byte) 0xFD, (byte) 0x13,
                (byte) 0x8F, (byte) 0x3D, (byte) 0xC9, (byte) 0xFB, (byte) 0xEC, (byte) 0x15, (byte) 0x1A, (byte) 0x1F,
                (byte) 0xFB, (byte) 0xB0, (byte) 0xBF, (byte) 0xCE, (byte) 0x53, (byte) 0x91, (byte) 0x10, (byte) 0x9E,
                (byte) 0xC9, (byte) 0xD7, (byte) 0x3A, (byte) 0x6C, (byte) 0xA1, (byte) 0x87, (byte) 0x5B, (byte) 0x1A,
                (byte) 0x4B, (byte) 0xAE, (byte) 0xD1, (byte) 0x16, (byte) 0x1B, (byte) 0x00, (byte) 0x33, (byte) 0x9D,
                (byte) 0x61, (byte) 0xDA, (byte) 0x50, (byte) 0x08, (byte) 0xC9, (byte) 0xAB, (byte) 0xF3, (byte) 0xFC,
                (byte) 0x17, (byte) 0xE2, (byte) 0xC5, (byte) 0xEF, (byte) 0x84, (byte) 0x95, (byte) 0x2C, (byte) 0x20,
                (byte) 0x2B, (byte) 0xEB, (byte) 0xFA, (byte) 0x53, (byte) 0xE4, (byte) 0x6E, (byte) 0x33, (byte) 0xBB,
                (byte) 0x2B, (byte) 0xE4, (byte) 0xEE, (byte) 0xC1, (byte) 0x83, (byte) 0xF3, (byte) 0xC3, (byte) 0x93,
                (byte) 0x6C, (byte) 0xCB, (byte) 0x92, (byte) 0xA4, (byte) 0x5C, (byte) 0x09, (byte) 0x67, (byte) 0x54,
                (byte) 0xA1, (byte) 0x26, (byte) 0x77, (byte) 0x8B, (byte) 0xE3, (byte) 0x8C, (byte) 0xC2, (byte) 0x77,
                (byte) 0xE7, (byte) 0x98, (byte) 0xFF, (byte) 0xC0, (byte) 0xBF, (byte) 0xD6, (byte) 0xB0, (byte) 0x0E,
                (byte) 0xB2, (byte) 0x67, (byte) 0xD4, (byte) 0x74, (byte) 0x5B, (byte) 0xF5, (byte) 0x6C, (byte) 0x4F,
                (byte) 0xE6, (byte) 0x64, (byte) 0x09, (byte) 0x01, (byte) 0x96, (byte) 0xC4, (byte) 0xB3, (byte) 0x67,
                (byte) 0x90, (byte) 0x48, (byte) 0xF8, (byte) 0x06, (byte) 0x18, (byte) 0x6F, (byte) 0x1B, (byte) 0x4E,
                (byte) 0x0D, (byte) 0x01, (byte) 0xF9, (byte) 0x4F, (byte) 0xBD, (byte) 0xBE, (byte) 0x98, (byte) 0xCB,
                (byte) 0xB0, (byte) 0xA0, (byte) 0x90, (byte) 0x43, (byte) 0x46, (byte) 0x42, (byte) 0x26, (byte) 0xEC,
                (byte) 0x4C, (byte) 0x11, (byte) 0xF4, (byte) 0xD4, (byte) 0x55, (byte) 0xDB, (byte) 0x89, (byte) 0xCB,
                (byte) 0xDC, (byte) 0x46, (byte) 0x19, (byte) 0xDB, (byte) 0xB3, (byte) 0xA8, (byte) 0x33, (byte) 0x9C,
                (byte) 0xB4, (byte) 0x61, (byte) 0xB3, (byte) 0x38, (byte) 0x07, (byte) 0x87, (byte) 0x5C, (byte) 0x30,
                (byte) 0x2F, (byte) 0x59, (byte) 0x31, (byte) 0x1F, (byte) 0xD1, (byte) 0xFE, (byte) 0xEF, (byte) 0x95,
                (byte) 0x5A, (byte) 0x79, (byte) 0x19, (byte) 0x85, (byte) 0x02, (byte) 0x03, (byte) 0x01, (byte) 0x00,
                (byte) 0x01, (byte) 0x02, (byte) 0x82, (byte) 0x01, (byte) 0x00, (byte) 0x1F, (byte) 0xC6, (byte) 0x66,
                (byte) 0x09, (byte) 0xA9, (byte) 0x3D, (byte) 0xD5, (byte) 0x38, (byte) 0x41, (byte) 0x09, (byte) 0xF1,
                (byte) 0x2A, (byte) 0x10, (byte) 0x2F, (byte) 0xB0, (byte) 0xEB, (byte) 0xCF, (byte) 0x32, (byte) 0xFB,
                (byte) 0x6F, (byte) 0x65, (byte) 0xFA, (byte) 0xBB, (byte) 0x56, (byte) 0xB0, (byte) 0xC1, (byte) 0x47,
                (byte) 0x37, (byte) 0x3F, (byte) 0x57, (byte) 0x8D, (byte) 0x9C, (byte) 0x1D, (byte) 0xDF, (byte) 0x77,
                (byte) 0xFB, (byte) 0x73, (byte) 0x30, (byte) 0xAB, (byte) 0x3F, (byte) 0xE7, (byte) 0x8F, (byte) 0xC5,
                (byte) 0x95, (byte) 0x4C, (byte) 0xAE, (byte) 0x7B, (byte) 0xC6, (byte) 0x6C, (byte) 0xE9, (byte) 0x3D,
                (byte) 0x7D, (byte) 0x71, (byte) 0x0D, (byte) 0x5A, (byte) 0xE2, (byte) 0xEC, (byte) 0x5F, (byte) 0xE1,
                (byte) 0x82, (byte) 0xA8, (byte) 0x8E, (byte) 0x81, (byte) 0x56, (byte) 0x75, (byte) 0x64, (byte) 0x48,
                (byte) 0x8B, (byte) 0xAA, (byte) 0xEE, (byte) 0x48, (byte) 0x10, (byte) 0x21, (byte) 0xDE, (byte) 0x4E,
                (byte) 0x4A, (byte) 0x32, (byte) 0x1C, (byte) 0x27, (byte) 0x94, (byte) 0x50, (byte) 0xC8, (byte) 0x50,
                (byte) 0x38, (byte) 0xFC, (byte) 0x48, (byte) 0x55, (byte) 0x26, (byte) 0x6A, (byte) 0xC7, (byte) 0xEB,
                (byte) 0xDD, (byte) 0x60, (byte) 0xFB, (byte) 0x5F, (byte) 0x13, (byte) 0x42, (byte) 0x19, (byte) 0xD4,
                (byte) 0x0A, (byte) 0xA1, (byte) 0x38, (byte) 0x16, (byte) 0x70, (byte) 0x14, (byte) 0xAB, (byte) 0xC6,
                (byte) 0xA1, (byte) 0xCC, (byte) 0x86, (byte) 0x99, (byte) 0x76, (byte) 0xA9, (byte) 0x24, (byte) 0x81,
                (byte) 0xD1, (byte) 0x62, (byte) 0xDE, (byte) 0xBE, (byte) 0x42, (byte) 0x17, (byte) 0x81, (byte) 0x54,
                (byte) 0xDA, (byte) 0x67, (byte) 0xFB, (byte) 0xD6, (byte) 0x92, (byte) 0xD6, (byte) 0x01, (byte) 0xA8,
                (byte) 0x9C, (byte) 0xE7, (byte) 0xE5, (byte) 0xA8, (byte) 0xC3, (byte) 0x81, (byte) 0x27, (byte) 0x69,
                (byte) 0x52, (byte) 0xA9, (byte) 0xD0, (byte) 0xAA, (byte) 0xE4, (byte) 0xF4, (byte) 0xC9, (byte) 0x67,
                (byte) 0xA2, (byte) 0x9E, (byte) 0x68, (byte) 0x72, (byte) 0x39, (byte) 0x1B, (byte) 0x77, (byte) 0xFE,
                (byte) 0x56, (byte) 0x3D, (byte) 0x51, (byte) 0x80, (byte) 0x62, (byte) 0x22, (byte) 0x0D, (byte) 0x9E,
                (byte) 0xD5, (byte) 0x83, (byte) 0xB7, (byte) 0x90, (byte) 0x23, (byte) 0xCE, (byte) 0xA0, (byte) 0xF6,
                (byte) 0xC9, (byte) 0x46, (byte) 0x1E, (byte) 0xF8, (byte) 0xAF, (byte) 0x76, (byte) 0x63, (byte) 0x7F,
                (byte) 0x39, (byte) 0x1B, (byte) 0xB0, (byte) 0x1C, (byte) 0x83, (byte) 0xCB, (byte) 0x89, (byte) 0x87,
                (byte) 0x1D, (byte) 0x23, (byte) 0x84, (byte) 0xAC, (byte) 0x69, (byte) 0xC7, (byte) 0x90, (byte) 0x61,
                (byte) 0xB6, (byte) 0x5B, (byte) 0x1F, (byte) 0x20, (byte) 0x77, (byte) 0x38, (byte) 0x2B, (byte) 0xE9,
                (byte) 0x3C, (byte) 0x42, (byte) 0x0B, (byte) 0x26, (byte) 0x2C, (byte) 0x2F, (byte) 0xD7, (byte) 0xE7,
                (byte) 0x43, (byte) 0x9F, (byte) 0x80, (byte) 0xD6, (byte) 0x95, (byte) 0x6B, (byte) 0xDA, (byte) 0xD7,
                (byte) 0xF0, (byte) 0x87, (byte) 0xFF, (byte) 0x62, (byte) 0x09, (byte) 0x68, (byte) 0x36, (byte) 0xF9,
                (byte) 0x53, (byte) 0x90, (byte) 0x95, (byte) 0xEE, (byte) 0x6A, (byte) 0xDF, (byte) 0xF7, (byte) 0x04,
                (byte) 0x3E, (byte) 0xA3, (byte) 0x89, (byte) 0xD9, (byte) 0xC5, (byte) 0x81, (byte) 0xBE, (byte) 0x96,
                (byte) 0x80, (byte) 0xFF, (byte) 0xD0, (byte) 0x1F, (byte) 0x86, (byte) 0x75, (byte) 0xF8, (byte) 0x3D,
                (byte) 0x67, (byte) 0xB0, (byte) 0x43, (byte) 0xBB, (byte) 0x35, (byte) 0x02, (byte) 0x81, (byte) 0x81,
                (byte) 0x00, (byte) 0xF6, (byte) 0x3C, (byte) 0x8D, (byte) 0xAD, (byte) 0x48, (byte) 0x05, (byte) 0x93,
                (byte) 0xB8, (byte) 0xEC, (byte) 0x7B, (byte) 0x3F, (byte) 0xD8, (byte) 0xB4, (byte) 0x58, (byte) 0x60,
                (byte) 0x34, (byte) 0x55, (byte) 0xE2, (byte) 0xB7, (byte) 0x6A, (byte) 0x3D, (byte) 0x16, (byte) 0x69,
                (byte) 0x2F, (byte) 0xC9, (byte) 0xC7, (byte) 0x04, (byte) 0xE7, (byte) 0x2E, (byte) 0x8B, (byte) 0xE8,
                (byte) 0xCD, (byte) 0xF9, (byte) 0x28, (byte) 0x7C, (byte) 0x8A, (byte) 0x3E, (byte) 0x30, (byte) 0x03,
                (byte) 0x80, (byte) 0xC7, (byte) 0xC9, (byte) 0x6C, (byte) 0x57, (byte) 0x0A, (byte) 0x2F, (byte) 0xB8,
                (byte) 0x78, (byte) 0x1D, (byte) 0xAA, (byte) 0x8F, (byte) 0xD3, (byte) 0xAC, (byte) 0xB2, (byte) 0x9F,
                (byte) 0x7D, (byte) 0xEE, (byte) 0x34, (byte) 0xD6, (byte) 0x4D, (byte) 0x27, (byte) 0x79, (byte) 0xD6,
                (byte) 0x72, (byte) 0xC8, (byte) 0x82, (byte) 0x9A, (byte) 0xF0, (byte) 0xED, (byte) 0x5C, (byte) 0x57,
                (byte) 0x36, (byte) 0x32, (byte) 0x6C, (byte) 0x8C, (byte) 0x18, (byte) 0xD2, (byte) 0xA9, (byte) 0x2D,
                (byte) 0x39, (byte) 0x94, (byte) 0x83, (byte) 0x24, (byte) 0x43, (byte) 0x78, (byte) 0x86, (byte) 0x53,
                (byte) 0x12, (byte) 0x02, (byte) 0xB3, (byte) 0x64, (byte) 0x0D, (byte) 0xBD, (byte) 0xBD, (byte) 0x9C,
                (byte) 0x59, (byte) 0xDA, (byte) 0x88, (byte) 0x07, (byte) 0x3E, (byte) 0x30, (byte) 0xD7, (byte) 0x31,
                (byte) 0x6A, (byte) 0x76, (byte) 0xE9, (byte) 0x91, (byte) 0x69, (byte) 0xA1, (byte) 0xA0, (byte) 0x96,
                (byte) 0x33, (byte) 0x40, (byte) 0x5D, (byte) 0x1A, (byte) 0x7F, (byte) 0x23, (byte) 0x02, (byte) 0x9C,
                (byte) 0xF4, (byte) 0xEC, (byte) 0x63, (byte) 0x40, (byte) 0x4E, (byte) 0xF9, (byte) 0xE3, (byte) 0x54,
                (byte) 0xAF, (byte) 0x02, (byte) 0x81, (byte) 0x81, (byte) 0x00, (byte) 0xE7, (byte) 0xB9, (byte) 0xA3,
                (byte) 0x1E, (byte) 0xEE, (byte) 0x0B, (byte) 0x27, (byte) 0xD2, (byte) 0xF8, (byte) 0x24, (byte) 0x28,
                (byte) 0x56, (byte) 0x9D, (byte) 0x99, (byte) 0x56, (byte) 0x2E, (byte) 0x5C, (byte) 0x95, (byte) 0x9C,
                (byte) 0xB3, (byte) 0xD8, (byte) 0xAA, (byte) 0x64, (byte) 0x19, (byte) 0x33, (byte) 0xC6, (byte) 0x29,
                (byte) 0xF0, (byte) 0x2D, (byte) 0x7C, (byte) 0xA3, (byte) 0x14, (byte) 0x61, (byte) 0x83, (byte) 0xEC,
                (byte) 0x8A, (byte) 0x5D, (byte) 0x57, (byte) 0xF2, (byte) 0x75, (byte) 0x2E, (byte) 0x51, (byte) 0xB9,
                (byte) 0x4F, (byte) 0x80, (byte) 0xA7, (byte) 0xA5, (byte) 0x87, (byte) 0x4B, (byte) 0xB2, (byte) 0xCE,
                (byte) 0xF8, (byte) 0xEF, (byte) 0x7A, (byte) 0xF2, (byte) 0xD2, (byte) 0x34, (byte) 0x49, (byte) 0x05,
                (byte) 0x66, (byte) 0xD2, (byte) 0x43, (byte) 0xB7, (byte) 0x3E, (byte) 0x9B, (byte) 0x20, (byte) 0x00,
                (byte) 0x8C, (byte) 0xDC, (byte) 0xCE, (byte) 0xC5, (byte) 0xF5, (byte) 0x10, (byte) 0x25, (byte) 0xDD,
                (byte) 0xFA, (byte) 0x95, (byte) 0x78, (byte) 0x00, (byte) 0x07, (byte) 0x33, (byte) 0x3B, (byte) 0x18,
                (byte) 0x97, (byte) 0x0C, (byte) 0x8D, (byte) 0x0B, (byte) 0x20, (byte) 0x0B, (byte) 0x68, (byte) 0x4C,
                (byte) 0x06, (byte) 0x42, (byte) 0xB5, (byte) 0x11, (byte) 0x3E, (byte) 0x62, (byte) 0xDF, (byte) 0xF2,
                (byte) 0xD3, (byte) 0x1A, (byte) 0xAA, (byte) 0x17, (byte) 0xA1, (byte) 0x7A, (byte) 0x81, (byte) 0xF6,
                (byte) 0xBD, (byte) 0x11, (byte) 0x0A, (byte) 0xC8, (byte) 0xB2, (byte) 0xCF, (byte) 0x53, (byte) 0xE5,
                (byte) 0xE9, (byte) 0x89, (byte) 0xE8, (byte) 0xF0, (byte) 0xFB, (byte) 0xC1, (byte) 0x48, (byte) 0x9F,
                (byte) 0xD9, (byte) 0xBE, (byte) 0x9C, (byte) 0x6A, (byte) 0x0B, (byte) 0x02, (byte) 0x81, (byte) 0x80,
                (byte) 0x7F, (byte) 0x23, (byte) 0xEF, (byte) 0x07, (byte) 0x6C, (byte) 0xEB, (byte) 0xAC, (byte) 0x45,
                (byte) 0xCC, (byte) 0x9D, (byte) 0x2A, (byte) 0xEF, (byte) 0x4A, (byte) 0xD7, (byte) 0x62, (byte) 0xFD,
                (byte) 0x42, (byte) 0xE7, (byte) 0x68, (byte) 0x4B, (byte) 0x0B, (byte) 0xB5, (byte) 0xB9, (byte) 0xA4,
                (byte) 0x3A, (byte) 0x2C, (byte) 0x6F, (byte) 0xE4, (byte) 0xAA, (byte) 0x8D, (byte) 0x05, (byte) 0x2C,
                (byte) 0xED, (byte) 0x1C, (byte) 0xB1, (byte) 0x89, (byte) 0x73, (byte) 0xE8, (byte) 0xB8, (byte) 0xB6,
                (byte) 0x52, (byte) 0xF2, (byte) 0x36, (byte) 0xE5, (byte) 0x8E, (byte) 0x55, (byte) 0xEB, (byte) 0x8A,
                (byte) 0xE1, (byte) 0xCE, (byte) 0x82, (byte) 0xAB, (byte) 0x0A, (byte) 0x3D, (byte) 0xC4, (byte) 0xCA,
                (byte) 0x72, (byte) 0x27, (byte) 0x88, (byte) 0x0B, (byte) 0x98, (byte) 0x79, (byte) 0x79, (byte) 0x78,
                (byte) 0x14, (byte) 0xFE, (byte) 0x12, (byte) 0xE0, (byte) 0x99, (byte) 0xBE, (byte) 0x97, (byte) 0x2D,
                (byte) 0x5D, (byte) 0xC5, (byte) 0xDC, (byte) 0x64, (byte) 0xA8, (byte) 0xC9, (byte) 0x3F, (byte) 0x97,
                (byte) 0xBE, (byte) 0x8E, (byte) 0x24, (byte) 0x38, (byte) 0x4E, (byte) 0x50, (byte) 0x15, (byte) 0x2E,
                (byte) 0x74, (byte) 0x84, (byte) 0x8A, (byte) 0x01, (byte) 0xEA, (byte) 0x59, (byte) 0xB0, (byte) 0x14,
                (byte) 0x59, (byte) 0x18, (byte) 0xE9, (byte) 0x9C, (byte) 0x7B, (byte) 0xFD, (byte) 0x70, (byte) 0xFF,
                (byte) 0x86, (byte) 0xDC, (byte) 0xBC, (byte) 0xFF, (byte) 0x6C, (byte) 0xBC, (byte) 0x0D, (byte) 0x31,
                (byte) 0xF3, (byte) 0x66, (byte) 0xDF, (byte) 0x5A, (byte) 0x8C, (byte) 0x0A, (byte) 0x3A, (byte) 0x5E,
                (byte) 0xE0, (byte) 0x7B, (byte) 0xB2, (byte) 0xE5, (byte) 0xD4, (byte) 0x73, (byte) 0x43, (byte) 0xF3,
                (byte) 0x02, (byte) 0x81, (byte) 0x81, (byte) 0x00, (byte) 0xC1, (byte) 0xB4, (byte) 0x6C, (byte) 0xA7,
                (byte) 0x6E, (byte) 0x55, (byte) 0xE4, (byte) 0xF4, (byte) 0x76, (byte) 0x79, (byte) 0xC3, (byte) 0x3E,
                (byte) 0xA4, (byte) 0x7F, (byte) 0x89, (byte) 0x8B, (byte) 0x37, (byte) 0xD9, (byte) 0xD9, (byte) 0x24,
                (byte) 0x7D, (byte) 0xF9, (byte) 0xF1, (byte) 0xB3, (byte) 0x1F, (byte) 0x94, (byte) 0x87, (byte) 0x7A,
                (byte) 0x3E, (byte) 0x8B, (byte) 0xF1, (byte) 0xC7, (byte) 0x17, (byte) 0xBD, (byte) 0x2F, (byte) 0xFE,
                (byte) 0x7A, (byte) 0x09, (byte) 0x0A, (byte) 0xA4, (byte) 0xEA, (byte) 0x13, (byte) 0x66, (byte) 0xD5,
                (byte) 0x0E, (byte) 0xB7, (byte) 0x15, (byte) 0xA8, (byte) 0x03, (byte) 0x9F, (byte) 0x75, (byte) 0x64,
                (byte) 0xA2, (byte) 0xCC, (byte) 0x24, (byte) 0x2F, (byte) 0x93, (byte) 0x85, (byte) 0x76, (byte) 0xFE,
                (byte) 0x7C, (byte) 0xC6, (byte) 0x1E, (byte) 0x68, (byte) 0x37, (byte) 0x44, (byte) 0x89, (byte) 0x31,
                (byte) 0x37, (byte) 0x63, (byte) 0xA2, (byte) 0x17, (byte) 0x39, (byte) 0x68, (byte) 0x6E, (byte) 0x27,
                (byte) 0x0A, (byte) 0xCB, (byte) 0x45, (byte) 0x3A, (byte) 0xBF, (byte) 0x98, (byte) 0xA6, (byte) 0xF5,
                (byte) 0x9D, (byte) 0x88, (byte) 0x49, (byte) 0xC4, (byte) 0x7F, (byte) 0xC4, (byte) 0xAF, (byte) 0xC8,
                (byte) 0x8B, (byte) 0xFA, (byte) 0xD0, (byte) 0x6F, (byte) 0x56, (byte) 0x37, (byte) 0xDE, (byte) 0xC4,
                (byte) 0x99, (byte) 0x85, (byte) 0x96, (byte) 0x3B, (byte) 0x66, (byte) 0x2D, (byte) 0x3E, (byte) 0x14,
                (byte) 0xCE, (byte) 0x2A, (byte) 0x35, (byte) 0x9B, (byte) 0x43, (byte) 0xB4, (byte) 0xDE, (byte) 0x7C,
                (byte) 0x5A, (byte) 0xCC, (byte) 0x5E, (byte) 0xA6, (byte) 0x14, (byte) 0xEC, (byte) 0xA0, (byte) 0xB3,
                (byte) 0x64, (byte) 0xA3, (byte) 0x5C, (byte) 0x01, (byte) 0x02, (byte) 0x81, (byte) 0x80, (byte) 0x02,
                (byte) 0xE1, (byte) 0x20, (byte) 0x38, (byte) 0x52, (byte) 0xE2, (byte) 0x10, (byte) 0x16, (byte) 0xB6,
                (byte) 0x4E, (byte) 0x8F, (byte) 0x91, (byte) 0x03, (byte) 0xAA, (byte) 0x98, (byte) 0x42, (byte) 0x34,
                (byte) 0x0A, (byte) 0x56, (byte) 0x44, (byte) 0x06, (byte) 0x4A, (byte) 0x4F, (byte) 0x14, (byte) 0xD7,
                (byte) 0xAD, (byte) 0x44, (byte) 0x45, (byte) 0xFB, (byte) 0x2A, (byte) 0xB0, (byte) 0x45, (byte) 0x76,
                (byte) 0x2D, (byte) 0x59, (byte) 0xCC, (byte) 0x04, (byte) 0x33, (byte) 0xA9, (byte) 0x7C, (byte) 0xB2,
                (byte) 0x91, (byte) 0xDB, (byte) 0x85, (byte) 0x9E, (byte) 0x72, (byte) 0x0E, (byte) 0x6D, (byte) 0x1C,
                (byte) 0x84, (byte) 0xD0, (byte) 0x10, (byte) 0x0F, (byte) 0x6E, (byte) 0x14, (byte) 0x0A, (byte) 0xB5,
                (byte) 0xEB, (byte) 0x3B, (byte) 0x54, (byte) 0xA5, (byte) 0x7F, (byte) 0x4A, (byte) 0x91, (byte) 0x08,
                (byte) 0x21, (byte) 0x7D, (byte) 0xE1, (byte) 0xEC, (byte) 0xE6, (byte) 0x2B, (byte) 0x8D, (byte) 0x44,
                (byte) 0x81, (byte) 0x48, (byte) 0x8B, (byte) 0x45, (byte) 0x05, (byte) 0x4A, (byte) 0x3D, (byte) 0x73,
                (byte) 0xEA, (byte) 0xFA, (byte) 0xE9, (byte) 0xB4, (byte) 0xE1, (byte) 0xA5, (byte) 0x97, (byte) 0xDD,
                (byte) 0xE0, (byte) 0x0E, (byte) 0x32, (byte) 0xC3, (byte) 0x89, (byte) 0xA1, (byte) 0x8A, (byte) 0x3D,
                (byte) 0x1A, (byte) 0xC2, (byte) 0x63, (byte) 0x98, (byte) 0x60, (byte) 0xD5, (byte) 0xF5, (byte) 0xD1,
                (byte) 0x40, (byte) 0x51, (byte) 0x68, (byte) 0xE8, (byte) 0x2F, (byte) 0x2A, (byte) 0xDE, (byte) 0x84,
                (byte) 0x00, (byte) 0x5D, (byte) 0xDB, (byte) 0x48, (byte) 0x00, (byte) 0x75, (byte) 0x86, (byte) 0xAC,
                (byte) 0x3C, (byte) 0x4A, (byte) 0xA2, (byte) 0x89, (byte) 0x52, (byte) 0x08, (byte) 0x4F
        };

        public static final byte[] ASYM_CERT = {
                0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46,
                0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44,
                0x7a, 0x54, 0x43, 0x43, 0x41, 0x72, 0x57, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41, 0x67, 0x49, 0x48, 0x57,
                0x52, 0x79, 0x55, 0x41, 0x42, 0x4d, 0x54, 0x67, 0x44, 0x41, 0x4e, 0x42, 0x67, 0x6b, 0x71,
                0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x73, 0x46, 0x41, 0x44, 0x43, 0x42, 0x6a,
                0x54, 0x45, 0x4c, 0x4d, 0x41, 0x6b, 0x47, 0x41, 0x31, 0x55, 0x45, 0x0a, 0x42, 0x68, 0x4d,
                0x43, 0x51, 0x30, 0x34, 0x78, 0x44, 0x7a, 0x41, 0x4e, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x67, 0x4d,
                0x42, 0x6b, 0x5a, 0x31, 0x53, 0x6d, 0x6c, 0x68, 0x62, 0x6a, 0x45, 0x50, 0x4d, 0x41, 0x30,
                0x47, 0x41, 0x31, 0x55, 0x45, 0x42, 0x77, 0x77, 0x47, 0x52, 0x6e, 0x56, 0x61, 0x61, 0x47, 0x39, 0x31,
                0x4d, 0x53, 0x73, 0x77, 0x4b, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x4b, 0x0a, 0x44, 0x43,
                0x4a, 0x4f, 0x5a, 0x58, 0x64, 0x73, 0x59, 0x57, 0x35, 0x6b, 0x49, 0x46, 0x42, 0x68, 0x65, 0x57, 0x31,
                0x6c, 0x62, 0x6e, 0x51, 0x67, 0x56, 0x47, 0x56, 0x6a, 0x61, 0x47, 0x35, 0x76, 0x62, 0x47,
                0x39, 0x6e, 0x65, 0x53, 0x42, 0x44, 0x62, 0x79, 0x34, 0x73, 0x54, 0x48, 0x52, 0x6b, 0x4d, 0x52, 0x63,
                0x77, 0x46, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x4c, 0x45, 0x77, 0x35, 0x45, 0x0a, 0x52,
                0x56, 0x59, 0x67, 0x54, 0x6c, 0x42, 0x55, 0x49, 0x45, 0x31, 0x47, 0x52, 0x79, 0x42, 0x44, 0x51, 0x54,
                0x45, 0x57, 0x4d, 0x42, 0x51, 0x47, 0x41, 0x31, 0x55, 0x45, 0x41, 0x77, 0x77, 0x4e, 0x52,
                0x45, 0x56, 0x57, 0x49, 0x45, 0x31, 0x47, 0x52, 0x79, 0x42, 0x54, 0x64, 0x57, 0x4a, 0x44, 0x51, 0x54,
                0x41, 0x65, 0x46, 0x77, 0x30, 0x79, 0x4d, 0x54, 0x41, 0x33, 0x4d, 0x44, 0x63, 0x77, 0x0a,
                0x4d, 0x44, 0x41, 0x77, 0x4d, 0x44, 0x42, 0x61, 0x46, 0x77, 0x30, 0x7a, 0x4d, 0x54, 0x41, 0x33, 0x4d,
                0x44, 0x67, 0x77, 0x4d, 0x44, 0x41, 0x77, 0x4d, 0x44, 0x42, 0x61, 0x4d, 0x49, 0x47, 0x52,
                0x4d, 0x51, 0x73, 0x77, 0x43, 0x51, 0x59, 0x44, 0x56, 0x51, 0x51, 0x47, 0x45, 0x77, 0x4a, 0x44, 0x54,
                0x6a, 0x45, 0x50, 0x4d, 0x41, 0x30, 0x47, 0x41, 0x31, 0x55, 0x45, 0x43, 0x41, 0x77, 0x47,
                0x0a, 0x52, 0x6e, 0x56, 0x71, 0x61, 0x57, 0x46, 0x75, 0x4d, 0x51, 0x38, 0x77, 0x44, 0x51, 0x59, 0x44,
                0x56, 0x51, 0x51, 0x48, 0x44, 0x41, 0x5a, 0x47, 0x64, 0x58, 0x70, 0x6f, 0x62, 0x33, 0x55,
                0x78, 0x4b, 0x7a, 0x41, 0x70, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x6f, 0x4d, 0x49, 0x6b, 0x35, 0x6c,
                0x64, 0x32, 0x78, 0x68, 0x62, 0x6d, 0x52, 0x66, 0x55, 0x47, 0x46, 0x35, 0x62, 0x57, 0x56,
                0x75, 0x0a, 0x64, 0x46, 0x39, 0x55, 0x5a, 0x57, 0x4e, 0x6f, 0x62, 0x6d, 0x39, 0x73, 0x62, 0x32, 0x64,
                0x35, 0x58, 0x30, 0x4e, 0x76, 0x4c, 0x69, 0x78, 0x4d, 0x64, 0x47, 0x51, 0x78, 0x46, 0x7a,
                0x41, 0x56, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x73, 0x4d, 0x44, 0x6b, 0x35, 0x51, 0x56, 0x43, 0x35,
                0x55, 0x5a, 0x57, 0x4e, 0x6f, 0x51, 0x32, 0x56, 0x75, 0x64, 0x47, 0x56, 0x79, 0x4d, 0x52,
                0x6f, 0x77, 0x0a, 0x47, 0x41, 0x59, 0x44, 0x56, 0x51, 0x51, 0x44, 0x44, 0x42, 0x46, 0x54, 0x54, 0x6a,
                0x41, 0x78, 0x4d, 0x6a, 0x4d, 0x30, 0x4e, 0x54, 0x59, 0x33, 0x4f, 0x44, 0x6b, 0x74, 0x51,
                0x56, 0x56, 0x55, 0x53, 0x44, 0x43, 0x43, 0x41, 0x53, 0x49, 0x77, 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f,
                0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41, 0x51, 0x45, 0x42, 0x42, 0x51, 0x41, 0x44, 0x67,
                0x67, 0x45, 0x50, 0x0a, 0x41, 0x44, 0x43, 0x43, 0x41, 0x51, 0x6f, 0x43, 0x67, 0x67, 0x45, 0x42, 0x41,
                0x4e, 0x37, 0x6a, 0x4d, 0x6f, 0x41, 0x4d, 0x65, 0x77, 0x6f, 0x63, 0x38, 0x52, 0x4b, 0x41,
                0x4c, 0x61, 0x47, 0x45, 0x64, 0x4a, 0x6e, 0x2b, 0x44, 0x49, 0x4d, 0x4a, 0x78, 0x41, 0x31, 0x4d, 0x50,
                0x53, 0x6a, 0x6f, 0x57, 0x6c, 0x4e, 0x50, 0x31, 0x72, 0x54, 0x6a, 0x46, 0x43, 0x43, 0x32,
                0x50, 0x49, 0x67, 0x67, 0x0a, 0x6e, 0x6b, 0x64, 0x37, 0x58, 0x38, 0x75, 0x48, 0x31, 0x48, 0x75, 0x49,
                0x56, 0x30, 0x4c, 0x52, 0x35, 0x58, 0x37, 0x4c, 0x71, 0x50, 0x6d, 0x74, 0x6e, 0x57, 0x47,
                0x66, 0x72, 0x50, 0x71, 0x77, 0x57, 0x45, 0x46, 0x4a, 0x69, 0x50, 0x6a, 0x4b, 0x32, 0x30, 0x39, 0x4c,
                0x78, 0x64, 0x72, 0x62, 0x65, 0x74, 0x6c, 0x4c, 0x59, 0x7a, 0x55, 0x6b, 0x55, 0x4b, 0x37,
                0x7a, 0x53, 0x32, 0x52, 0x4c, 0x0a, 0x48, 0x61, 0x50, 0x67, 0x37, 0x2f, 0x30, 0x54, 0x6a, 0x7a, 0x33,
                0x4a, 0x2b, 0x2b, 0x77, 0x56, 0x47, 0x68, 0x2f, 0x37, 0x73, 0x4c, 0x2f, 0x4f, 0x55, 0x35,
                0x45, 0x51, 0x6e, 0x73, 0x6e, 0x58, 0x4f, 0x6d, 0x79, 0x68, 0x68, 0x31, 0x73, 0x61, 0x53, 0x36, 0x37,
                0x52, 0x46, 0x68, 0x73, 0x41, 0x4d, 0x35, 0x31, 0x68, 0x32, 0x6c, 0x41, 0x49, 0x79, 0x61,
                0x76, 0x7a, 0x2f, 0x42, 0x66, 0x69, 0x0a, 0x78, 0x65, 0x2b, 0x45, 0x6c, 0x53, 0x77, 0x67, 0x4b, 0x2b,
                0x76, 0x36, 0x55, 0x2b, 0x52, 0x75, 0x4d, 0x37, 0x73, 0x72, 0x35, 0x4f, 0x37, 0x42, 0x67,
                0x2f, 0x50, 0x44, 0x6b, 0x32, 0x7a, 0x4c, 0x6b, 0x71, 0x52, 0x63, 0x43, 0x57, 0x64, 0x55, 0x6f, 0x53,
                0x5a, 0x33, 0x69, 0x2b, 0x4f, 0x4d, 0x77, 0x6e, 0x66, 0x6e, 0x6d, 0x50, 0x2f, 0x41, 0x76,
                0x39, 0x61, 0x77, 0x44, 0x72, 0x4a, 0x6e, 0x0a, 0x31, 0x48, 0x52, 0x62, 0x39, 0x57, 0x78, 0x50, 0x35,
                0x6d, 0x51, 0x4a, 0x41, 0x5a, 0x62, 0x45, 0x73, 0x32, 0x65, 0x51, 0x53, 0x50, 0x67, 0x47,
                0x47, 0x47, 0x38, 0x62, 0x54, 0x67, 0x30, 0x42, 0x2b, 0x55, 0x2b, 0x39, 0x76, 0x70, 0x6a, 0x4c, 0x73,
                0x4b, 0x43, 0x51, 0x51, 0x30, 0x5a, 0x43, 0x4a, 0x75, 0x78, 0x4d, 0x45, 0x66, 0x54, 0x55,
                0x56, 0x64, 0x75, 0x4a, 0x79, 0x39, 0x78, 0x47, 0x0a, 0x47, 0x64, 0x75, 0x7a, 0x71, 0x44, 0x4f, 0x63,
                0x74, 0x47, 0x47, 0x7a, 0x4f, 0x41, 0x65, 0x48, 0x58, 0x44, 0x41, 0x76, 0x57, 0x54, 0x45,
                0x66, 0x30, 0x66, 0x37, 0x76, 0x6c, 0x56, 0x70, 0x35, 0x47, 0x59, 0x55, 0x43, 0x41, 0x77, 0x45, 0x41,
                0x41, 0x61, 0x4d, 0x73, 0x4d, 0x43, 0x6f, 0x77, 0x48, 0x51, 0x59, 0x44, 0x56, 0x52, 0x30,
                0x4f, 0x42, 0x42, 0x59, 0x45, 0x46, 0x48, 0x46, 0x64, 0x0a, 0x78, 0x7a, 0x51, 0x6e, 0x73, 0x78, 0x51,
                0x6b, 0x4d, 0x6d, 0x68, 0x64, 0x37, 0x6a, 0x7a, 0x44, 0x30, 0x44, 0x4a, 0x65, 0x68, 0x4a,
                0x70, 0x44, 0x4d, 0x41, 0x6b, 0x47, 0x41, 0x31, 0x55, 0x64, 0x45, 0x77, 0x51, 0x43, 0x4d, 0x41, 0x41,
                0x77, 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41, 0x51,
                0x45, 0x4c, 0x42, 0x51, 0x41, 0x44, 0x67, 0x67, 0x45, 0x42, 0x0a, 0x41, 0x43, 0x70, 0x58, 0x6e, 0x57,
                0x4e, 0x75, 0x71, 0x37, 0x79, 0x35, 0x70, 0x5a, 0x58, 0x77, 0x77, 0x4c, 0x63, 0x2f, 0x71,
                0x52, 0x66, 0x36, 0x72, 0x73, 0x4c, 0x79, 0x4e, 0x75, 0x39, 0x71, 0x55, 0x4b, 0x65, 0x43, 0x75, 0x56,
                0x69, 0x54, 0x71, 0x32, 0x41, 0x52, 0x52, 0x43, 0x7a, 0x72, 0x67, 0x73, 0x32, 0x5a, 0x77,
                0x4b, 0x58, 0x43, 0x59, 0x46, 0x43, 0x32, 0x54, 0x4b, 0x4e, 0x68, 0x0a, 0x66, 0x74, 0x4a, 0x66, 0x31,
                0x76, 0x66, 0x77, 0x45, 0x43, 0x72, 0x4b, 0x4b, 0x4f, 0x47, 0x6f, 0x64, 0x64, 0x6f, 0x36,
                0x72, 0x46, 0x6c, 0x6e, 0x63, 0x62, 0x50, 0x73, 0x59, 0x67, 0x61, 0x32, 0x79, 0x66, 0x69, 0x61, 0x71,
                0x76, 0x5a, 0x72, 0x47, 0x64, 0x6d, 0x48, 0x58, 0x66, 0x42, 0x70, 0x61, 0x73, 0x68, 0x49,
                0x49, 0x47, 0x74, 0x33, 0x4a, 0x6e, 0x54, 0x37, 0x53, 0x7a, 0x57, 0x4b, 0x0a, 0x73, 0x76, 0x30, 0x4f,
                0x49, 0x79, 0x77, 0x36, 0x42, 0x78, 0x6f, 0x67, 0x67, 0x77, 0x74, 0x71, 0x72, 0x4d, 0x61,
                0x48, 0x52, 0x74, 0x62, 0x69, 0x69, 0x79, 0x5a, 0x67, 0x57, 0x4c, 0x38, 0x38, 0x31, 0x37, 0x53, 0x2f,
                0x71, 0x63, 0x52, 0x48, 0x44, 0x65, 0x42, 0x51, 0x42, 0x50, 0x6b, 0x70, 0x63, 0x75, 0x53,
                0x36, 0x53, 0x57, 0x2b, 0x71, 0x4f, 0x58, 0x31, 0x48, 0x63, 0x73, 0x6e, 0x4c, 0x0a, 0x42, 0x37, 0x45,
                0x65, 0x74, 0x36, 0x69, 0x50, 0x66, 0x51, 0x36, 0x59, 0x6d, 0x32, 0x2f, 0x70, 0x68, 0x6b,
                0x41, 0x43, 0x63, 0x6b, 0x31, 0x6c, 0x79, 0x53, 0x51, 0x30, 0x69, 0x66, 0x30, 0x37, 0x4d, 0x32, 0x53,
                0x58, 0x6a, 0x6e, 0x6e, 0x65, 0x2f, 0x74, 0x79, 0x62, 0x34, 0x72, 0x77, 0x48, 0x4b, 0x31,
                0x4e, 0x31, 0x73, 0x50, 0x79, 0x75, 0x32, 0x4b, 0x59, 0x71, 0x49, 0x4c, 0x73, 0x77, 0x0a, 0x4b, 0x6b,
                0x62, 0x71, 0x53, 0x43, 0x47, 0x67, 0x30, 0x33, 0x64, 0x73, 0x52, 0x56, 0x48, 0x37, 0x61,
                0x5a, 0x33, 0x47, 0x36, 0x4f, 0x38, 0x56, 0x66, 0x6a, 0x63, 0x6a, 0x5a, 0x65, 0x6d, 0x41, 0x67, 0x4c,
                0x50, 0x45, 0x76, 0x74, 0x44, 0x66, 0x47, 0x6a, 0x65, 0x4a, 0x63, 0x48, 0x34, 0x75, 0x56,
                0x6b, 0x61, 0x4e, 0x46, 0x4c, 0x74, 0x77, 0x4a, 0x6f, 0x4d, 0x71, 0x31, 0x32, 0x65, 0x4f, 0x0a, 0x6f,
                0x55, 0x55, 0x42, 0x77, 0x4d, 0x2f, 0x62, 0x5a, 0x37, 0x53, 0x70, 0x5a, 0x51, 0x54, 0x68,
                0x71, 0x2f, 0x39, 0x6e, 0x5a, 0x56, 0x51, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44,
                0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d,
                0x2d, 0x2d
        };

        private final KeyManager keyManager;
        private final Crypto crypto;

        public DemoRKLAPI(Context context, NSDKModuleManager moduleManager) {
            super(context, moduleManager);
            this.keyManager = (KeyManager) moduleManager.getModule(ModuleType.KEY_MANAGER);
            this.crypto = (Crypto) moduleManager.getModule(ModuleType.CRYPTO);
        }

        @Override
        public String getDeviceCert(int id) {
            return new String(ASYM_CERT);
        }

        public void installDefaultDeviceCertKey() throws RKLException {
            AsymmetricKey deviceCertKey = new AsymmetricKey();
            deviceCertKey.setKeyID((byte) CERT_PRIVATE_KEY_ID);
            deviceCertKey.setKeyUsage(AsymKeyUsage.AUTH);
            deviceCertKey.setKeyType(AsymKeyType.RSA);
            deviceCertKey.setKeyData(CERT_PRIVATE_KEY);
            deviceCertKey.setKeyLen(CERT_PRIVATE_KEY.length);
            try {
                keyManager.generateKey(KeyGenerateMethod.CLEAR, null, null, deviceCertKey, ASYM_CERT);
            } catch (NSDKException e) {
                e.printStackTrace();
                throw new RKLException("Failed to generate cert private key.", e);
            }
        }

        @Override
        public byte[] sign(int certId, byte[] hash) throws DeviceException {
            AsymmetricKey key = new AsymmetricKey();

            // Use default cert private key
            key.setKeyID((byte) CERT_PRIVATE_KEY_ID);
            key.setKeyType(AsymKeyType.RSA);
            key.setKeyUsage(AsymKeyUsage.AUTH);

            AsymAlgorithmParameters parameters = new AsymAlgorithmParameters();
            parameters.setMessageDigestType(MessageDigestType.SHA256);
            parameters.setEncodingMode(com.newland.nsdk.core.api.common.crypto.AsymEncodingMode.PKCS_V15);
            try {
                return crypto.signAsym(key, parameters, hash);
            } catch (NSDKException e) {
                e.printStackTrace();
                throw new DeviceException("Failed to sign data.", e);
            }
        }
    }

}
