Skip to content

Commit

Permalink
Merge pull request #16 from r4gus/0.11.2-alpha
Browse files Browse the repository at this point in the history
0.11.2 alpha
  • Loading branch information
r4gus authored Mar 19, 2023
2 parents 94688ef + 6ae7f39 commit 69f416f
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 148 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const di = DataItem.new("\x1b\xff\xff\xff\xff\xff\xff\xff\xff") catch {
};
```

`DataItem.new()` will check if the given data is well-formed before returning a `DataItem`. The data is well formed if it's syntactically correct and no bytes are left in the input after parsing (see [RFC 8949 Appendix C](https://www.rfc-editor.org/rfc/rfc8949.html#section-appendix.c-1)).
`DataItem.new()` will check if the given data is well-formed before returning a `DataItem`. The data is well formed if it's syntactically correct.

To check the type of the given `DataItem` use the `getType()` function.

Expand Down Expand Up @@ -117,6 +117,18 @@ try stringify(i, .{}, str.writer());
`u8`slices with sentinel terminator (e.g. `const x: [:0] = "FIDO_2_0"`) are treated as text strings and
`u8` slices without sentinel terminator as byte strings.

##### Stringify Options

You can pass options to the `stringify` function to influence its behaviour.

This includes:

* `allocator` - The allocator to be used (if necessary)
* `skip_null_fields` - Struct fields that are null will not be included in the CBOR map
* `slice_as_text` - Convert an u8 slice into a CBOR text string
* `enum_as_text`- Use the field name instead of the numerical value to represent a enum
* `field_settings` - Lets you influence how `stringify` treats specific fileds. The settings set using `field_settings` override the default settings.

#### Deserialization

You can deserialize CBOR data into Zig objects using the `parse()` function.
Expand All @@ -139,6 +151,7 @@ This includes:
* `allocator` - The allocator to be used (if necessary)
* `duplicate_field_behavior` - How to handle duplicate fields (`.UseFirst`, `.Error`)
* `ignore_unknown_fields` - Ignore unknown fields
* `field_settings` - Lets you specify aliases for struct fields

#### Builder

Expand Down
22 changes: 22 additions & 0 deletions src/cose.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ pub const Algorithm = enum(i32) {
A192Gcm = 2,
/// AES-GCM mode w/ 256-bit key, 128-bit tag
A256Gcm = 3,

pub fn to_raw(self: @This()) [4]u8 {
const i = @enumToInt(self);
return std.mem.asBytes(&i).*;
}

pub fn from_raw(raw: [4]u8) @This() {
return @intToEnum(@This(), std.mem.bytesToValue(i32, &raw));
}
};

/// COSE key types
Expand Down Expand Up @@ -205,3 +214,16 @@ test "cose Key p256 parse #1" {

//const x = try EcdsaP256Sha256.PublicKey.fromSec1("\x04\xd9\xf4\xc2\xa3\x52\x13\x6f\x19\xc9\xa9\x5d\xa8\x82\x4a\xb5\xcd\xc4\xd5\x63\x1e\xbc\xfd\x5b\xdb\xb0\xbf\xff\x25\x36\x09\x12\x9e\xef\x40\x4b\x88\x07\x65\x57\x60\x07\x88\x8a\x3e\xd6\xab\xff\xb4\x25\x7b\x71\x23\x55\x33\x25\xd4\x50\x61\x3c\xb5\xbc\x9a\x3a\x52");
}

test "alg to raw" {
const es256 = Algorithm.Es256;
const x: [4]u8 = es256.to_raw();

try std.testing.expectEqualSlices(u8, "\xF9\xFF\xFF\xFF", &x);
}

test "raw to alg" {
const x: [4]u8 = "\xF9\xFF\xFF\xFF".*;

try std.testing.expectEqual(Algorithm.Es256, Algorithm.from_raw(x));
}
Loading

0 comments on commit 69f416f

Please sign in to comment.