public static ReadOnlyMemory <byte> GetBitStringBytes(this AsnReader reader, Asn1Tag expectedTag) { if (reader.TryGetPrimitiveBitStringValue(expectedTag, out _, out ReadOnlyMemory <byte> contents)) { return(contents); } // Guaranteed too big, because it has the tag and length. int length = reader.PeekEncodedValue().Length; byte[] rented = ArrayPool <byte> .Shared.Rent(length); try { if (reader.TryCopyBitStringBytes(expectedTag, rented, out _, out int bytesWritten)) { return(new ReadOnlyMemory <byte>(rented.AsSpan(0, bytesWritten).ToArray())); } Debug.Fail("TryCopyBitStringBytes produced more data than the encoded size"); throw new CryptographicException(); } finally { Array.Clear(rented, 0, length); ArrayPool <byte> .Shared.Return(rented); } }