~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/bcpg/sig/KeyFlags.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.bcpg.sig;
 
2
 
 
3
import org.bouncycastle.bcpg.SignatureSubpacket;
 
4
import org.bouncycastle.bcpg.SignatureSubpacketTags;
 
5
 
 
6
/**
 
7
 * Packet holding the key flag values.
 
8
 */
 
9
public class KeyFlags 
 
10
    extends SignatureSubpacket
 
11
{
 
12
    public static final int CERTIFY_OTHER = 0x01;
 
13
    public static final int SIGN_DATA = 0x02;
 
14
    public static final int ENCRYPT_COMMS = 0x04;
 
15
    public static final int ENCRYPT_STORAGE = 0x08;
 
16
    public static final int SPLIT = 0x10;
 
17
    public static final int AUTHENTICATION = 0x20;
 
18
    public static final int SHARED = 0x80;
 
19
    
 
20
    private static byte[] intToByteArray(
 
21
        int    v)
 
22
    {
 
23
        byte[] tmp = new byte[4];
 
24
        int    size = 0;
 
25
 
 
26
        for (int i = 0; i != 4; i++)
 
27
        {
 
28
            tmp[i] = (byte)(v >> (i * 8));
 
29
            if (tmp[i] != 0)
 
30
            {
 
31
                size = i;
 
32
            }
 
33
        }
 
34
 
 
35
        byte[]    data = new byte[size + 1];
 
36
        
 
37
        System.arraycopy(tmp, 0, data, 0, data.length);
 
38
 
 
39
        return data;
 
40
    }
 
41
    
 
42
    public KeyFlags(
 
43
        boolean    critical,
 
44
        byte[]     data)
 
45
    {
 
46
        super(SignatureSubpacketTags.KEY_FLAGS, critical, data);
 
47
    }
 
48
    
 
49
    public KeyFlags(
 
50
        boolean    critical,
 
51
        int        flags)
 
52
    {
 
53
        super(SignatureSubpacketTags.KEY_FLAGS, critical, intToByteArray(flags));
 
54
    }
 
55
 
 
56
    /**
 
57
     * Return the flag values contained in the first 4 octets (note: at the moment
 
58
     * the standard only uses the first one).
 
59
     *
 
60
     * @return flag values.
 
61
     */
 
62
    public int getFlags()
 
63
    {
 
64
        int flags = 0;
 
65
 
 
66
        for (int i = 0; i != data.length; i++)
 
67
        {
 
68
            flags |= (data[i] & 0xff) << (i * 8);
 
69
        }
 
70
 
 
71
        return flags;
 
72
    }
 
73
}