KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERInteger


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.IOException JavaDoc;
23 import java.math.BigInteger JavaDoc;
24
25 public class DERInteger
26     extends DERObject
27 {
28     byte[] bytes;
29
30     /**
31      * return an integer from the passed in object
32      *
33      * @exception IllegalArgumentException if the object cannot be converted.
34      */

35     public static DERInteger getInstance(
36         Object JavaDoc obj)
37     {
38         if (obj == null || obj instanceof DERInteger)
39         {
40             return (DERInteger)obj;
41         }
42
43         if (obj instanceof ASN1OctetString)
44         {
45             return new DERInteger(((ASN1OctetString)obj).getOctets());
46         }
47
48         if (obj instanceof ASN1TaggedObject)
49         {
50             return getInstance(((ASN1TaggedObject)obj).getObject());
51         }
52
53         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
54     }
55
56     /**
57      * return an Integer from a tagged object.
58      *
59      * @param obj the tagged object holding the object we want
60      * @param explicit true if the object is meant to be explicitly
61      * tagged false otherwise.
62      * @exception IllegalArgumentException if the tagged object cannot
63      * be converted.
64      */

65     public static DERInteger getInstance(
66         ASN1TaggedObject obj,
67         boolean explicit)
68     {
69         return getInstance(obj.getObject());
70     }
71
72     public DERInteger(
73         int value)
74     {
75         bytes = BigInteger.valueOf(value).toByteArray();
76     }
77
78     public DERInteger(
79         BigInteger JavaDoc value)
80     {
81         bytes = value.toByteArray();
82     }
83
84     public DERInteger(
85         byte[] bytes)
86     {
87         this.bytes = bytes;
88     }
89
90     public BigInteger JavaDoc getValue()
91     {
92         return new BigInteger JavaDoc(bytes);
93     }
94
95     /**
96      * in some cases positive values get crammed into a space,
97      * that's not quite big enough...
98      */

99     public BigInteger JavaDoc getPositiveValue()
100     {
101         return new BigInteger JavaDoc(1, bytes);
102     }
103
104     void encode(
105         DEROutputStream out)
106         throws IOException JavaDoc
107     {
108         out.writeEncoded(INTEGER, bytes);
109     }
110
111     public int hashCode()
112     {
113          int value = 0;
114
115          for (int i = 0; i != bytes.length; i++)
116          {
117              value ^= (bytes[i] & 0xff) << (i % 4);
118          }
119
120          return value;
121     }
122
123     public boolean equals(
124         Object JavaDoc o)
125     {
126         if (o == null || !(o instanceof DERInteger))
127         {
128             return false;
129         }
130
131         DERInteger other = (DERInteger)o;
132
133         if (bytes.length != other.bytes.length)
134         {
135             return false;
136         }
137
138         for (int i = 0; i != bytes.length; i++)
139         {
140             if (bytes[i] != other.bytes[i])
141             {
142                 return false;
143             }
144         }
145
146         return true;
147     }
148 }
149
Popular Tags