package com.yida.data.common.core.utils.construction;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;

/**
 * RSAPubKey类
 *
 * @author ZYJ
 * @date 2023/6/22 2:25
 */
public class RSAPubKey implements RSAPublicKey {

    protected BigInteger exponent;
    protected BigInteger modulus;

    public RSAPubKey() {
    }

    public RSAPubKey(BigInteger biginteger, BigInteger biginteger1) {
        this.modulus = biginteger;
        this.exponent = biginteger1;
    }

    public RSAPubKey(byte[] abyte0) {
        this.x509Decode(abyte0);
    }

    @Override
    public String getAlgorithm() {
        return "RSA";
    }

    @Override
    public byte[] getEncoded() {
        return this.x509Encode();
    }

    @Override
    public String getFormat() {
        return "X.509";
    }

    @Override
    public BigInteger getModulus() {
        return this.modulus;
    }

    @Override
    public BigInteger getPublicExponent() {
        return this.exponent;
    }

    @Override
    public String toString() {
        return this.modulus.toString(16) + "." + this.exponent.toString(16);
    }

    private void x509Decode(byte[] abyte0) {
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);

        try {
            DER.readTag(bytearrayinputstream);
            DER.readLen(bytearrayinputstream);
            bytearrayinputstream.skip((long)DER.rsaEncryptionAlgorithmIdentifier.length);
            DER.readTag(bytearrayinputstream);
            DER.readLen(bytearrayinputstream);
            bytearrayinputstream.skip(1L);
            DER.readTag(bytearrayinputstream);
            DER.readLen(bytearrayinputstream);
            this.modulus = DER.readDERint(bytearrayinputstream);
            this.exponent = DER.readDERint(bytearrayinputstream);
        } catch (IOException var4) {
            var4.printStackTrace();
            throw new ExceptionInInitializerError(var4);
        }
    }

    private byte[] x509Encode() {
        try {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            DER.writeDERint(bytearrayoutputstream, this.modulus);
            DER.writeDERint(bytearrayoutputstream, this.exponent);
            byte[] abyte0 = bytearrayoutputstream.toByteArray();
            bytearrayoutputstream = new ByteArrayOutputStream();
            bytearrayoutputstream.write(DER.SEQUENCE | DER.CONSTRUCTED);
            DER.writeDERlen(bytearrayoutputstream, abyte0.length);
            bytearrayoutputstream.write(abyte0);
            byte[] abyte1 = bytearrayoutputstream.toByteArray();
            bytearrayoutputstream = new ByteArrayOutputStream();
            bytearrayoutputstream.write(DER.BIT_STRING);
            DER.writeDERlen(bytearrayoutputstream, abyte1.length + 1);
            bytearrayoutputstream.write(0);
            bytearrayoutputstream.write(abyte1);
            byte[] abyte2 = bytearrayoutputstream.toByteArray();
            bytearrayoutputstream = new ByteArrayOutputStream();
            bytearrayoutputstream.write(DER.SEQUENCE | DER.CONSTRUCTED);
            DER.writeDERlen(bytearrayoutputstream, DER.rsaEncryptionAlgorithmIdentifier.length + abyte2.length);
            bytearrayoutputstream.write(DER.rsaEncryptionAlgorithmIdentifier);
            bytearrayoutputstream.write(abyte2);
            return bytearrayoutputstream.toByteArray();
        } catch (IOException var5) {
            var5.printStackTrace();
            throw new ExceptionInInitializerError(var5);
        }
    }

}
