1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.IOException ; 21 import java.math.BigInteger ; 22 23 public class DERInteger 24 extends DERObject 25 { 26 byte[] bytes; 27 28 33 public static DERInteger getInstance( 34 Object obj) 35 { 36 if (obj == null || obj instanceof DERInteger) 37 { 38 return (DERInteger)obj; 39 } 40 41 if (obj instanceof ASN1OctetString) 42 { 43 return new DERInteger(((ASN1OctetString)obj).getOctets()); 44 } 45 46 if (obj instanceof ASN1TaggedObject) 47 { 48 return getInstance(((ASN1TaggedObject)obj).getObject()); 49 } 50 51 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 52 } 53 54 63 public static DERInteger getInstance( 64 ASN1TaggedObject obj, 65 boolean explicit) 66 { 67 return getInstance(obj.getObject()); 68 } 69 70 public DERInteger( 71 int value) 72 { 73 bytes = BigInteger.valueOf(value).toByteArray(); 74 } 75 76 public DERInteger( 77 BigInteger value) 78 { 79 bytes = value.toByteArray(); 80 } 81 82 public DERInteger( 83 byte[] bytes) 84 { 85 this.bytes = bytes; 86 } 87 88 public BigInteger getValue() 89 { 90 return new BigInteger (bytes); 91 } 92 93 97 public BigInteger getPositiveValue() 98 { 99 return new BigInteger (1, bytes); 100 } 101 102 void encode( 103 DEROutputStream out) 104 throws IOException 105 { 106 out.writeEncoded(INTEGER, bytes); 107 } 108 109 public int hashCode() 110 { 111 int value = 0; 112 113 for (int i = 0; i != bytes.length; i++) 114 { 115 value ^= (bytes[i] & 0xff) << (i % 4); 116 } 117 118 return value; 119 } 120 121 public boolean equals( 122 Object o) 123 { 124 if (o == null || !(o instanceof DERInteger)) 125 { 126 return false; 127 } 128 129 DERInteger other = (DERInteger)o; 130 131 if (bytes.length != other.bytes.length) 132 { 133 return false; 134 } 135 136 for (int i = 0; i != bytes.length; i++) 137 { 138 if (bytes[i] != other.bytes[i]) 139 { 140 return false; 141 } 142 } 143 144 return true; 145 } 146 } 147 | Popular Tags |