DEREnumerated.java revision c37f4a04ef89e73a39a59f3c5a179af8c8ab5974
1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1; 2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.Arrays; 4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException; 6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.math.BigInteger; 7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DEREnumerated 9c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom extends ASN1Object 10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{ 11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] bytes; 12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return an integer from the passed in object 15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the object cannot be converted. 17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DEREnumerated getInstance( 19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam Object obj) 20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj == null || obj instanceof DEREnumerated) 22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return (DEREnumerated)obj; 24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj instanceof ASN1OctetString) 27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return new DEREnumerated(((ASN1OctetString)obj).getOctets()); 29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (obj instanceof ASN1TaggedObject) 32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return getInstance(((ASN1TaggedObject)obj).getObject()); 34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam /** 40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * return an Enumerated from a tagged object. 41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * 42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param obj the tagged object holding the object we want 43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @param explicit true if the object is meant to be explicitly 44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * tagged false otherwise. 45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * @exception IllegalArgumentException if the tagged object cannot 46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * be converted. 47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */ 48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public static DEREnumerated getInstance( 49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam ASN1TaggedObject obj, 50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam boolean explicit) 51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return getInstance(obj.getObject()); 53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DEREnumerated( 56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam int value) 57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bytes = BigInteger.valueOf(value).toByteArray(); 59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DEREnumerated( 62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam BigInteger value) 63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam bytes = value.toByteArray(); 65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public DEREnumerated( 68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam byte[] bytes) 69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam this.bytes = bytes; 71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public BigInteger getValue() 74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return new BigInteger(bytes); 76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam void encode( 79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DEROutputStream out) 80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam throws IOException 81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam out.writeEncoded(ENUMERATED, bytes); 83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom boolean asn1Equals( 86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom DERObject o) 87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam if (!(o instanceof DEREnumerated)) 89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam return false; 91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam DEREnumerated other = (DEREnumerated)o; 94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam 95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return Arrays.areEqual(this.bytes, other.bytes); 96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom 98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam public int hashCode() 99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam { 100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom return Arrays.hashCode(bytes); 101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam } 102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam} 103