diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-16 22:37:52 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-16 22:37:52 +0700 |
commit | be15049124860ccb7335b92215e8b8dfa1821bf9 (patch) | |
tree | 7a86ed2110021307465a617076d601ca812c69ed /crypto/src/asn1/DerBitString.cs | |
parent | Make cast more specific (diff) | |
download | BouncyCastle.NET-ed25519-be15049124860ccb7335b92215e8b8dfa1821bf9.tar.xz |
ASN.1: Staged encoding
Diffstat (limited to 'crypto/src/asn1/DerBitString.cs')
-rw-r--r-- | crypto/src/asn1/DerBitString.cs | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/crypto/src/asn1/DerBitString.cs b/crypto/src/asn1/DerBitString.cs index 2814b9677..375abb479 100644 --- a/crypto/src/asn1/DerBitString.cs +++ b/crypto/src/asn1/DerBitString.cs @@ -216,34 +216,37 @@ namespace Org.BouncyCastle.Asn1 } } - internal override bool EncodeConstructed(int encoding) + internal override IAsn1Encoding GetEncoding(int encoding) { - return false; - } + int padBits = contents[0]; + if (padBits != 0) + { + int last = contents.Length - 1; + byte lastBer = contents[last]; + byte lastDer = (byte)(lastBer & (0xFF << padBits)); - internal override int EncodedLength(int encoding, bool withID) - { - return Asn1OutputStream.GetLengthOfEncodingDL(withID, contents.Length); + if (lastBer != lastDer) + return new PrimitiveEncodingSuffixed(Asn1Tags.Universal, Asn1Tags.BitString, contents, lastDer); + } + + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.BitString, contents); } - internal override void Encode(Asn1OutputStream asn1Out, bool withID) - { + internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) + { int padBits = contents[0]; - int length = contents.Length; - int last = length - 1; - - byte lastOctet = contents[last]; - byte lastOctetDer = (byte)(contents[last] & (0xFF << padBits)); - - if (lastOctet == lastOctetDer) - { - asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, contents); - } - else + if (padBits != 0) { - asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, contents, 0, last, lastOctetDer); + int last = contents.Length - 1; + byte lastBer = contents[last]; + byte lastDer = (byte)(lastBer & (0xFF << padBits)); + + if (lastBer != lastDer) + return new PrimitiveEncodingSuffixed(tagClass, tagNo, contents, lastDer); } - } + + return new PrimitiveEncoding(tagClass, tagNo, contents); + } protected override int Asn1GetHashCode() { @@ -253,11 +256,11 @@ namespace Org.BouncyCastle.Asn1 int padBits = contents[0]; int last = contents.Length - 1; - byte lastOctetDer = (byte)(contents[last] & (0xFF << padBits)); + byte lastDer = (byte)(contents[last] & (0xFF << padBits)); int hc = Arrays.GetHashCode(contents, 0, last); hc *= 257; - hc ^= lastOctetDer; + hc ^= lastDer; return hc; } @@ -283,10 +286,10 @@ namespace Org.BouncyCastle.Asn1 } int padBits = thisContents[0]; - byte thisLastOctetDer = (byte)(thisContents[last] & (0xFF << padBits)); - byte thatLastOctetDer = (byte)(thatContents[last] & (0xFF << padBits)); + byte thisLastDer = (byte)(thisContents[last] & (0xFF << padBits)); + byte thatLastDer = (byte)(thatContents[last] & (0xFF << padBits)); - return thisLastOctetDer == thatLastOctetDer; + return thisLastDer == thatLastDer; } public override string GetString() @@ -306,21 +309,6 @@ namespace Org.BouncyCastle.Asn1 return buffer.ToString(); } - internal static int EncodedLength(bool withID, int contentsLength) - { - return Asn1OutputStream.GetLengthOfEncodingDL(withID, contentsLength); - } - - internal static void Encode(Asn1OutputStream asn1Out, bool withID, byte[] buf, int off, int len) - { - asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, buf, off, len); - } - - internal static void Encode(Asn1OutputStream asn1Out, bool withID, byte pad, byte[] buf, int off, int len) - { - asn1Out.WriteEncodingDL(withID, Asn1Tags.BitString, pad, buf, off, len); - } - internal static DerBitString CreatePrimitive(byte[] contents) { int length = contents.Length; |