summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/prng/test/TestEntropySourceProvider.java
blob: 64e759585a56a0797fb02ec6b2d874ee0035ebea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.bouncycastle.crypto.prng.test;

import org.bouncycastle.crypto.prng.EntropySource;
import org.bouncycastle.crypto.prng.EntropySourceProvider;

public class TestEntropySourceProvider
    implements EntropySourceProvider
{
    private final byte[] data;
    private final boolean isPredictionResistant;

    protected TestEntropySourceProvider(byte[] data, boolean isPredictionResistant)
    {
        this.data = data;
        this.isPredictionResistant = isPredictionResistant;
    }

    public EntropySource get(final int bitsRequired)
    {
        return new EntropySource()
        {
            int index = 0;

            public boolean isPredictionResistant()
            {
                return isPredictionResistant;
            }

            public byte[] getEntropy()
            {
                byte[] rv = new byte[bitsRequired / 8];

                System.arraycopy(data, index, rv, 0, rv.length);

                index += bitsRequired / 8;

                return rv;
            }

            public int entropySize()
            {
                return bitsRequired;
            }
        };
    }
}