PBEParametersGenerator.java revision a198e1ecc615e26a167d0f2dca9fa7e5fc62de10
1package org.bouncycastle.crypto;
2
3import org.bouncycastle.util.Strings;
4
5/**
6 * super class for all Password Based Encryption (PBE) parameter generator classes.
7 */
8public abstract class PBEParametersGenerator
9{
10    protected byte[]  password;
11    protected byte[]  salt;
12    protected int     iterationCount;
13
14    /**
15     * base constructor.
16     */
17    protected PBEParametersGenerator()
18    {
19    }
20
21    /**
22     * initialise the PBE generator.
23     *
24     * @param password the password converted into bytes (see below).
25     * @param salt the salt to be mixed with the password.
26     * @param iterationCount the number of iterations the "mixing" function
27     * is to be applied for.
28     */
29    public void init(
30        byte[]  password,
31        byte[]  salt,
32        int     iterationCount)
33    {
34        this.password = password;
35        this.salt = salt;
36        this.iterationCount = iterationCount;
37    }
38
39    /**
40     * return the password byte array.
41     *
42     * @return the password byte array.
43     */
44    public byte[] getPassword()
45    {
46        return password;
47    }
48
49    /**
50     * return the salt byte array.
51     *
52     * @return the salt byte array.
53     */
54    public byte[] getSalt()
55    {
56        return salt;
57    }
58
59    /**
60     * return the iteration count.
61     *
62     * @return the iteration count.
63     */
64    public int getIterationCount()
65    {
66        return iterationCount;
67    }
68
69    /**
70     * generate derived parameters for a key of length keySize.
71     *
72     * @param keySize the length, in bits, of the key required.
73     * @return a parameters object representing a key.
74     */
75    public abstract CipherParameters generateDerivedParameters(int keySize);
76
77    /**
78     * generate derived parameters for a key of length keySize, and
79     * an initialisation vector (IV) of length ivSize.
80     *
81     * @param keySize the length, in bits, of the key required.
82     * @param ivSize the length, in bits, of the iv required.
83     * @return a parameters object representing a key and an IV.
84     */
85    public abstract CipherParameters generateDerivedParameters(int keySize, int ivSize);
86
87    /**
88     * generate derived parameters for a key of length keySize, specifically
89     * for use with a MAC.
90     *
91     * @param keySize the length, in bits, of the key required.
92     * @return a parameters object representing a key.
93     */
94    public abstract CipherParameters generateDerivedMacParameters(int keySize);
95
96    /**
97     * converts a password to a byte array according to the scheme in
98     * PKCS5 (ascii, no padding)
99     *
100     * @param password a character array representing the password.
101     * @return a byte array representing the password.
102     */
103    public static byte[] PKCS5PasswordToBytes(
104        char[]  password)
105    {
106        if (password != null)
107        {
108            byte[]  bytes = new byte[password.length];
109
110            for (int i = 0; i != bytes.length; i++)
111            {
112                bytes[i] = (byte)password[i];
113            }
114
115            return bytes;
116        }
117        else
118        {
119            return new byte[0];
120        }
121    }
122
123    /**
124     * converts a password to a byte array according to the scheme in
125     * PKCS5 (UTF-8, no padding)
126     *
127     * @param password a character array representing the password.
128     * @return a byte array representing the password.
129     */
130    public static byte[] PKCS5PasswordToUTF8Bytes(
131        char[]  password)
132    {
133        if (password != null)
134        {
135            return Strings.toUTF8ByteArray(password);
136        }
137        else
138        {
139            return new byte[0];
140        }
141    }
142
143    /**
144     * converts a password to a byte array according to the scheme in
145     * PKCS12 (unicode, big endian, 2 zero pad bytes at the end).
146     *
147     * @param password a character array representing the password.
148     * @return a byte array representing the password.
149     */
150    public static byte[] PKCS12PasswordToBytes(
151        char[]  password)
152    {
153        if (password != null && password.length > 0)
154        {
155                                       // +1 for extra 2 pad bytes.
156            byte[]  bytes = new byte[(password.length + 1) * 2];
157
158            for (int i = 0; i != password.length; i ++)
159            {
160                bytes[i * 2] = (byte)(password[i] >>> 8);
161                bytes[i * 2 + 1] = (byte)password[i];
162            }
163
164            return bytes;
165        }
166        else
167        {
168            return new byte[0];
169        }
170    }
171}
172