DERInteger.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.asn1; 2 3import java.io.IOException; 4import java.math.BigInteger; 5 6import org.bouncycastle.util.Arrays; 7 8/** 9 * Use ASN1Integer instead of this, 10 */ 11public class DERInteger 12 extends ASN1Primitive 13{ 14 byte[] bytes; 15 16 /** 17 * return an integer from the passed in object 18 * 19 * @exception IllegalArgumentException if the object cannot be converted. 20 */ 21 public static ASN1Integer getInstance( 22 Object obj) 23 { 24 if (obj == null || obj instanceof ASN1Integer) 25 { 26 return (ASN1Integer)obj; 27 } 28 if (obj instanceof DERInteger) 29 { 30 return new ASN1Integer((((DERInteger)obj).getValue())); 31 } 32 33 if (obj instanceof byte[]) 34 { 35 try 36 { 37 return (ASN1Integer)fromByteArray((byte[])obj); 38 } 39 catch (Exception e) 40 { 41 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString()); 42 } 43 } 44 45 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 46 } 47 48 /** 49 * return an Integer from a tagged object. 50 * 51 * @param obj the tagged object holding the object we want 52 * @param explicit true if the object is meant to be explicitly 53 * tagged false otherwise. 54 * @exception IllegalArgumentException if the tagged object cannot 55 * be converted. 56 */ 57 public static ASN1Integer getInstance( 58 ASN1TaggedObject obj, 59 boolean explicit) 60 { 61 ASN1Primitive o = obj.getObject(); 62 63 if (explicit || o instanceof DERInteger) 64 { 65 return getInstance(o); 66 } 67 else 68 { 69 return new ASN1Integer(ASN1OctetString.getInstance(obj.getObject()).getOctets()); 70 } 71 } 72 73 /** 74 * @deprecated use ASN1Integer constructor 75 */ 76 public DERInteger( 77 long value) 78 { 79 bytes = BigInteger.valueOf(value).toByteArray(); 80 } 81 82 /** 83 * @deprecated use ASN1Integer constructor 84 */ 85 public DERInteger( 86 BigInteger value) 87 { 88 bytes = value.toByteArray(); 89 } 90 91 /** 92 * @deprecated use ASN1Integer constructor 93 */ 94 public DERInteger( 95 byte[] bytes) 96 { 97 this.bytes = bytes; 98 } 99 100 public BigInteger getValue() 101 { 102 return new BigInteger(bytes); 103 } 104 105 /** 106 * in some cases positive values get crammed into a space, 107 * that's not quite big enough... 108 */ 109 public BigInteger getPositiveValue() 110 { 111 return new BigInteger(1, bytes); 112 } 113 114 boolean isConstructed() 115 { 116 return false; 117 } 118 119 int encodedLength() 120 { 121 return 1 + StreamUtil.calculateBodyLength(bytes.length) + bytes.length; 122 } 123 124 void encode( 125 ASN1OutputStream out) 126 throws IOException 127 { 128 out.writeEncoded(BERTags.INTEGER, bytes); 129 } 130 131 public int hashCode() 132 { 133 int value = 0; 134 135 for (int i = 0; i != bytes.length; i++) 136 { 137 value ^= (bytes[i] & 0xff) << (i % 4); 138 } 139 140 return value; 141 } 142 143 boolean asn1Equals( 144 ASN1Primitive o) 145 { 146 if (!(o instanceof DERInteger)) 147 { 148 return false; 149 } 150 151 DERInteger other = (DERInteger)o; 152 153 return Arrays.areEqual(bytes, other.bytes); 154 } 155 156 public String toString() 157 { 158 return getValue().toString(); 159 } 160} 161