1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.math.BigInteger;
5
6 public class DERInteger
7 extends DERObject
8 {
9 byte[] bytes;
10
11
16 public static DERInteger getInstance(
17 Object obj)
18 {
19 if (obj == null || obj instanceof DERInteger)
20 {
21 return (DERInteger)obj;
22 }
23
24 if (obj instanceof ASN1OctetString)
25 {
26 return new DERInteger(((ASN1OctetString)obj).getOctets());
27 }
28
29 if (obj instanceof ASN1TaggedObject)
30 {
31 return getInstance(((ASN1TaggedObject)obj).getObject());
32 }
33
34 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
35 }
36
37
46 public static DERInteger getInstance(
47 ASN1TaggedObject obj,
48 boolean explicit)
49 {
50 return getInstance(obj.getObject());
51 }
52
53 public DERInteger(
54 int value)
55 {
56 bytes = BigInteger.valueOf(value).toByteArray();
57 }
58
59 public DERInteger(
60 BigInteger value)
61 {
62 bytes = value.toByteArray();
63 }
64
65 public DERInteger(
66 byte[] bytes)
67 {
68 this.bytes = bytes;
69 }
70
71 public BigInteger getValue()
72 {
73 return new BigInteger(bytes);
74 }
75
76
80 public BigInteger getPositiveValue()
81 {
82 return new BigInteger(1, bytes);
83 }
84
85 void encode(
86 DEROutputStream out)
87 throws IOException
88 {
89 out.writeEncoded(INTEGER, bytes);
90 }
91
92 public boolean equals(
93 Object o)
94 {
95 if (o == null || !(o instanceof DERInteger))
96 {
97 return false;
98 }
99
100 DERInteger other = (DERInteger)o;
101
102 if (bytes.length != other.bytes.length)
103 {
104 return false;
105 }
106
107 for (int i = 0; i != bytes.length; i++)
108 {
109 if (bytes[i] != other.bytes[i])
110 {
111 return false;
112 }
113 }
114
115 return true;
116 }
117 }
118