Skip to content

Commit 21eff82

Browse files
authored
Add to_bytes() to COSEKeyInterface.
Add to_bytes() to COSEKeyInterface.
2 parents 88a085a + 1c0fe4b commit 21eff82

5 files changed

Lines changed: 24 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ r = Recipient.new(
646646
},
647647
},
648648
)
649-
r.encode(enc_key.key, recipient_key=rpk)
649+
r.encode(enc_key.to_bytes(), recipient_key=rpk)
650650
sender = COSE.new()
651651
encoded = sender.encode_and_encrypt(
652652
b"This is the content.",

cwt/algs/symmetric.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def __init__(self, params: Dict[int, Any]):
4141
def key(self) -> bytes:
4242
return self._key
4343

44+
def to_bytes(self) -> bytes:
45+
return self._key
46+
4447
def to_dict(self) -> Dict[int, Any]:
4548
res = super().to_dict()
4649
res[-1] = self._key

cwt/cose_key_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def key(self) -> Any:
104104
"""
105105
raise NotImplementedError
106106

107+
def to_bytes(self) -> bytes:
108+
"""
109+
Serializes the body of the key as a byte string.
110+
"""
111+
raise NotImplementedError
112+
107113
def to_dict(self) -> Dict[int, Any]:
108114
"""
109115
Returns the CBOR-like structure (Dict[int, Any]) of the COSE key.

tests/test_cose_key.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,14 @@ def test_key_builder_from_jwk_with_invalid_arg(self, invalid, msg):
615615
COSEKey.from_jwk(invalid)
616616
pytest.fail("from_jwk should fail.")
617617
assert msg in str(err.value)
618+
619+
def test_cose_key_interface(self):
620+
ki = COSEKeyInterface({1: 4, 3: 1})
621+
with pytest.raises(NotImplementedError) as err:
622+
ki.key
623+
pytest.fail("key should fail.")
624+
assert "" == str(err.value)
625+
with pytest.raises(NotImplementedError) as err:
626+
ki.to_bytes()
627+
pytest.fail("to_bytes should fail.")
628+
assert "" == str(err.value)

tests/test_cose_sample.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def test_cose_usage_examples_cose_encrypt_hpke(self):
444444
},
445445
},
446446
)
447-
r.encode(enc_key.key, recipient_key=rpk)
447+
r.encode(enc_key.to_bytes(), recipient_key=rpk)
448448
sender = COSE.new(alg_auto_inclusion=True)
449449
encoded = sender.encode_and_encrypt(
450450
b"This is the content.",
@@ -494,7 +494,7 @@ def test_cose_usage_examples_cose_encrypt_hpke_with_1st_layer_hpke(self):
494494
},
495495
},
496496
)
497-
r.encode(enc_key.key, recipient_key=rpk)
497+
r.encode(enc_key.to_bytes(), recipient_key=rpk)
498498
sender = COSE.new()
499499
with pytest.raises(ValueError) as err:
500500
sender.encode_and_encrypt(
@@ -541,7 +541,7 @@ def test_cose_usage_examples_cose_encrypt_hpke_with_nonce(self):
541541
},
542542
},
543543
)
544-
r.encode(enc_key.key, recipient_key=rpk)
544+
r.encode(enc_key.to_bytes(), recipient_key=rpk)
545545
sender = COSE.new()
546546
with pytest.raises(ValueError) as err:
547547
sender.encode_and_encrypt(

0 commit comments

Comments
 (0)