@@ -12,7 +12,7 @@ public class DerData
1212
1313 private const byte Boolean = 0x01 ;
1414 private const byte Integer = 0x02 ;
15- // private const byte BITSTRING = 0x03;
15+ private const byte BITSTRING = 0x03 ;
1616 private const byte Octetstring = 0x04 ;
1717 private const byte Null = 0x05 ;
1818 private const byte Objectidentifier = 0x06 ;
@@ -101,7 +101,7 @@ public BigInteger ReadBigInteger()
101101 {
102102 var type = ReadByte ( ) ;
103103 if ( type != Integer )
104- throw new InvalidOperationException ( "Invalid data type, INTEGER(02) is expected." ) ;
104+ throw new InvalidOperationException ( string . Format ( "Invalid data type, INTEGER(02) is expected, got {0}" , type ) ) ;
105105
106106 var length = ReadLength ( ) ;
107107
@@ -118,7 +118,7 @@ public int ReadInteger()
118118 {
119119 var type = ReadByte ( ) ;
120120 if ( type != Integer )
121- throw new InvalidOperationException ( "Invalid data type, INTEGER(02) is expected." ) ;
121+ throw new InvalidOperationException ( string . Format ( "Invalid data type, INTEGER(02) is expected, got {0}" , type ) ) ;
122122
123123 var length = ReadLength ( ) ;
124124
@@ -140,6 +140,36 @@ public int ReadInteger()
140140 return result ;
141141 }
142142
143+ /// <summary>
144+ /// Reads next octetstring data type from internal buffer.
145+ /// </summary>
146+ /// <returns>data read.</returns>
147+ public byte [ ] ReadOctetString ( )
148+ {
149+ var type = ReadByte ( ) ;
150+ if ( type != Octetstring )
151+ throw new InvalidOperationException ( string . Format ( "Invalid data type, OCTETSTRING(04) is expected, got {0}" , type ) ) ;
152+
153+ var length = ReadLength ( ) ;
154+ var data = ReadBytes ( length ) ;
155+ return data ;
156+ }
157+
158+ /// <summary>
159+ /// Reads next object data type from internal buffer.
160+ /// </summary>
161+ /// <returns>data read.</returns>
162+ public byte [ ] ReadObject ( )
163+ {
164+ var type = ReadByte ( ) ;
165+ if ( type != Objectidentifier )
166+ throw new InvalidOperationException ( string . Format ( "Invalid data type, OBJECT(06) is expected, got {0}" , type ) ) ;
167+
168+ var length = ReadLength ( ) ;
169+ var data = ReadBytes ( length ) ;
170+ return data ;
171+ }
172+
143173 /// <summary>
144174 /// Writes BOOLEAN data into internal buffer.
145175 /// </summary>
@@ -189,6 +219,18 @@ public void Write(byte[] data)
189219 WriteBytes ( data ) ;
190220 }
191221
222+ /// <summary>
223+ /// Writes BITSTRING data into internal buffer.
224+ /// </summary>
225+ /// <param name="data">The data.</param>
226+ public void WriteBitstring ( byte [ ] data )
227+ {
228+ _data . Add ( BITSTRING ) ;
229+ var length = GetLength ( data . Length ) ;
230+ WriteBytes ( length ) ;
231+ WriteBytes ( data ) ;
232+ }
233+
192234 /// <summary>
193235 /// Writes OBJECTIDENTIFIER data into internal buffer.
194236 /// </summary>
@@ -229,6 +271,18 @@ public void Write(ObjectIdentifier identifier)
229271 WriteBytes ( bytes ) ;
230272 }
231273
274+ /// <summary>
275+ /// Writes OBJECTIDENTIFIER data into internal buffer.
276+ /// </summary>
277+ /// <param name="bytes">The bytes.</param>
278+ public void WriteObjectIdentifier ( byte [ ] bytes )
279+ {
280+ _data . Add ( Objectidentifier ) ;
281+ var length = GetLength ( bytes . Length ) ;
282+ WriteBytes ( length ) ;
283+ WriteBytes ( bytes ) ;
284+ }
285+
232286 /// <summary>
233287 /// Writes NULL data into internal buffer.
234288 /// </summary>
@@ -270,8 +324,11 @@ private static IEnumerable<byte> GetLength(int length)
270324 }
271325 return new [ ] { ( byte ) length } ;
272326 }
273-
274- private int ReadLength ( )
327+ /// <summary>
328+ /// Gets Data Length
329+ /// </summary>
330+ /// <returns>length</returns>
331+ public int ReadLength ( )
275332 {
276333 int length = ReadByte ( ) ;
277334
@@ -306,20 +363,32 @@ private int ReadLength()
306363 return length ;
307364 }
308365
309- private void WriteBytes ( IEnumerable < byte > data )
366+ /// <summary>
367+ /// Write Byte data into internal buffer.
368+ /// </summary>
369+ public void WriteBytes ( IEnumerable < byte > data )
310370 {
311371 _data . AddRange ( data ) ;
312372 }
313373
314- private byte ReadByte ( )
374+ /// <summary>
375+ /// Reads Byte data into internal buffer.
376+ /// </summary>
377+ /// <returns>data read</returns>
378+ public byte ReadByte ( )
315379 {
316380 if ( _readerIndex > _data . Count )
317381 throw new InvalidOperationException ( "Read out of boundaries." ) ;
318382
319383 return _data [ _readerIndex ++ ] ;
320384 }
321385
322- private byte [ ] ReadBytes ( int length )
386+ /// <summary>
387+ /// Reads lengths Bytes data into internal buffer.
388+ /// </summary>
389+ /// <returns>data read</returns>
390+ /// <param name="length">amount of data to read.</param>
391+ public byte [ ] ReadBytes ( int length )
323392 {
324393 if ( _readerIndex + length > _data . Count )
325394 throw new InvalidOperationException ( "Read out of boundaries." ) ;
0 commit comments