1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email transport capabilities.
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.der;
10
11
12 import org.webdocwf.util.smime.exception.SMIMEException;
13
14
15 /***
16 * DERBitString is primitive type of DER encoded object which represents bit
17 * string (array of bits) in ASN.1 notation.
18 */
19 public class DERBitString extends DERObject {
20
21 /***
22 * Constructs DERBitString object from string which has only two values of his
23 * elements: 0 and 1
24 * @param bitStr0 String consisting of 0 and 1 elements
25 * @exception SMIMEException thrown in super class constructor or in super class
26 * method addContent.
27 */
28 public DERBitString(String bitStr0) throws SMIMEException {
29 super(3);
30 super.addContent(this.UnusedBitsFromString(bitStr0));
31 super.addContent(this.ConvertStringBitsToByteArray(bitStr0));
32 }
33
34 /***
35 * Constructs DERBitString object from boolean array which has only two values
36 * of his elements: true and false.
37 * @param bitsArray0 array consisting of boolean elements
38 * @exception SMIMEException thrown in super class constructor or in super class
39 * method addContent.
40 */
41 public DERBitString(boolean[] bitsArray0) throws SMIMEException {
42 super(3);
43 String temp = "";
44
45 for (int i = 0; i != bitsArray0.length; i++) {
46 if (bitsArray0[i])
47 temp = temp.concat("1");
48 else
49 temp = temp.concat("0");
50 }
51 super.addContent(this.UnusedBitsFromString(temp));
52 super.addContent(this.ConvertStringBitsToByteArray(temp));
53 }
54
55 /***
56 * Constructs DERBitString object from bits organised in bytes with information
57 * about last few unused bits. This information is important in generation of
58 * DER object because first byte in Content Octets part of DERBitString
59 * represents number of DERBitString.
60 * @param bitStr0 bit string represented as byte array
61 * @param unused0 number of last few unused bits from last input byte
62 * (must be in range 0-7)
63 * @exception SMIMEException if number of unused bits isn't between 0-7. Also,
64 * it can be thrown in super class constructor or addContent method.
65 */
66 public DERBitString(byte[] bitStr0, int unused0) throws SMIMEException {
67 super(3);
68 if (unused0 > 7 || unused0 < 0)
69 throw new SMIMEException(this, 1006);
70 byte[] temp = new byte[1];
71
72 temp[0] = (byte) unused0; // Number of a last few unused bits
73 super.addContent(temp);
74 super.addContent(bitStr0); // bitStr0 is a byte array containing the two's-complement binary representation of a bit String
75 }
76
77 /***
78 * Constructs bit string represented as byte array without information about
79 * unused bits (no bits are unused).
80 * @param bitStr0 bit string represented as byte array
81 * @exception SMIMEException thrown in super class constructor or addContent
82 * method of super class.
83 */
84 public DERBitString(byte[] bitStr0) throws SMIMEException {
85 this(bitStr0, 0);
86 }
87
88 /***
89 * Finds number of unused last bits (0-7) from bit string converted into byte array
90 * @param bitStr0 bit string for verification number of unused bits.
91 * @return Number between 0-7
92 */
93 private byte[] UnusedBitsFromString(String bitStr0) {
94 byte[] temp = new byte[1];
95 int len = bitStr0.length();
96
97 temp[0] = (byte) (8 - len % 8); // Number of unused bits
98 if (temp[0] == 8)
99 temp[0] = 0;
100 return temp;
101 }
102
103 /***
104 * Converts String consisting of 0 and 1 into byte array
105 * @param bitStr0 bit string for transformation in byte array of the Content
106 * Octets
107 * @return Content Octets of DER encoded bit string object
108 * @exception SMIMEException if bit string has values other than 0 and 1
109 */
110 private byte[] ConvertStringBitsToByteArray(String bitStr0) throws SMIMEException {
111 int len = bitStr0.length();
112
113 if (len % 8 == 0)
114 len = len / 8;
115 else
116 len = len / 8 + 1;
117 int[] temp = new int[len];
118
119 for (int i = 0; i != bitStr0.length(); i++) {
120 if (!(bitStr0.charAt(i) == '0' || bitStr0.charAt(i) == '1'))
121 throw new SMIMEException(this, 1007);
122 if ((i % 8) == 0)
123 temp[i / 8] = 0;
124 temp[i / 8] = temp[i / 8] + (int) ((byte) bitStr0.charAt(i) - 48) * (int) Math.pow((double) 2, (double) (7 - (i % 8)));
125 }
126 byte[] ret = new byte[temp.length];
127
128 for (int i = 0; i != ret.length; i++)
129 ret[i] = (byte) temp[i];
130 return ret;
131 }
132 }
133
This page was automatically generated by Maven