1 module hunt.security.x509.SerialNumber; 2 3 import hunt.security.util.DerValue; 4 import hunt.security.util.DerInputStream; 5 import hunt.security.util.DerOutputStream; 6 7 import hunt.stream.Common; 8 import hunt.Exceptions; 9 10 import std.bigint; 11 import std.format; 12 13 alias BigInteger = BigInt; 14 15 /** 16 * This class defines the SerialNumber class used by certificates. 17 * 18 * @author Amit Kapoor 19 * @author Hemma Prafullchandra 20 */ 21 class SerialNumber { 22 private BigInteger serialNum; 23 24 // Construct the class from the DerValue 25 private void construct(DerValue derVal) { 26 serialNum = derVal.getBigInteger(); 27 implementationMissing(); 28 // if (derVal.data.available() != 0) { 29 // throw new IOException("Excess SerialNumber data"); 30 // } 31 } 32 33 /** 34 * The default constructor for this class using BigInteger. 35 * 36 * @param num the BigInteger number used to create the serial number. 37 */ 38 this(BigInteger num) { 39 serialNum = num; 40 } 41 42 /** 43 * The default constructor for this class using int. 44 * 45 * @param num the BigInteger number used to create the serial number. 46 */ 47 this(int num) { 48 serialNum = BigInteger(num); 49 } 50 51 /** 52 * Create the object, decoding the values from the passed DER stream. 53 * 54 * @param in the DerInputStream to read the SerialNumber from. 55 * @exception IOException on decoding errors. 56 */ 57 this(DerInputStream stream) { 58 DerValue derVal = stream.getDerValue(); 59 construct(derVal); 60 } 61 62 /** 63 * Create the object, decoding the values from the passed DerValue. 64 * 65 * @param val the DerValue to read the SerialNumber from. 66 * @exception IOException on decoding errors. 67 */ 68 this(DerValue val) { 69 construct(val); 70 } 71 72 /** 73 * Create the object, decoding the values from the passed stream. 74 * 75 * @param stream the InputStream to read the SerialNumber from. 76 * @exception IOException on decoding errors. 77 */ 78 this(InputStream stream) { 79 DerValue derVal = new DerValue(stream); 80 construct(derVal); 81 } 82 83 /** 84 * Return the SerialNumber as user readable string. 85 */ 86 override string toString() { 87 // return ("SerialNumber: [" ~ Debug.toHexString(serialNum) ~ "]"); 88 return ("SerialNumber: [" ~ format("%02X", serialNum) ~ "]"); 89 } 90 91 /** 92 * Encode the SerialNumber in DER form to the stream. 93 * 94 * @param stream the DerOutputStream to marshal the contents to. 95 * @exception IOException on errors. 96 */ 97 void encode(DerOutputStream stream) { 98 stream.putInteger(serialNum.toInt()); 99 } 100 101 /** 102 * Return the serial number. 103 */ 104 BigInteger getNumber() { 105 return serialNum; 106 } 107 }