Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Please add an example method for ES256 signing #31

Open
naomatheus opened this issue Sep 3, 2024 · 1 comment
Open

Please add an example method for ES256 signing #31

naomatheus opened this issue Sep 3, 2024 · 1 comment

Comments

@naomatheus
Copy link

I generate ES256 keys in my project, and needed to support ES256 signing algorithm. This is how I did this. Please consider adding to the repository's examples.

Adding, for example, to c2pa_api.py and examples:

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.serialization import load_pem_private_key

def sign_es256(data: bytes, key_path: str) -> bytes:
    with open(key_path, "rb") as key_file:
        private_key = load_pem_private_key(
            key_file.read(),
            password=None,
        )
    
    # Ensure the key is an EC private key
    if not isinstance(private_key, ec.EllipticCurvePrivateKey):
        raise ValueError("The provided key is not an Elliptic Curve private key")
    
    # Sign the data using ECDSA with SHA256
    signature = private_key.sign(
        data,
        ec.ECDSA(hashes.SHA256())
    )
    return signature

You could tag this on here:

return signature

Happy to open a PR post-discussion.

@gpeacock
Copy link
Contributor

gpeacock commented Oct 9, 2024

Thanks for this. We will test it out and consider adding it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants