aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/jce/src/main/java/javax/crypto/spec/IvParameterSpec.java
blob: 4ed7376c0aa307582442e74a6666894571423ee6 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package javax.crypto.spec;

import java.security.spec.AlgorithmParameterSpec;

/**
 * This class specifies an <i>initialization vector</i> (IV). IVs are used
 * by ciphers in feedback mode, e.g., DES in CBC mode.
 */
public class IvParameterSpec
    implements AlgorithmParameterSpec
{
    private byte[]  iv;

    /**
     * Uses the bytes in <code>iv</code> as the IV.
     *
     * @param iv the buffer with the IV
     */
    public IvParameterSpec(
        byte[]  iv)
    {
        if (iv == null)
        {
            throw new IllegalArgumentException("null iv passed");
        }

        this.iv = new byte[iv.length];

        System.arraycopy(iv, 0, this.iv, 0, iv.length);
    }

    /**
     * Uses the first <code>len</code> bytes in <code>iv</code>,
     * beginning at <code>offset</code> inclusive, as the IV.
     * <p>
     * The bytes that constitute the IV are those between
     * <code>iv[offset]</code> and <code>iv[offset+len-1]</code> inclusive.
     *
     * @param iv the buffer with the IV
     * @param offset the offset in <code>iv</code> where the IV starts
     * @param len the number of IV bytes
     */
    public IvParameterSpec(
        byte[]  iv,
        int     offset,
        int     len)
    {
        if (iv == null)
        {
            throw new IllegalArgumentException("Null iv passed");
        }

        if (offset < 0 || len < 0 || (iv.length - offset) < len)
        {
            throw new IllegalArgumentException("Bad offset/len");
        }

        this.iv = new byte[len];

        System.arraycopy(iv, offset, this.iv, 0, len);
    }

    /**
     * Returns the initialization vector (IV).
     *
     * @return the initialization vector (IV)
     */
    public byte[] getIV()
    {
        byte[]  tmp = new byte[iv.length];

        System.arraycopy(iv, 0, tmp, 0, iv.length);
        return tmp;
    }
}