diff options
Diffstat (limited to 'crypto/src/bcpg')
-rw-r--r-- | crypto/src/bcpg/BcpgOutputStream.cs | 15 | ||||
-rw-r--r-- | crypto/src/bcpg/SignaturePacket.cs | 6 | ||||
-rw-r--r-- | crypto/src/bcpg/SignatureSubpacket.cs | 38 | ||||
-rw-r--r-- | crypto/src/bcpg/UserAttributeSubpacket.cs | 26 |
4 files changed, 52 insertions, 33 deletions
diff --git a/crypto/src/bcpg/BcpgOutputStream.cs b/crypto/src/bcpg/BcpgOutputStream.cs index 5b53bb28d..bbbe486ce 100644 --- a/crypto/src/bcpg/BcpgOutputStream.cs +++ b/crypto/src/bcpg/BcpgOutputStream.cs @@ -1,6 +1,7 @@ using System; using System.IO; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.IO; @@ -125,11 +126,18 @@ namespace Org.BouncyCastle.Bcpg } else { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> buf = stackalloc byte[5]; + buf[0] = 0xFF; + Pack.UInt32_To_BE((uint)bodyLen, buf, 1); + outStr.Write(buf); +#else outStr.WriteByte(0xff); outStr.WriteByte((byte)(bodyLen >> 24)); outStr.WriteByte((byte)(bodyLen >> 16)); outStr.WriteByte((byte)(bodyLen >> 8)); outStr.WriteByte((byte)bodyLen); +#endif } } @@ -169,11 +177,18 @@ namespace Org.BouncyCastle.Bcpg } else { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> buf = stackalloc byte[5]; + buf[0] = (byte)(hdr | 0x02); + Pack.UInt32_To_BE((uint)bodyLen, buf, 1); + this.Write(buf); +#else this.WriteByte((byte)(hdr | 0x02)); this.WriteByte((byte)(bodyLen >> 24)); this.WriteByte((byte)(bodyLen >> 16)); this.WriteByte((byte)(bodyLen >> 8)); this.WriteByte((byte)bodyLen); +#endif } } } diff --git a/crypto/src/bcpg/SignaturePacket.cs b/crypto/src/bcpg/SignaturePacket.cs index 305039e1c..ff3e5d3f9 100644 --- a/crypto/src/bcpg/SignaturePacket.cs +++ b/crypto/src/bcpg/SignaturePacket.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using Org.BouncyCastle.Bcpg.Sig; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Date; using Org.BouncyCastle.Utilities.IO; @@ -265,10 +266,7 @@ namespace Org.BouncyCastle.Bcpg byte[] trailer = new byte[5]; trailer[0] = (byte)signatureType; - trailer[1] = (byte)(time >> 24); - trailer[2] = (byte)(time >> 16); - trailer[3] = (byte)(time >> 8); - trailer[4] = (byte)(time ); + Pack.UInt32_To_BE((uint)time, trailer, 1); return trailer; } diff --git a/crypto/src/bcpg/SignatureSubpacket.cs b/crypto/src/bcpg/SignatureSubpacket.cs index f7f82b2a6..12c74bd6e 100644 --- a/crypto/src/bcpg/SignatureSubpacket.cs +++ b/crypto/src/bcpg/SignatureSubpacket.cs @@ -1,5 +1,7 @@ +using System; using System.IO; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg @@ -50,35 +52,31 @@ namespace Org.BouncyCastle.Bcpg { int bodyLen = data.Length + 1; - if (isLongLength) + if (isLongLength || bodyLen > 8383) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> buf = stackalloc byte[5]; + buf[0] = 0xFF; + Pack.UInt32_To_BE((uint)bodyLen, buf, 1); + os.Write(buf); +#else os.WriteByte(0xff); os.WriteByte((byte)(bodyLen >> 24)); os.WriteByte((byte)(bodyLen >> 16)); os.WriteByte((byte)(bodyLen >> 8)); os.WriteByte((byte)bodyLen); +#endif + } + else if (bodyLen < 192) + { + os.WriteByte((byte)bodyLen); } else { - if (bodyLen < 192) - { - os.WriteByte((byte)bodyLen); - } - else if (bodyLen <= 8383) - { - bodyLen -= 192; - - os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192)); - os.WriteByte((byte)bodyLen); - } - else - { - os.WriteByte(0xff); - os.WriteByte((byte)(bodyLen >> 24)); - os.WriteByte((byte)(bodyLen >> 16)); - os.WriteByte((byte)(bodyLen >> 8)); - os.WriteByte((byte)bodyLen); - } + bodyLen -= 192; + + os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192)); + os.WriteByte((byte)bodyLen); } if (critical) diff --git a/crypto/src/bcpg/UserAttributeSubpacket.cs b/crypto/src/bcpg/UserAttributeSubpacket.cs index 05f60ac17..246ac0759 100644 --- a/crypto/src/bcpg/UserAttributeSubpacket.cs +++ b/crypto/src/bcpg/UserAttributeSubpacket.cs @@ -1,6 +1,7 @@ using System; using System.IO; +using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Bcpg @@ -43,23 +44,30 @@ namespace Org.BouncyCastle.Bcpg { int bodyLen = data.Length + 1; - if (bodyLen < 192 && !longLength) + if (longLength || bodyLen > 8383) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span<byte> buf = stackalloc byte[5]; + buf[0] = 0xFF; + Pack.UInt32_To_BE((uint)bodyLen, buf, 1); + os.Write(buf); +#else + os.WriteByte(0xff); + os.WriteByte((byte)(bodyLen >> 24)); + os.WriteByte((byte)(bodyLen >> 16)); + os.WriteByte((byte)(bodyLen >> 8)); os.WriteByte((byte)bodyLen); +#endif } - else if (bodyLen <= 8383 && !longLength) + else if (bodyLen < 192) { - bodyLen -= 192; - - os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192)); os.WriteByte((byte)bodyLen); } else { - os.WriteByte(0xff); - os.WriteByte((byte)(bodyLen >> 24)); - os.WriteByte((byte)(bodyLen >> 16)); - os.WriteByte((byte)(bodyLen >> 8)); + bodyLen -= 192; + + os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192)); os.WriteByte((byte)bodyLen); } |