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

package org.bouncycastle.crypto.signers;

import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.math.ec.rfc8032.Ed25519;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Signer;

public class Ed25519phSigner implements Signer
{
    private final Digest prehash;
    private final byte[] context;
    private boolean forSigning;
    private Ed25519PrivateKeyParameters privateKey;
    private Ed25519PublicKeyParameters publicKey;
    
    public Ed25519phSigner(final byte[] array) {
        this.prehash = Ed25519.createPrehash();
        if (null == array) {
            throw new NullPointerException("'context' cannot be null");
        }
        this.context = Arrays.clone(array);
    }
    
    @Override
    public void init(final boolean forSigning, final CipherParameters cipherParameters) {
        this.forSigning = forSigning;
        if (forSigning) {
            this.privateKey = (Ed25519PrivateKeyParameters)cipherParameters;
            this.publicKey = null;
        }
        else {
            this.privateKey = null;
            this.publicKey = (Ed25519PublicKeyParameters)cipherParameters;
        }
        CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("Ed25519", 128, cipherParameters, forSigning));
        this.reset();
    }
    
    @Override
    public void update(final byte b) {
        this.prehash.update(b);
    }
    
    @Override
    public void update(final byte[] array, final int n, final int n2) {
        this.prehash.update(array, n, n2);
    }
    
    @Override
    public byte[] generateSignature() {
        if (!this.forSigning || null == this.privateKey) {
            throw new IllegalStateException("Ed25519phSigner not initialised for signature generation.");
        }
        final byte[] array = new byte[64];
        if (64 != this.prehash.doFinal(array, 0)) {
            throw new IllegalStateException("Prehash digest failed");
        }
        final byte[] array2 = new byte[64];
        this.privateKey.sign(2, this.context, array, 0, 64, array2, 0);
        return array2;
    }
    
    @Override
    public boolean verifySignature(final byte[] array) {
        if (this.forSigning || null == this.publicKey) {
            throw new IllegalStateException("Ed25519phSigner not initialised for verification");
        }
        if (64 != array.length) {
            this.prehash.reset();
            return false;
        }
        final byte[] array2 = new byte[64];
        if (64 != this.prehash.doFinal(array2, 0)) {
            throw new IllegalStateException("Prehash digest failed");
        }
        return this.publicKey.verify(2, this.context, array2, 0, 64, array, 0);
    }
    
    @Override
    public void reset() {
        this.prehash.reset();
    }
}
