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

package org.bouncycastle.crypto.digests;

import org.bouncycastle.util.Memoable;
import org.bouncycastle.crypto.CryptoServicePurpose;
import org.bouncycastle.crypto.SavableDigest;

public class SHA3Digest extends KeccakDigest implements SavableDigest
{
    private static int checkBitLength(final int i) {
        switch (i) {
            case 224:
            case 256:
            case 384:
            case 512: {
                return i;
            }
            default: {
                throw new IllegalArgumentException("'bitLength' " + i + " not supported for SHA-3");
            }
        }
    }
    
    public SHA3Digest() {
        this(256, CryptoServicePurpose.ANY);
    }
    
    public SHA3Digest(final CryptoServicePurpose cryptoServicePurpose) {
        this(256, cryptoServicePurpose);
    }
    
    public SHA3Digest(final int n) {
        super(checkBitLength(n), CryptoServicePurpose.ANY);
    }
    
    public SHA3Digest(final int n, final CryptoServicePurpose cryptoServicePurpose) {
        super(checkBitLength(n), cryptoServicePurpose);
    }
    
    public SHA3Digest(final byte[] array) {
        super(array);
    }
    
    public SHA3Digest(final SHA3Digest sha3Digest) {
        super(sha3Digest);
    }
    
    @Override
    public String getAlgorithmName() {
        return "SHA3-" + this.fixedOutputLength;
    }
    
    @Override
    public int doFinal(final byte[] array, final int n) {
        this.absorbBits(2, 2);
        return super.doFinal(array, n);
    }
    
    @Override
    protected int doFinal(final byte[] array, final int n, final byte b, final int n2) {
        if (n2 < 0 || n2 > 7) {
            throw new IllegalArgumentException("'partialBits' must be in the range [0,7]");
        }
        int n3 = (b & (1 << n2) - 1) | 2 << n2;
        int n4 = n2 + 2;
        if (n4 >= 8) {
            this.absorb((byte)n3);
            n4 -= 8;
            n3 >>>= 8;
        }
        return super.doFinal(array, n, (byte)n3, n4);
    }
    
    @Override
    public byte[] getEncodedState() {
        final byte[] array = new byte[this.state.length * 8 + this.dataQueue.length + 12 + 2];
        super.getEncodedState(array);
        return array;
    }
    
    @Override
    public Memoable copy() {
        return new SHA3Digest(this);
    }
    
    @Override
    public void reset(final Memoable memoable) {
        this.copyIn((KeccakDigest)memoable);
    }
}
