package CardConnect;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;


public class AddonFunctions {

    public static final byte MYAPP_CLA = (byte)0x80;

    public static byte[] checkSW(byte[] stream){

        byte[] result;
        int error = 2;

        if((stream[16] & 0xFF) == 144 && (stream[17] & 0xFF) == 0){
            result = new byte[stream.length - 2];
            for(int i = 0; i < stream.length - 2; i++){
                result[i] = stream [i];
            }
            return result;
        }

        result = new byte[error];
        for(int i = 0; i < error; i++){
            result[i] = stream [i];
        }

        return result;
    }
    public static String bytesToDec(byte[] bytes) {

        // convert bytes to int
        int[] intValue = new int[bytes.length];
        int i = 0;
        for (byte b : bytes) {
            intValue[i] = (b & 0xFF);
            i++;
        }

        return Arrays.toString(intValue).replaceAll("\\[|\\]|\\,|", "");

    }
    public static byte[] decTobytes(String sequence){

        //String test = "02 0F 84 31 FF FF FF FF FF FF 1A 2B 3C 4D 5E 6F 90 00";
        String arraySequence[] = sequence.split(" ");
        byte[] byteSequence = new byte[arraySequence.length];

        for (int i = 0; i < arraySequence.length; i++){
            int parse = Integer.parseInt(arraySequence[i]);
            byteSequence[i] =  (byte) parse;
        }
         //System.out.println(byteSequence[2]+" "+byteSequence[5]+ " delka: "+ byteSequence.length);

        return byteSequence;

    }

    public static String genVerifierKey(String verifierID) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        int[] Master = {130, 202, 131, 48, 95,21, 189, 182, 17, 61, 171, 108, 75, 253, 78, 120};
        byte[] MasterByte = new byte[Master.length];
        byte[] verIDByte = decTobytes(verifierID);

        for (int i = 0; i < Master.length; i++){
            MasterByte[i] = (byte) Master[i];
        }

        String ECB = "AES/ECB/NoPadding";

        SecretKey k = new SecretKeySpec(MasterByte, 0, MasterByte.length, "AES");
        Cipher c = Cipher.getInstance(ECB);
        c.init(Cipher.ENCRYPT_MODE,k);

        byte[]verifierKey = c.doFinal(verIDByte);

        return bytesToDec(verifierKey);

    }

    public static String genUserKey(String userID, String verifierKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        byte[] usrIDByte = decTobytes(userID);
        byte[] verKeyByte = decTobytes(verifierKey);
        String ECB = "AES/ECB/NoPadding";

        SecretKey verK = new SecretKeySpec(verKeyByte, 0, verKeyByte.length, "AES");
        Cipher verC = Cipher.getInstance(ECB);
        verC.init(Cipher.ENCRYPT_MODE,verK);

        byte[]userKey = verC.doFinal(usrIDByte);

        return bytesToDec(userKey);
    }
    
    public static void main(String args[]) {

        byte[] aid = { (byte)0xf0, (byte)0x00, (byte)0x00, (byte)0x01};

        APDU apduSender = new APDU(aid, (byte)MYAPP_CLA);


    }
}
