-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a stateful CBOR writer #32047
Comments
May I suggest support for writing Streams? Caveat: |
Supporting writing directly to streams requires one of:
It also, as you observe, requires making async versions of everything. It would possibly make sense to have something like a Alternatively and/or additionally, a static public partial static class CborEncoder
{
public static bool TryEncodeNumber(int value, Span<byte> destination, out int bytesWritten) => throw null;
public static void EncodeNumber(int value, Stream destination);
} The problem that I see with that, is that there isn't any type safety on maps or arrays... e.g. it either says it accepts The design in my head, FWIW, would be that writing something like https://tools.ietf.org/html/rfc8152#appendix-C.1.1
could be done via byte[] payload = Encoding.UTF8.GetBytes("This is the content.");
CoseKey key = ...;
using (CborWriter writer = new CborWriter(CborCanonicalization.FIDO2))
{
writer.WriteTag(98);
using (writer.StartArray())
{
// protected
writer.WriteByteString(ReadOnlySpan<byte>.Empty);
// unprotected
writer.WriteStartMap();
writer.WriteEndMap();
// payload
writer.WriteByteString(payload);
// signatures
using (writer.StartArray())
using (writer.StartArray())
{
// protected
using (writer.StartByteStringWrapper())
using (writer.StartMap())
{
writer.WriteNumber((int)CoseProperties.AlgorithmId);
writer.WriteNumber((int)key.AlgorithmId);
}
// unprotected
using (writer.StartMap())
{
writer.WriteNumber((int)CoseProperties.KeyIdentifier);
writer.WriteByteString(key.Id);
}
// signature
writer.WriteByteString(key.Sign(payload));
}
}
} (Actual code would be broken up into more helper routines, each one writing down their part) Using a definite encoding and a non-seekable stream; that can only write the tag; it has to hold back the entirety of the outer array until it knows how long it is. Even with a seekable stream, using the canonical CBOR "write the length in the fewest bytes possible" it would have to hold everything in case it mis-predicted the length-length. Indefinite encoding would work; but that's not compatible with protocols that specify canonical CBOR (such as FIDO2 / WebAuthn). Plus, I completely skipped over the detail of needing to sort the map by encoded key values (RFC 7049 3.9 rule 3). So... Streams are hard. But, it's good to know that someone wants them... it's an interesting thought experiment. |
To consider: adding support for System.Decimal. The CBOR spec already supports decimal fractionals. |
How will this project compare to https://github.com/peteroupc/CBOR? |
Also area-System.Security is the wrong tag for CBOR-related issues/PRs. Should be area-Serialization. With |
@charlesroddie Currently it's only "funded" via System.Security; and the namespace is something we're working on. |
Closing this issue as the implementation aspect of this work is done. |
Create a reader for CBOR (Concise Binary Object Representation), as defined in IETF RFC 7049.
Rough project plan (approximately a PR for each stage, and 3-5 days of work)
Dictionary<object, object>
... key types are heterogeneous.The text was updated successfully, but these errors were encountered: