// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.signers;

import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.BigIntegers;
import java.math.BigInteger;

public class PlainDSAEncoding implements DSAEncoding
{
    public static final PlainDSAEncoding INSTANCE;
    
    @Override
    public byte[] encode(final BigInteger bigInteger, final BigInteger bigInteger2, final BigInteger bigInteger3) {
        final int unsignedByteLength = BigIntegers.getUnsignedByteLength(bigInteger);
        final byte[] array = new byte[unsignedByteLength * 2];
        this.encodeValue(bigInteger, bigInteger2, array, 0, unsignedByteLength);
        this.encodeValue(bigInteger, bigInteger3, array, unsignedByteLength, unsignedByteLength);
        return array;
    }
    
    @Override
    public BigInteger[] decode(final BigInteger bigInteger, final byte[] array) {
        final int unsignedByteLength = BigIntegers.getUnsignedByteLength(bigInteger);
        if (array.length != unsignedByteLength * 2) {
            throw new IllegalArgumentException("Encoding has incorrect length");
        }
        return new BigInteger[] { this.decodeValue(bigInteger, array, 0, unsignedByteLength), this.decodeValue(bigInteger, array, unsignedByteLength, unsignedByteLength) };
    }
    
    protected BigInteger checkValue(final BigInteger val, final BigInteger bigInteger) {
        if (bigInteger.signum() < 0 || bigInteger.compareTo(val) >= 0) {
            throw new IllegalArgumentException("Value out of range");
        }
        return bigInteger;
    }
    
    protected BigInteger decodeValue(final BigInteger bigInteger, final byte[] array, final int n, final int n2) {
        return this.checkValue(bigInteger, new BigInteger(1, Arrays.copyOfRange(array, n, n + n2)));
    }
    
    private void encodeValue(final BigInteger bigInteger, final BigInteger bigInteger2, final byte[] array, final int n, final int n2) {
        final byte[] byteArray = this.checkValue(bigInteger, bigInteger2).toByteArray();
        final int max = Math.max(0, byteArray.length - n2);
        final int n3 = byteArray.length - max;
        final int n4 = n2 - n3;
        Arrays.fill(array, n, n + n4, (byte)0);
        System.arraycopy(byteArray, max, array, n + n4, n3);
    }
    
    static {
        INSTANCE = new PlainDSAEncoding();
    }
}
