package org.bouncycastle.openpgp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.bcpg.UserAttributeSubpacket;

/* JADX INFO: loaded from: classes.dex */
abstract class PGPDefaultSignatureGenerator {
    protected byte lastb;
    protected OutputStream sigOut;
    protected int sigType;
    protected final int version;

    public PGPDefaultSignatureGenerator(int i) {
        this.version = i;
    }

    private void byteUpdate(byte b) {
        try {
            this.sigOut.write(b);
        } catch (IOException e) {
            throw new PGPRuntimeOperationException(e.getMessage(), e);
        }
    }

    private byte[] getEncodedPublicKey(PGPPublicKey pGPPublicKey) throws PGPException {
        try {
            return pGPPublicKey.publicPk.getEncodedContents();
        } catch (IOException e) {
            throw new PGPException("exception preparing key.", e);
        }
    }

    protected void blockUpdate(byte[] bArr, int i, int i2) {
        try {
            this.sigOut.write(bArr, i, i2);
        } catch (IOException e) {
            throw new PGPRuntimeOperationException("unable to update signature: " + e.getMessage(), e);
        }
    }

    protected void getAttributesHash(PGPUserAttributeSubpacketVector pGPUserAttributeSubpacketVector) throws PGPException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            UserAttributeSubpacket[] subpacketArray = pGPUserAttributeSubpacketVector.toSubpacketArray();
            for (int i = 0; i != subpacketArray.length; i++) {
                subpacketArray[i].encode(byteArrayOutputStream);
            }
            updateWithIdData(209, byteArrayOutputStream.toByteArray());
        } catch (IOException e) {
            throw new PGPException("cannot encode subpacket array", e);
        }
    }

    /* JADX WARN: Code duplicated, block: B:6:0x000b  */
    public void update(byte b) {
        if (this.sigType != 1) {
            byteUpdate(b);
            return;
        }
        if (b == 13) {
            byteUpdate((byte) 13);
            byteUpdate((byte) 10);
        } else if (b != 10) {
            byteUpdate(b);
        } else if (this.lastb != 13) {
            byteUpdate((byte) 13);
            byteUpdate((byte) 10);
        }
        this.lastb = b;
    }

    public void update(byte[] bArr) {
        update(bArr, 0, bArr.length);
    }

    public void update(byte[] bArr, int i, int i2) {
        if (this.sigType != 1) {
            blockUpdate(bArr, i, i2);
            return;
        }
        int i3 = i2 + i;
        while (i != i3) {
            update(bArr[i]);
            i++;
        }
    }

    protected void updateWithIdData(int i, byte[] bArr) {
        update((byte) i);
        update((byte) (bArr.length >> 24));
        update((byte) (bArr.length >> 16));
        update((byte) (bArr.length >> 8));
        update((byte) bArr.length);
        update(bArr);
    }

    protected void updateWithPublicKey(PGPPublicKey pGPPublicKey) throws PGPException {
        byte b;
        byte length;
        byte[] encodedPublicKey = getEncodedPublicKey(pGPPublicKey);
        if (this.version == 4) {
            length = -103;
        } else {
            if (this.version != 5) {
                b = this.version == 6 ? (byte) -101 : (byte) -102;
                update(encodedPublicKey);
            }
            update(b);
            update((byte) (encodedPublicKey.length >> 24));
            length = (byte) (encodedPublicKey.length >> 16);
        }
        update(length);
        update((byte) (encodedPublicKey.length >> 8));
        update((byte) encodedPublicKey.length);
        update(encodedPublicKey);
    }
}
