Skip to content

Commit fd96d0f

Browse files
Make CoseKeyBuilder more useful (#54)
1 parent cbf9a47 commit fd96d0f

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 0.3.3 - TBD
4+
5+
- Add `CoseKeyBuilder` methods `kty`, `key_type` and `new_okp_key`.
6+
37
## 0.3.2 - 2022-04-02
48

59
- Add basic [CWT](https://datatracker.ietf.org/doc/html/rfc8392) support in `cwt` module, via the `ClaimsSet` type.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "coset"
3-
version = "0.3.2"
3+
version = "0.3.3-alpha.1"
44
authors = ["David Drysdale <[email protected]>", "Paul Crowley <[email protected]>"]
55
edition = "2018"
66
license = "Apache-2.0"

src/key/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ pub struct CoseKeyBuilder(CoseKey);
183183

184184
impl CoseKeyBuilder {
185185
builder! {CoseKey}
186+
builder_set! {kty: KeyType}
186187
builder_set! {key_id: Vec<u8>}
187188
builder_set! {base_iv: Vec<u8>}
188189

@@ -250,6 +251,21 @@ impl CoseKeyBuilder {
250251
})
251252
}
252253

254+
/// Constructor for a octet keypair key.
255+
pub fn new_okp_key() -> Self {
256+
Self(CoseKey {
257+
kty: KeyType::Assigned(iana::KeyType::OKP),
258+
..Default::default()
259+
})
260+
}
261+
262+
/// Set the key type.
263+
#[must_use]
264+
pub fn key_type(mut self, key_type: iana::KeyType) -> Self {
265+
self.0.kty = KeyType::Assigned(key_type);
266+
self
267+
}
268+
253269
/// Set the algorithm.
254270
#[must_use]
255271
pub fn algorithm(mut self, alg: iana::Algorithm) -> Self {

src/key/tests.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use super::*;
1818
use crate::{cbor::value::Value, iana, util::expect_err, CborSerializable};
19-
use alloc::{borrow::ToOwned, vec};
19+
use alloc::{borrow::ToOwned, string::ToString, vec};
2020

2121
#[test]
2222
fn test_cose_key_encode() {
@@ -702,6 +702,31 @@ fn test_key_builder() {
702702
..Default::default()
703703
},
704704
),
705+
(
706+
CoseKeyBuilder::new_okp_key().build(),
707+
CoseKey {
708+
kty: KeyType::Assigned(iana::KeyType::OKP),
709+
..Default::default()
710+
},
711+
),
712+
(
713+
CoseKeyBuilder::new()
714+
.key_type(iana::KeyType::WalnutDSA)
715+
.build(),
716+
CoseKey {
717+
kty: KeyType::Assigned(iana::KeyType::WalnutDSA),
718+
..Default::default()
719+
},
720+
),
721+
(
722+
CoseKeyBuilder::new()
723+
.kty(KeyType::Text("test".to_string()))
724+
.build(),
725+
CoseKey {
726+
kty: KeyType::Text("test".to_string()),
727+
..Default::default()
728+
},
729+
),
705730
];
706731
for (got, want) in tests {
707732
assert_eq!(got, want);

0 commit comments

Comments
 (0)