public static ReadOnlyMemory <byte> GetOctetStringBytes(this AsnReader reader, Asn1Tag expectedTag) { if (reader.TryGetPrimitiveOctetStringBytes(expectedTag, 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.TryCopyOctetStringBytes(expectedTag, rented, out int bytesWritten)) { return(new ReadOnlyMemory <byte>(rented.AsSpan(0, bytesWritten).ToArray())); } Debug.Fail("TryCopyOctetStringBytes produced more data than the encoded size"); throw new CryptographicException(); } finally { Array.Clear(rented, 0, length); ArrayPool <byte> .Shared.Return(rented); } }