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

org.bouncycastle.crypto.general.GeneralParametersWithIV Maven / Gradle / Ivy

Go to download

The FIPS 140-3 Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms certified to FIPS 140-3 level 1. This jar contains JCE provider and low-level API for the BC-FJA version 2.0.0, FIPS Certificate #4743. Please see certificate for certified platform details.

There is a newer version: 2.0.0
Show newest version
package org.bouncycastle.crypto.general;

import java.security.SecureRandom;

import org.bouncycastle.crypto.ParametersWithIV;
import org.bouncycastle.util.Arrays;

/**
 * Base class for parameter classes for algorithms that require an initialization vector or nonce.
 *
 * @param  the actual parameters type that extends this class.
 */
public abstract class GeneralParametersWithIV
    extends GeneralParameters
    implements ParametersWithIV
{
    protected final int blockSize;
    protected final byte[] iv;

    GeneralParametersWithIV(GeneralAlgorithm algorithm, int blockSize, byte[] iv)
    {
        super(algorithm);

        this.blockSize = blockSize;
        this.iv = iv;
    }

    /**
     * Return a copy of the current IV value.
     *
     * @return the current IV.
     */
    public byte[] getIV()
    {
        return Arrays.clone(iv);
    }

    /**
     * Return an implementation of our parameterized type with an IV constructed from the passed in SecureRandom.
     *
     * @param random the SecureRandom to use as the source of IV data.
     * @return a new instance of our parameterized type with a new IV.
     */
    public T withIV(SecureRandom random)
    {
        return create(this.getAlgorithm(), this.getAlgorithm().createDefaultIvIfNecessary(blockSize, random));
    }

    /**
     * Return an implementation of our parameterized type containing the passed in IV.
     *
     * @param iv the bytes making up the iv, or nonce, to use.
     * @return a new instance of our parameterized type with a new IV.
     */
    public T withIV(byte[] iv)
    {
        return create(this.getAlgorithm(), Arrays.clone(iv));
    }

    abstract T create(GeneralAlgorithm algorithm, byte[] iv);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy