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