CipherKeyGenerator.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.crypto;
2
3import java.security.SecureRandom;
4
5/**
6 * The base class for symmetric, or secret, cipher key generators.
7 */
8public class CipherKeyGenerator
9{
10    protected SecureRandom  random;
11    protected int           strength;
12
13    /**
14     * initialise the key generator.
15     *
16     * @param param the parameters to be used for key generation
17     */
18    public void init(
19        KeyGenerationParameters param)
20    {
21        this.random = param.getRandom();
22        this.strength = (param.getStrength() + 7) / 8;
23    }
24
25    /**
26     * generate a secret key.
27     *
28     * @return a byte array containing the key value.
29     */
30    public byte[] generateKey()
31    {
32        byte[]  key = new byte[strength];
33
34        random.nextBytes(key);
35
36        return key;
37    }
38}
39