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

org.bouncycastle.crypto.util.EntropyUtil 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.util;

import org.bouncycastle.crypto.EntropySource;

/**
 * Utility methods for making use of EntropySources.
 */
public class EntropyUtil
{
    /**
     * Generate numBytes worth of entropy from the passed in entropy source.
     *
     * @param entropySource the entropy source to request the data from.
     * @param numBytes the number of bytes of entropy requested.
     * @return a byte array populated with the random data.
     */
    public static byte[] generateSeed(EntropySource entropySource, int numBytes)
    {
        byte[] bytes = new byte[numBytes];

        if (numBytes * 8 <= entropySource.entropySize())
        {
            byte[] ent = entropySource.getEntropy();

            System.arraycopy(ent, 0, bytes, 0, bytes.length);
        }
        else
        {
            int entSize = entropySource.entropySize() / 8;

            for (int i = 0; i < bytes.length; i += entSize)
            {
                byte[] ent = entropySource.getEntropy();

                if (ent.length <= bytes.length - i)
                {
                    System.arraycopy(ent, 0, bytes, i, ent.length);
                }
                else
                {
                    System.arraycopy(ent, 0, bytes, i, bytes.length - i);
                }
            }
        }

        return bytes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy