KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > util > DERLengthSearcher


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.util;
10
11 import java.math.BigInteger JavaDoc;
12
13
14 /**
15  * DERLengthSearcher is used for searching the content length of inner DER object,
16  * and for counting the number of length octets in that DER object. This data
17  * can be used later for counting length of the whole inner DER encoded
18  * object as:<BR>
19  * 1 + number of length octets + number of content octets
20  */

21 public class DERLengthSearcher
22 {
23
24 /**
25  * Number of content octets for specified DER object
26  */

27   private int lengthContent;
28
29 /**
30  * Number of length octets for specified DER object
31  */

32   private int lengthLength;
33
34 /**
35  * Construction with the position (index) of the inner DER encoded object,
36  * which length is required, and with the structured outer DER encoded object
37  * represented as byte array
38  * @param startElement0 position in byte array where inner DER object begines
39  * (Start element is always DER object type identifier)
40  * @param array0 structured DER encoded object in which is counted the length
41  * of particular inner DER object
42  */

43   public DERLengthSearcher (int startElement0, byte[] array0) {
44     this.newInitialization(startElement0, array0);
45   }
46
47 /**
48  * Initialization of the next length searching
49  * @param startElement0 starting element for next searching
50  * @param array0 structured DER encoded object in which the length of the
51  * particular iner DER object is being counted
52  */

53   public void newInitialization (int startElement0, byte[] array0)
54   // Start element is DER object type identifier
55
{
56     int numBytes;
57     byte[] temp;
58     if ((array0[startElement0 + 1] & 128) == 128) // If long form of length octets is present
59
{
60       numBytes = array0[startElement0 + 1] & 0x7F;
61       if ((array0[startElement0 + 2] & 128) == 128) // For BigInteger (for example) 0x80 is negativ number (positiv must be written like 0x0080)
62
{
63         temp = new byte[numBytes + 1];
64         temp[0] = 0;
65         for (int i = 0; i != numBytes; i++)
66           temp[i + 1] = array0[startElement0 + 2 + i];
67       }
68       else {
69         temp = new byte[numBytes];
70         for (int i = 0; i != numBytes; i++)
71           temp[i] = array0[startElement0 + 2 + i];
72       }
73       lengthContent = new BigInteger JavaDoc(temp).intValue();
74       lengthLength = numBytes + 1;
75     }
76     else {
77       lengthContent = array0[startElement0 + 1];
78       lengthLength = 1;
79     }
80   }
81
82 /**
83  * Returns the number of the content octets in particular DER encoded object
84  * @return Number of content octets
85  */

86   public int getLengthtDERContentPart () {
87     return lengthContent;
88   }
89
90 /**
91  * Returns the number of the length octets in particular DER encoded object
92  * @return Number of length octets
93  */

94   public int getLengthtDERLengthPart () {
95     return lengthLength;
96   }
97 }
98
99
100
101
Popular Tags