diff options
Diffstat (limited to 'crypto/src/asn1/DERGenerator.cs')
-rw-r--r-- | crypto/src/asn1/DERGenerator.cs | 88 |
1 files changed, 37 insertions, 51 deletions
diff --git a/crypto/src/asn1/DERGenerator.cs b/crypto/src/asn1/DERGenerator.cs index 387a2c5f8..4bdc60972 100644 --- a/crypto/src/asn1/DERGenerator.cs +++ b/crypto/src/asn1/DERGenerator.cs @@ -11,16 +11,12 @@ namespace Org.BouncyCastle.Asn1 private bool _isExplicit; private int _tagNo; - protected DerGenerator( - Stream outStream) + protected DerGenerator(Stream outStream) : base(outStream) { } - protected DerGenerator( - Stream outStream, - int tagNo, - bool isExplicit) + protected DerGenerator(Stream outStream, int tagNo, bool isExplicit) : base(outStream) { _tagged = true; @@ -28,80 +24,70 @@ namespace Org.BouncyCastle.Asn1 _tagNo = tagNo; } - private static void WriteLength( - Stream outStr, - int length) + internal void WriteDerEncoded(int tag, byte[] bytes) { - if (length > 127) + if (_tagged) { - int size = 1; - int val = length; + int tagNum = _tagNo | Asn1Tags.ContextSpecific; - while ((val >>= 8) != 0) + if (_isExplicit) { - size++; + int newTag = _tagNo | Asn1Tags.Constructed | Asn1Tags.ContextSpecific; + MemoryStream bOut = new MemoryStream(); + WriteDerEncoded(bOut, tag, bytes); + WriteDerEncoded(OutStream, newTag, bOut.ToArray()); } - - outStr.WriteByte((byte)(size | 0x80)); - - for (int i = (size - 1) * 8; i >= 0; i -= 8) + else { - outStr.WriteByte((byte)(length >> i)); + if ((tag & Asn1Tags.Constructed) != 0) + { + tagNum |= Asn1Tags.Constructed; + } + + WriteDerEncoded(OutStream, tagNum, bytes); } } else { - outStr.WriteByte((byte)length); + WriteDerEncoded(OutStream, tag, bytes); } } - internal static void WriteDerEncoded( - Stream outStream, - int tag, - byte[] bytes) + internal static void WriteDerEncoded(Stream outStream, int tag, byte[] bytes) { - outStream.WriteByte((byte) tag); + outStream.WriteByte((byte)tag); WriteLength(outStream, bytes.Length); outStream.Write(bytes, 0, bytes.Length); } - internal void WriteDerEncoded( - int tag, - byte[] bytes) + internal static void WriteDerEncoded(Stream outStream, int tag, Stream inStream) { - if (_tagged) + WriteDerEncoded(outStream, tag, Streams.ReadAll(inStream)); + } + + private static void WriteLength(Stream outStream, int length) + { + if (length > 127) { - int tagNum = _tagNo | Asn1Tags.ContextSpecific; + int size = 1; + int val = length; - if (_isExplicit) + while ((val >>= 8) != 0) { - int newTag = _tagNo | Asn1Tags.Constructed | Asn1Tags.ContextSpecific; - MemoryStream bOut = new MemoryStream(); - WriteDerEncoded(bOut, tag, bytes); - WriteDerEncoded(Out, newTag, bOut.ToArray()); + size++; } - else - { - if ((tag & Asn1Tags.Constructed) != 0) - { - tagNum |= Asn1Tags.Constructed; - } - WriteDerEncoded(Out, tagNum, bytes); + outStream.WriteByte((byte)(size | 0x80)); + + for (int i = (size - 1) * 8; i >= 0; i -= 8) + { + outStream.WriteByte((byte)(length >> i)); } } else { - WriteDerEncoded(Out, tag, bytes); + outStream.WriteByte((byte)length); } } - - internal static void WriteDerEncoded( - Stream outStr, - int tag, - Stream inStr) - { - WriteDerEncoded(outStr, tag, Streams.ReadAll(inStr)); - } } } |