1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 import java.math.BigInteger ; 24 25 public class DERInteger 26 extends DERObject 27 { 28 byte[] bytes; 29 30 35 public static DERInteger getInstance( 36 Object 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 ("illegal object in getInstance: " + obj.getClass().getName()); 54 } 55 56 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 value) 80 { 81 bytes = value.toByteArray(); 82 } 83 84 public DERInteger( 85 byte[] bytes) 86 { 87 this.bytes = bytes; 88 } 89 90 public BigInteger getValue() 91 { 92 return new BigInteger (bytes); 93 } 94 95 99 public BigInteger getPositiveValue() 100 { 101 return new BigInteger (1, bytes); 102 } 103 104 void encode( 105 DEROutputStream out) 106 throws IOException 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 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 |