Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Formats.​Asn1.​AsnWriter
Assembly: System.Formats.Asn1
Inheritance: object → AsnWriter
A writer for BER-encoded, CER-encoded, and DER-encoded ASN.1 data.
Properties
public
AsnEncodingRules
RuleSet
Gets the encoding rules in use by this writer.
Methods
public
void
CopyTo​(AsnWriter destination)
Copies the value of this writer into another.
destination
The writer to receive the value.
public
byte[]
Encode​()
Returns a new array containing the encoded value.
Returns A precisely-sized array containing the encoded value.
public
int
Encode​(Span<​byte> destination)
Writes the encoded representation of the data to <paramref name="destination" /> .
Returns The number of bytes written to <paramref name="destination" /> .
destination
The buffer in which to write.
public
bool
EncodedValueEquals​(AsnWriter other)
Determines if <see cref="M:System.Formats.Asn1.AsnWriter.Encode" /> would produce an output identical to
<paramref name="other" /> .
Returns <see langword="true" /> if the pending encoded data is identical to <paramref name="other" /> ,
<see langword="false" /> otherwise.
public
bool
EncodedValueEquals​(ReadOnlySpan<​byte> other)
Determines if <see cref="M:System.Formats.Asn1.AsnWriter.Encode" /> would produce an output identical to
<paramref name="other" /> .
Returns <see langword="true" /> if the pending encoded data is identical to <paramref name="other" /> ,
<see langword="false" /> otherwise.
public
TReturn
Encode​(Func<​ReadOnlySpan<​byte>, TReturn> encodeCallback)
Provides the encoded representation of the data to the specified callback.
Returns Returns the value returned from <paramref name="encodeCallback" /> .
encodeCallback
The callback that receives the encoded data.
public
void
Encode​(TState state,
Action<​TState, ReadOnlySpan<​byte>> encodeCallback)
Provides the encoded representation of the data to the specified callback.
state
The state to pass to <paramref name="encodeCallback" /> .
encodeCallback
The callback that receives the encoded data.
public
TReturn
Encode​(TState state,
Func<​TState, ReadOnlySpan<​byte>, TReturn> encodeCallback)
Provides the encoded representation of the data to the specified callback.
Returns Returns the value returned from <paramref name="encodeCallback" /> .
state
The state to pass to <paramref name="encodeCallback" /> .
encodeCallback
The callback that receives the encoded data.
public
int
GetEncodedLength​()
Gets the number of bytes that would be written by <see cref="M:System.Formats.Asn1.AsnWriter.TryEncode(System.Span{System.Byte},System.Int32@)" /> .
Returns The number of bytes that would be written by <see cref="M:System.Formats.Asn1.AsnWriter.TryEncode(System.Span{System.Byte},System.Int32@)" /> .
public
void
PopOctetString​(Asn1Tag? tag = null)
Indicate that the open Octet String with the tag UNIVERSAL 4 is closed,
returning the writer to the parent context.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 4).
Remarks
In <see cref="F:System.Formats.Asn1.AsnEncodingRules.BER" /> and <see cref="F:System.Formats.Asn1.AsnEncodingRules.DER" /> modes,
the encoded contents will remain in a single primitive Octet String.
In <see cref="F:System.Formats.Asn1.AsnEncodingRules.CER" /> mode, the contents will be broken up into
multiple segments, when required.
public
void
PopSequence​(Asn1Tag? tag = null)
Indicate that the open Sequence with the specified tag is closed,
returning the writer to the parent context.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 16).
public
void
PopSetOf​(Asn1Tag? tag = null)
Indicate that the open Set-Of with the specified tag is closed,
returning the writer to the parent context.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 17).
Remarks
In <see cref="F:System.Formats.Asn1.AsnEncodingRules.CER" /> and <see cref="F:System.Formats.Asn1.AsnEncodingRules.DER" /> modes,
the writer will sort the Set-Of elements when the tag is closed.
public
Scope
PushOctetString​(Asn1Tag? tag = null)
Begin writing an Octet String value with a specified tag.
Returns A disposable value which will automatically call <see cref="M:System.Formats.Asn1.AsnWriter.PopOctetString(System.Nullable{System.Formats.Asn1.Asn1Tag})" /> .
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 4).
Remarks
This method is just an accelerator for writing an Octet String value where the
contents are also ASN.1 data encoded under the same encoding system.
When <see cref="M:System.Formats.Asn1.AsnWriter.PopOctetString(System.Nullable{System.Formats.Asn1.Asn1Tag})" /> is called the entire nested contents are
normalized as a single Octet String value, encoded correctly for the current encoding
rules.
This method does not necessarily create a Constructed encoding, and it is not invalid to
write values other than Octet String inside this Push/Pop.
public
Scope
PushSequence​(Asn1Tag? tag = null)
Begin writing a Sequence with a specified tag.
Returns A disposable value which will automatically call <see cref="M:System.Formats.Asn1.AsnWriter.PopSequence(System.Nullable{System.Formats.Asn1.Asn1Tag})" /> .
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 16).
public
Scope
PushSetOf​(Asn1Tag? tag = null)
Begin writing a Set-Of with a specified tag.
Returns A disposable value which will automatically call <see cref="M:System.Formats.Asn1.AsnWriter.PopSetOf(System.Nullable{System.Formats.Asn1.Asn1Tag})" /> .
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 17).
Remarks
In <see cref="F:System.Formats.Asn1.AsnEncodingRules.CER" /> and <see cref="F:System.Formats.Asn1.AsnEncodingRules.DER" /> modes,
the writer will sort the Set-Of elements when the tag is closed.
public
void
Reset​()
Resets the writer to have no data, without releasing resources.
public
void
WriteBitString​(ReadOnlySpan<​byte> value,
int unusedBitCount = 0,
Asn1Tag? tag = null)
Write a Bit String value with a specified tag.
value
The value to write.
unusedBitCount
The number of trailing bits which are not semantic.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 3).
public
void
WriteBoolean​(bool value,
Asn1Tag? tag = null)
Write a Boolean value with a specified tag.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 1).
public
void
WriteCharacterString​(UniversalTagNumber encodingType,
ReadOnlySpan<​char> str,
Asn1Tag? tag = null)
Write the provided string using the specified encoding type using the specified
tag corresponding to the encoding type.
encodingType
One of the enumeration values representing the encoding to use.
str
The string to write.
tag
The tag to write, or <see langword="null" /> for the universal tag that is appropriate to
the requested encoding type.
public
void
WriteCharacterString​(UniversalTagNumber encodingType,
string value,
Asn1Tag? tag = null)
Write the provided string using the specified encoding type using the specified
tag corresponding to the encoding type.
encodingType
One of the enumeration values representing the encoding to use.
value
The string to write.
tag
The tag to write, or <see langword="null" /> for the universal tag that is appropriate to
the requested encoding type.
public
void
WriteEncodedValue​(ReadOnlySpan<​byte> value)
Writes a single value that has already been encoded.
value
The value to write.
Remarks
This method only checks that the tag and length are encoded according to the current ruleset,
and that the end of the value is the end of the input. The contents are not evaluated for
semantic meaning.
public
void
WriteEnumeratedValue​(Enum value,
Asn1Tag? tag = null)
Write a non-[ <see cref="T:System.FlagsAttribute" /> ] enum value as an Enumerated with
tag UNIVERSAL 10.
value
The boxed enumeration value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 10).
public
void
WriteEnumeratedValue​(TEnum value,
Asn1Tag? tag = null)
Write a non-[ <see cref="T:System.FlagsAttribute" /> ] enum value as an Enumerated with
tag UNIVERSAL 10.
value
The boxed enumeration value to write.
tag
The tag to write.
public
void
WriteGeneralizedTime​(DateTimeOffset value,
bool omitFractionalSeconds = false,
Asn1Tag? tag = null)
Write the provided <see cref="T:System.DateTimeOffset" /> as a GeneralizedTime with a specified
UNIVERSAL 24, optionally excluding the fractional seconds.
value
The value to write.
omitFractionalSeconds
<see langword="true" /> to treat the fractional seconds in <paramref name="value" /> as 0 even if
a non-zero value is present.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 24).
public
void
WriteInteger​(long value,
Asn1Tag? tag = null)
Write an Integer value with a specified tag.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 2).
public
void
WriteInteger​(Numerics.​BigInteger value,
Asn1Tag? tag = null)
Write an Integer value with a specified tag.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 2).
public
void
WriteInteger​(ReadOnlySpan<​byte> value,
Asn1Tag? tag = null)
Write an Integer value with a specified tag.
value
The integer value to write, in signed big-endian byte order.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 2).
public
void
WriteInteger​(ulong value,
Asn1Tag? tag = null)
Write an Integer value with a specified tag.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 2).
public
void
WriteIntegerUnsigned​(ReadOnlySpan<​byte> value,
Asn1Tag? tag = null)
Write an Integer value with a specified tag.
value
The integer value to write, in unsigned big-endian byte order.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 2).
public
void
WriteNamedBitList​(Collections.​BitArray value,
Asn1Tag? tag = null)
Write a bit array value as a NamedBitList with a specified tag.
value
The bits to write
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 3).
Remarks
The index of the bit array corresponds to the bit number in the encoded format, which is
different than the value produced by <see cref="M:System.Collections.BitArray.CopyTo(System.Array,System.Int32)" /> with a byte array.
For example, the bit array <c>{ false, true, true }</c> encodes as <c>0b0110_0000</c> with 5
unused bits.
public
void
WriteNamedBitList​(Enum value,
Asn1Tag? tag = null)
Write a [ <see cref="T:System.FlagsAttribute" /> ] enum value as a NamedBitList with
a specified tag.
value
The boxed enumeration value to write
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 3).
public
void
WriteNamedBitList​(TEnum value,
Asn1Tag? tag = null)
Write a [ <see cref="T:System.FlagsAttribute" /> ] enum value as a NamedBitList with
a specified tag.
value
The enumeration value to write
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 3).
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 5).
public
void
WriteObjectIdentifier​(ReadOnlySpan<​char> oidValue,
Asn1Tag? tag = null)
Write an Object Identifier with a specified tag.
oidValue
The object identifier to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 6).
public
void
WriteObjectIdentifier​(string oidValue,
Asn1Tag? tag = null)
Write an Object Identifier with a specified tag.
oidValue
The object identifier to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 6).
public
void
WriteOctetString​(ReadOnlySpan<​byte> value,
Asn1Tag? tag = null)
Write an Octet String value with a specified tag.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 4).
public
void
WriteUtcTime​(DateTimeOffset value,
int twoDigitYearMax,
Asn1Tag? tag = null)
Write the provided value as a UTCTime with a specified tag,
provided the year is in the allowed range.
value
The value to write.
twoDigitYearMax
The maximum valid year for <paramref name="value" /> , after conversion to UTC.
For the X.509 Time.utcTime range of 1950-2049, pass <c>2049</c> .
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 23).
public
void
WriteUtcTime​(DateTimeOffset value,
Asn1Tag? tag = null)
Write the provided value as a UTCTime with a specified tag,
accepting the two-digit year as valid in context.
value
The value to write.
tag
The tag to write, or <see langword="null" /> for the default tag (Universal 23).
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object