001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.util.asn1; 020 021 import java.io.IOException; 022 import java.math.BigInteger; 023 024 public class DERInteger 025 extends DERObject 026 { 027 byte[] bytes; 028 029 /** 030 * return an integer from the passed in object 031 * 032 * @exception IllegalArgumentException if the object cannot be converted. 033 */ 034 public static DERInteger getInstance( 035 Object obj) 036 { 037 if (obj == null || obj instanceof DERInteger) 038 { 039 return (DERInteger)obj; 040 } 041 042 if (obj instanceof ASN1OctetString) 043 { 044 return new DERInteger(((ASN1OctetString)obj).getOctets()); 045 } 046 047 if (obj instanceof ASN1TaggedObject) 048 { 049 return getInstance(((ASN1TaggedObject)obj).getObject()); 050 } 051 052 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 053 } 054 055 /** 056 * return an Integer from a tagged object. 057 * 058 * @param obj the tagged object holding the object we want 059 * @param explicit true if the object is meant to be explicitly 060 * tagged false otherwise. 061 * @exception IllegalArgumentException if the tagged object cannot 062 * be converted. 063 */ 064 public static DERInteger getInstance( 065 ASN1TaggedObject obj, 066 boolean explicit) 067 { 068 return getInstance(obj.getObject()); 069 } 070 071 public DERInteger( 072 int value) 073 { 074 bytes = BigInteger.valueOf(value).toByteArray(); 075 } 076 077 public DERInteger( 078 BigInteger value) 079 { 080 bytes = value.toByteArray(); 081 } 082 083 public DERInteger( 084 byte[] bytes) 085 { 086 this.bytes = bytes; 087 } 088 089 public BigInteger getValue() 090 { 091 return new BigInteger(bytes); 092 } 093 094 /** 095 * in some cases positive values get crammed into a space, 096 * that's not quite big enough... 097 */ 098 public BigInteger getPositiveValue() 099 { 100 return new BigInteger(1, bytes); 101 } 102 103 void encode( 104 DEROutputStream out) 105 throws IOException 106 { 107 out.writeEncoded(INTEGER, bytes); 108 } 109 110 public int hashCode() 111 { 112 int value = 0; 113 114 for (int i = 0; i != bytes.length; i++) 115 { 116 value ^= (bytes[i] & 0xff) << (i % 4); 117 } 118 119 return value; 120 } 121 122 public boolean equals( 123 Object o) 124 { 125 if (o == null || !(o instanceof DERInteger)) 126 { 127 return false; 128 } 129 130 DERInteger other = (DERInteger)o; 131 132 if (bytes.length != other.bytes.length) 133 { 134 return false; 135 } 136 137 for (int i = 0; i != bytes.length; i++) 138 { 139 if (bytes[i] != other.bytes[i]) 140 { 141 return false; 142 } 143 } 144 145 return true; 146 } 147 }