All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.crypto.digests.Prehash Maven / Gradle / Ivy

There is a newer version: 2.0.0.0-RC3
Show newest version
package org.bouncycastle.crypto.digests;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.util.io.LimitedBuffer;

public class Prehash
    implements Digest
{
    public static Prehash forDigest(Digest digest)
    {
        return new Prehash(digest);
    }

    private final String algorithmName;
    private final LimitedBuffer buf;

    private Prehash(Digest digest)
    {
        algorithmName = digest.getAlgorithmName();
        buf = new LimitedBuffer(digest.getDigestSize());
    }

    public String getAlgorithmName()
    {
        return algorithmName;
    }

    public int getDigestSize()
    {
        return buf.limit();
    }

    public void update(byte in)
    {
        buf.write(in);
    }

    public void update(byte[] in, int inOff, int len)
    {
        buf.write(in, inOff, len);
    }

    public int doFinal(byte[] out, int outOff)
    {
        try
        {
            if (getDigestSize() != buf.size())
            {
                throw new IllegalStateException("Incorrect prehash size");
            }

            return buf.copyTo(out, outOff);
        }
        finally
        {
            reset();
        }
    }

    public void reset()
    {
        buf.reset();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy