-
Notifications
You must be signed in to change notification settings - Fork 23
fix: Examples update #129
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
Merged
Merged
fix: Examples update #129
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6334191
fix: Update examples
tmathern dc2022a
fix: Add a v2 test
tmathern d868db5
fix: Add v2 callback signer test
tmathern 3a790f0
fix: One more example
tmathern 14ed825
fix: More examples
tmathern 1ab3c10
fix: Remove debug log
tmathern b32a7cf
Merge branch 'main' into mathern/docs-update
tmathern 89329f3
Update README.md
tmathern 2b288f1
Merge branch 'main' into mathern/docs-update
tmathern 384bcb3
fix: Docs review comments
tmathern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,79 +11,110 @@ | |
| # each license. | ||
|
|
||
| # This example shows how to sign an image with a C2PA manifest | ||
| # and read the metadata added to the image. | ||
| # using a callback signer and read the metadata added to the image. | ||
|
|
||
| import os | ||
| import c2pa | ||
| from cryptography.hazmat.primitives import hashes, serialization | ||
| from cryptography.hazmat.primitives.asymmetric import ec | ||
| from cryptography.hazmat.backends import default_backend | ||
|
|
||
| # Note: Builder, Reader, and Signer support being used as context managers | ||
| # (with 'with' statements), but this example shows manual usage which requires | ||
| # explicitly calling the close() function to clean up resources. | ||
|
|
||
| fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/") | ||
| output_dir = os.path.join(os.path.dirname(__file__), "../output/") | ||
|
|
||
| # ensure the output directory exists | ||
| # Ensure the output directory exists | ||
| if not os.path.exists(output_dir): | ||
| os.makedirs(output_dir) | ||
|
|
||
| print("c2pa version:") | ||
| version = c2pa.sdk_version() | ||
| print(version) | ||
|
|
||
| # Read existing C2PA metadata from the file | ||
| print("\nReading existing C2PA metadata:") | ||
| with open(fixtures_dir + "C.jpg", "rb") as file: | ||
| reader = c2pa.Reader("image/jpeg", file) | ||
| print(reader.json()) | ||
|
|
||
| # Create a signer from certificate and key files | ||
| # Load certificates and private key (here from the test fixtures) | ||
| # This is OK for development, but in production you should use a | ||
| # secure way to load the certificates and private key. | ||
| certs = open(fixtures_dir + "es256_certs.pem", "rb").read() | ||
| key = open(fixtures_dir + "es256_private.key", "rb").read() | ||
|
|
||
| signer_info = c2pa.C2paSignerInfo( | ||
| alg=b"es256", # Use bytes instead of encoded string | ||
| sign_cert=certs, | ||
| private_key=key, | ||
| ta_url=b"http://timestamp.digicert.com" # Use bytes and add timestamp URL | ||
| ) | ||
| # Define a callback signer function | ||
| def callback_signer_es256(data: bytes) -> bytes: | ||
| """Callback function that signs data using ES256 algorithm.""" | ||
| private_key = serialization.load_pem_private_key( | ||
| key, | ||
| password=None, | ||
| backend=default_backend() | ||
| ) | ||
| signature = private_key.sign( | ||
| data, | ||
| ec.ECDSA(hashes.SHA256()) | ||
| ) | ||
| return signature | ||
|
|
||
| signer = c2pa.Signer.from_info(signer_info) | ||
| # Create a signer using the callback function we defined | ||
| signer = c2pa.Signer.from_callback( | ||
| callback=callback_signer_es256, | ||
| alg=c2pa.C2paSigningAlg.ES256, | ||
| certs=certs.decode('utf-8'), | ||
| tsa_url="http://timestamp.digicert.com" | ||
| ) | ||
|
|
||
| # Create a manifest definition as a dictionary | ||
| # This manifest follows the V2 manifest format | ||
| manifest_definition = { | ||
| "claim_generator": "python_example", | ||
| "claim_generator_info": [{ | ||
| "name": "python_example", | ||
| "version": "0.0.1", | ||
| }], | ||
| "claim_version": 2, | ||
| "format": "image/jpeg", | ||
| "title": "Python Example Image", | ||
| "ingredients": [], | ||
| "assertions": [ | ||
| { | ||
| 'label': 'stds.schema-org.CreativeWork', | ||
| 'data': { | ||
| '@context': 'http://schema.org/', | ||
| '@type': 'CreativeWork', | ||
| 'author': [ | ||
| {'@type': 'Person', 'name': 'Example User'} | ||
| "label": "c2pa.actions", | ||
| "data": { | ||
| "actions": [ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is the most minimal valid V2 manifest we can have. |
||
| { | ||
| "action": "c2pa.created", | ||
| "parameters": { | ||
tmathern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # could hold additional information about this step | ||
| # eg. model used, etc. | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| 'kind': 'Json' | ||
| } | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # Create the builder with the manifest definition | ||
| builder = c2pa.Builder(manifest_definition) | ||
|
|
||
| # Sign the image | ||
| print("\nSigning the image...") | ||
| with open(fixtures_dir + "C.jpg", "rb") as source: | ||
| with open(output_dir + "C_signed.jpg", "wb") as dest: | ||
| result = builder.sign(signer, "image/jpeg", source, dest) | ||
| # Sign the image with the signer created above, | ||
| # which will use the callback signer | ||
| print("\nSigning the image file...") | ||
| builder.sign_file( | ||
| source_path=fixtures_dir + "A.jpg", | ||
| dest_path=output_dir + "A_signed.jpg", | ||
| signer=signer | ||
| ) | ||
|
|
||
| # Clean up | ||
| signer.close() | ||
| builder.close() | ||
|
|
||
| # Read the signed image to verify | ||
| # Re-Read the signed image to verify | ||
| print("\nReading signed image metadata:") | ||
| with open(output_dir + "C_signed.jpg", "rb") as file: | ||
| with open(output_dir + "A_signed.jpg", "rb") as file: | ||
| reader = c2pa.Reader("image/jpeg", file) | ||
| print(reader.json()) | ||
| reader.close() | ||
|
|
||
| print("\nExample completed successfully!") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Copyright 2025 Adobe. All rights reserved. | ||
tmathern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # This file is licensed to you under the Apache License, | ||
| # Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
| # or the MIT license (http://opensource.org/licenses/MIT), | ||
| # at your option. | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # this software is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
| # implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
| # specific language governing permissions and limitations under | ||
| # each license. | ||
|
|
||
| ############################################################### | ||
| # This example shows an "older" way of signing, | ||
| # and is left here as reference. | ||
| # Please refer to sign.py for the recommended implementation. | ||
| ############################################################### | ||
|
|
||
| # This example shows how to sign an image with a C2PA manifest | ||
| # and read the metadata added to the image. | ||
|
|
||
| import os | ||
| import c2pa | ||
|
|
||
| fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/") | ||
| output_dir = os.path.join(os.path.dirname(__file__), "../output/") | ||
|
|
||
| # Note: Builder, Reader, and Signer support being used as context managers | ||
| # (with 'with' statements), but this example shows manual usage which requires | ||
| # explicitly calling the close() function to clean up resources. | ||
|
|
||
| # Ensure the output directory exists | ||
| if not os.path.exists(output_dir): | ||
| os.makedirs(output_dir) | ||
|
|
||
| print("c2pa version:") | ||
| version = c2pa.sdk_version() | ||
| print(version) | ||
|
|
||
| # Read existing C2PA metadata from the file | ||
| print("\nReading existing C2PA metadata:") | ||
| with open(fixtures_dir + "C.jpg", "rb") as file: | ||
| reader = c2pa.Reader("image/jpeg", file) | ||
| print(reader.json()) | ||
| reader.close() | ||
|
|
||
| # Create a signer from certificate and key files | ||
| certs = open(fixtures_dir + "es256_certs.pem", "rb").read() | ||
| key = open(fixtures_dir + "es256_private.key", "rb").read() | ||
|
|
||
| # Define Signer information | ||
| signer_info = c2pa.C2paSignerInfo( | ||
| alg=b"es256", # Use bytes instead of encoded string | ||
| sign_cert=certs, | ||
| private_key=key, | ||
| ta_url=b"http://timestamp.digicert.com" # Use bytes and add timestamp URL | ||
| ) | ||
|
|
||
| # Create the Signer from the information | ||
| signer = c2pa.Signer.from_info(signer_info) | ||
|
|
||
| # Create a manifest definition as a dictionary | ||
| # This examples signs using a V1 manifest | ||
| manifest_definition = { | ||
| "claim_generator": "python_example", | ||
| "claim_generator_info": [{ | ||
| "name": "python_example", | ||
| "version": "0.0.1", | ||
| }], | ||
| "format": "image/jpeg", | ||
| "title": "Python Example Image", | ||
| "ingredients": [], | ||
| "assertions": [ | ||
| { | ||
| "label": "c2pa.actions", | ||
| "data": { | ||
| "actions": [ | ||
| { | ||
| "action": "c2pa.created", | ||
| "parameters": { | ||
| # could hold additional information about this step | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # Create the builder with the manifest definition | ||
| builder = c2pa.Builder(manifest_definition) | ||
|
|
||
| # Sign the image | ||
| print("\nSigning the image...") | ||
| with open(fixtures_dir + "C.jpg", "rb") as source: | ||
| with open(output_dir + "C_signed.jpg", "wb") as dest: | ||
| result = builder.sign(signer, "image/jpeg", source, dest) | ||
|
|
||
| # Read the signed image to verify | ||
| print("\nReading signed image metadata:") | ||
| with open(output_dir + "C_signed.jpg", "rb") as file: | ||
| reader = c2pa.Reader("image/jpeg", file) | ||
| print(reader.json()) | ||
| reader.close() | ||
|
|
||
| # Clean up resources manually, since we are not using with statements | ||
| signer.close() | ||
| builder.close() | ||
|
|
||
| print("\nExample completed successfully!") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.