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

org.bouncycastle.cert.dane.TruncatingDigestCalculator Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified.

There is a newer version: 2.0.7
Show newest version
package org.bouncycastle.cert.dane;

import java.io.OutputStream;

import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.DigestCalculator;

/**
 * A calculator which produces a truncated digest from a regular one, with the truncation
 * achieved by dropping off the right most octets.
 */
public class TruncatingDigestCalculator
    implements DigestCalculator
{
    private final DigestCalculator baseCalculator;
    private final int length;

    /**
     * Default constructor - truncate to 28.
     *
     * @param baseCalculator actual calculator for working out the digest.
     */
    public TruncatingDigestCalculator(DigestCalculator baseCalculator)
    {
       this(baseCalculator, 28);
    }

    /**
     * Constructor specifying a length.
     *
     * @param baseCalculator actual calculator for working out the digest.
     * @param length length in bytes of the final result.
     */
    public TruncatingDigestCalculator(DigestCalculator baseCalculator, int length)
    {
        this.baseCalculator = baseCalculator;
        this.length = length;
    }

    public AlgorithmIdentifier getAlgorithmIdentifier()
    {
        return baseCalculator.getAlgorithmIdentifier();
    }

    public OutputStream getOutputStream()
    {
        return baseCalculator.getOutputStream();
    }

    public byte[] getDigest()
    {
        byte[] rv = new byte[length];

        byte[] dig = baseCalculator.getDigest();

        System.arraycopy(dig, 0, rv, 0, rv.length);

        return rv;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy