Skip to content
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

Add option for Deterministic encoded protected and unprotected headers #574

Merged
merged 2 commits into from
Oct 26, 2024

Conversation

kentakayama
Copy link
Collaborator

Solves #573 .

This PR introduces deterministic_header option to COSE.new() constructor.
With this option, the headers will be deterministically encoded.

import cbor2
from cwt import COSE, COSEKey

sign_key = COSEKey.from_jwk({
    "kty": "EC",
    "alg": "ES256",
    "crv": "P-256",
    "x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
    "y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
    "d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM",
})

# UPDATED
ctx = COSE.new(alg_auto_inclusion=True, deterministic_header=True)
encoded = ctx.encode_and_sign(
    payload=b'a',
    key=sign_key,
    protected={"kid": "11"},
)
bstr_encoded_p = cbor2.loads(encoded).value[0]
encoded_p = cbor2.loads(bstr_encoded_p)
print(encoded_p)

displays {1: -7, 4: b'11'}, which is deterministically encoded.

Copy link
Owner

@dajiaji dajiaji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! It looks good to me.

@dajiaji dajiaji merged commit 6ced08f into main Oct 26, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants