1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1;
20
21 import java.io.IOException;
22 import java.math.BigInteger;
23
24 public class DERInteger
25 extends DERObject
26 {
27 byte[] bytes;
28
29 /**
30 * return an integer from the passed in object
31 *
32 * @exception IllegalArgumentException if the object cannot be converted.
33 */
34 public static DERInteger getInstance(
35 Object obj)
36 {
37 if (obj == null || obj instanceof DERInteger)
38 {
39 return (DERInteger)obj;
40 }
41
42 if (obj instanceof ASN1OctetString)
43 {
44 return new DERInteger(((ASN1OctetString)obj).getOctets());
45 }
46
47 if (obj instanceof ASN1TaggedObject)
48 {
49 return getInstance(((ASN1TaggedObject)obj).getObject());
50 }
51
52 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
53 }
54
55 /**
56 * return an Integer from a tagged object.
57 *
58 * @param obj the tagged object holding the object we want
59 * @param explicit true if the object is meant to be explicitly
60 * tagged false otherwise.
61 * @exception IllegalArgumentException if the tagged object cannot
62 * be converted.
63 */
64 public static DERInteger getInstance(
65 ASN1TaggedObject obj,
66 boolean explicit)
67 {
68 return getInstance(obj.getObject());
69 }
70
71 public DERInteger(
72 int value)
73 {
74 bytes = BigInteger.valueOf(value).toByteArray();
75 }
76
77 public DERInteger(
78 BigInteger value)
79 {
80 bytes = value.toByteArray();
81 }
82
83 public DERInteger(
84 byte[] bytes)
85 {
86 this.bytes = bytes;
87 }
88
89 public BigInteger getValue()
90 {
91 return new BigInteger(bytes);
92 }
93
94 /**
95 * in some cases positive values get crammed into a space,
96 * that's not quite big enough...
97 */
98 public BigInteger getPositiveValue()
99 {
100 return new BigInteger(1, bytes);
101 }
102
103 void encode(
104 DEROutputStream out)
105 throws IOException
106 {
107 out.writeEncoded(INTEGER, bytes);
108 }
109
110 public int hashCode()
111 {
112 int value = 0;
113
114 for (int i = 0; i != bytes.length; i++)
115 {
116 value ^= (bytes[i] & 0xff) << (i % 4);
117 }
118
119 return value;
120 }
121
122 public boolean equals(
123 Object o)
124 {
125 if (o == null || !(o instanceof DERInteger))
126 {
127 return false;
128 }
129
130 DERInteger other = (DERInteger)o;
131
132 if (bytes.length != other.bytes.length)
133 {
134 return false;
135 }
136
137 for (int i = 0; i != bytes.length; i++)
138 {
139 if (bytes[i] != other.bytes[i])
140 {
141 return false;
142 }
143 }
144
145 return true;
146 }
147 }