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

package org.bouncycastle.crypto.digests;

import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Bytes;
import org.bouncycastle.crypto.engines.PhotonBeetleEngine;

public class PhotonBeetleDigest extends BufferBaseDigest
{
    private final byte[] state;
    private static final int SQUEEZE_RATE_INBYTES = 16;
    private static final int D = 8;
    private int blockCount;
    
    public PhotonBeetleDigest() {
        super(ProcessingBufferType.Buffered, 4);
        this.DigestSize = 32;
        this.state = new byte[this.DigestSize];
        this.algorithmName = "Photon-Beetle Hash";
        this.blockCount = 0;
    }
    
    @Override
    protected void processBytes(final byte[] array, final int n) {
        if (this.blockCount < 4) {
            System.arraycopy(array, n, this.state, this.blockCount << 2, this.BlockSize);
        }
        else {
            PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, this.state);
            Bytes.xorTo(this.BlockSize, array, n, this.state);
        }
        ++this.blockCount;
    }
    
    @Override
    protected void finish(final byte[] array, final int n) {
        final int n2 = 5;
        if (this.m_bufPos == 0 && this.blockCount == 0) {
            final byte[] state = this.state;
            final int n3 = this.DigestSize - 1;
            state[n3] ^= (byte)(1 << n2);
        }
        else if (this.blockCount < 4) {
            System.arraycopy(this.m_buf, 0, this.state, this.blockCount << 2, this.m_bufPos);
            final byte[] state2 = this.state;
            final int n4 = (this.blockCount << 2) + this.m_bufPos;
            state2[n4] ^= 0x1;
            final byte[] state3 = this.state;
            final int n5 = this.DigestSize - 1;
            state3[n5] ^= (byte)(1 << n2);
        }
        else if (this.blockCount == 4 && this.m_bufPos == 0) {
            final byte[] state4 = this.state;
            final int n6 = this.DigestSize - 1;
            state4[n6] ^= (byte)(2 << n2);
        }
        else {
            PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, this.state);
            Bytes.xorTo(this.m_bufPos, this.m_buf, this.state);
            if (this.m_bufPos < this.BlockSize) {
                final byte[] state5 = this.state;
                final int bufPos = this.m_bufPos;
                state5[bufPos] ^= 0x1;
            }
            final byte[] state6 = this.state;
            final int n7 = this.DigestSize - 1;
            state6[n7] ^= (byte)(((this.m_bufPos % this.BlockSize == 0) ? 1 : 2) << n2);
        }
        PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, this.state);
        System.arraycopy(this.state, 0, array, n, 16);
        PhotonBeetleEngine.photonPermutation(Friend.INSTANCE, this.state);
        System.arraycopy(this.state, 0, array, n + 16, 16);
    }
    
    @Override
    public void reset() {
        super.reset();
        Arrays.fill(this.state, (byte)0);
        this.blockCount = 0;
    }
    
    public static class Friend
    {
        private static final Friend INSTANCE;
        
        private Friend() {
        }
        
        static {
            INSTANCE = new Friend();
        }
    }
}
