package org.bouncycastle.bcpg;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.Streams;

/* JADX INFO: loaded from: classes.dex */
public class OnePassSignaturePacket extends ContainedPacket {
    public static final int VERSION_3 = 3;
    public static final int VERSION_6 = 6;
    private final byte[] fingerprint;
    private final int hashAlgorithm;
    private final int isContaining;
    private final int keyAlgorithm;
    private final long keyID;
    private final byte[] salt;
    private final int sigType;
    private final int version;

    public OnePassSignaturePacket(int i, int i2, int i3, long j, boolean z) {
        super(4);
        this.version = 3;
        this.sigType = i;
        this.hashAlgorithm = i2;
        this.keyAlgorithm = i3;
        this.keyID = j;
        this.fingerprint = null;
        this.salt = null;
        this.isContaining = !z ? 1 : 0;
    }

    public OnePassSignaturePacket(int i, int i2, int i3, byte[] bArr, byte[] bArr2, boolean z) {
        super(4);
        this.version = 6;
        this.sigType = i;
        this.hashAlgorithm = i2;
        this.keyAlgorithm = i3;
        this.salt = bArr;
        this.fingerprint = bArr2;
        this.isContaining = !z ? 1 : 0;
        this.keyID = FingerprintUtil.keyIdFromV6Fingerprint(bArr2);
    }

    OnePassSignaturePacket(BCPGInputStream bCPGInputStream) throws IOException {
        this(bCPGInputStream, false);
    }

    OnePassSignaturePacket(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        super(4, z);
        this.version = bCPGInputStream.read();
        this.sigType = bCPGInputStream.read();
        this.hashAlgorithm = bCPGInputStream.read();
        this.keyAlgorithm = bCPGInputStream.read();
        if (this.version == 3) {
            this.keyID = StreamUtil.readKeyID(bCPGInputStream);
            this.fingerprint = null;
            this.salt = null;
        } else {
            if (this.version != 6) {
                Streams.drain(bCPGInputStream);
                throw new UnsupportedPacketVersionException("Unsupported OnePassSignature packet version encountered: " + this.version);
            }
            int i = bCPGInputStream.read();
            if (i < 0) {
                throw new IOException("Version 6 OPS packet has invalid salt length.");
            }
            this.salt = new byte[i];
            bCPGInputStream.readFully(this.salt);
            this.fingerprint = new byte[32];
            bCPGInputStream.readFully(this.fingerprint);
            this.keyID = FingerprintUtil.keyIdFromV6Fingerprint(this.fingerprint);
        }
        this.isContaining = bCPGInputStream.read();
    }

    @Override // org.bouncycastle.bcpg.ContainedPacket
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream2 = new BCPGOutputStream(byteArrayOutputStream);
        bCPGOutputStream2.write(this.version);
        bCPGOutputStream2.write(this.sigType);
        bCPGOutputStream2.write(this.hashAlgorithm);
        bCPGOutputStream2.write(this.keyAlgorithm);
        if (this.version == 3) {
            StreamUtil.writeKeyID(bCPGOutputStream2, this.keyID);
        } else if (this.version == 6) {
            bCPGOutputStream2.write(this.salt.length);
            bCPGOutputStream2.write(this.salt);
            bCPGOutputStream2.write(this.fingerprint);
        }
        bCPGOutputStream2.write(this.isContaining);
        bCPGOutputStream2.close();
        bCPGOutputStream.writePacket(hasNewPacketFormat(), 4, byteArrayOutputStream.toByteArray());
    }

    public byte[] getFingerprint() {
        return Arrays.clone(this.fingerprint);
    }

    public int getHashAlgorithm() {
        return this.hashAlgorithm;
    }

    public int getKeyAlgorithm() {
        return this.keyAlgorithm;
    }

    public long getKeyID() {
        return this.keyID;
    }

    public byte[] getSalt() {
        return Arrays.clone(this.salt);
    }

    public int getSignatureType() {
        return this.sigType;
    }

    public int getVersion() {
        return this.version;
    }

    public boolean isContaining() {
        return this.isContaining == 1;
    }
}
