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