package com.sunyard.chsm.sdf.adapter;

import com.sunyard.chsm.sdf.context.AlgId;
import com.sunyard.chsm.sdf.model.DeviceInfo;
import com.sunyard.chsm.sdf.model.EccCipher;
import com.sunyard.chsm.sdf.model.EccKey;
import com.sunyard.chsm.sdf.model.EccPubKey;
import com.sunyard.chsm.sdf.util.LangUtils;
import com.sunyard.chsm.utils.gm.BCECUtils;
import com.sunyard.chsm.utils.gm.BCSM2Utils;
import lombok.SneakyThrows;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.BigIntegers;

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.SecureRandom;

/**
 * @author liulu
 * @since 2024/11/5
 */
public class BcSdfApiAdaptor implements SdfApiAdapter {


    private static final DeviceInfo deviceInfo;

    static {
        deviceInfo = new DeviceInfo();
        deviceInfo.setIssuerName("BC");
        deviceInfo.setDeviceName("BC-3000");
        deviceInfo.setDeviceSerial("BC202411051037");
        deviceInfo.setDeviceVersion(1);
        deviceInfo.setStandardVersion(1);
        deviceInfo.setAsymAlgAbility(new long[]{7493065891348563935L, 3000543215027029126L});
        deviceInfo.setSymAlgAbility(2782707529341253316L);
        deviceInfo.setHashAlgAbility(7010608454676760881L);
    }

    @Override
    public String openDevice() {
        return "c95a78d9c04a557b7b46dbcb5f36cc66";
    }

    @Override
    public boolean closeDevice(String deviceHandle) {
        return true;
    }

    @Override
    public String openSession(String deviceHandle) {
        return "6975feaffaa35b31b6d4e4555ac403a1";
    }

    @Override
    public void closeSession(String sessionHandle) {

    }

    @Override
    public DeviceInfo getDeviceInfo(String sessionHandle) {
        return deviceInfo;
    }

    @Override
    public byte[] generateRandom(String sessionHandle, int uiLength) {
        byte[] res = new byte[uiLength];
        new SecureRandom().nextBytes(res);
        return res;
    }

    @Override
    public EccPubKey exportEncPublicKeyECC(String sessionHandle, int uiKeyIndex) {
        BigInteger d = new BigInteger(1, getD());
        ECPoint q = BCSM2Utils.G_POINT.multiply(d).normalize();
        return new EccPubKey(256, q.getXCoord().getEncoded(), q.getYCoord().getEncoded());
    }

    private byte[] getD() {
        return LangUtils.merge(LangUtils.toByteArray(deviceInfo.getAsymAlgAbility()[0]),
                LangUtils.toByteArray(deviceInfo.getAsymAlgAbility()[1]),
                LangUtils.toByteArray(deviceInfo.getSymAlgAbility()),
                LangUtils.toByteArray(deviceInfo.getHashAlgAbility()));
    }

    @SneakyThrows
    @Override
    public EccKey generateKeyPairECC(String sessionHandle, String alg, int uiKeyBits) {
        // 生成密钥对
        KeyPair keyPair = BCSM2Utils.generateKeyPair();
        BCECPublicKey pubKey = (BCECPublicKey) keyPair.getPublic();
        BCECPrivateKey priKey = (BCECPrivateKey) keyPair.getPrivate();
        byte[] x = pubKey.getQ().getXCoord().getEncoded();
        byte[] y = pubKey.getQ().getYCoord().getEncoded();
        byte[] d = BigIntegers.asUnsignedByteArray(32, priKey.getD());
        return new EccKey(LangUtils.merge(x, y), d);
    }

    @Override
    public boolean getPrivateKeyAccessRight(String hSessionHandle, int uiKeyIndex, byte[] pucPassword) {
        return false;
    }

    @Override
    public EccCipher exchangeDigitEnvelopeBaseOnECC(String sessionHandle, int uiKeyIndex, EccPubKey pubKey, EccCipher pucEncDateIn) {
        return null;
    }

    @Override
    public String importKeyWithISKECC(String sessionHandle, int uiIskIndex, EccCipher eccCipher) {
        return "";
    }

    @Override
    public String importKey(String sessionHandle, byte[] pucKey) {
        return "";
    }

    @Override
    public int destroyKey(String sessionHandle, String hKeyHandle) {
        return 0;
    }


    @Override
    public EccCipher externalEncryptECC(String sessionHandle, EccPubKey pubKey, byte[] pucData) {
        ECPublicKeyParameters parameters = BCECUtils.createECPublicKeyParameters(
                pubKey.getX(),
                pubKey.getY()
        );
        try {
            byte[] encrypt = BCSM2Utils.encrypt(parameters, pucData);
            return EccCipher.fromBytes(encrypt);
        } catch (InvalidCipherTextException e) {
            throw new IllegalArgumentException(e);
        }
    }


    @Override
    public byte[] symEncrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucData) {
        return new byte[0];
    }

    @Override
    public byte[] symDecrypt(String sessionHandle, String keyHandle, AlgId alg, byte[] pucIv, byte[] pucEncData) {
        return new byte[0];
    }
}
