Skip to content

Commit 14ed825

Browse files
committed
fix: More examples
1 parent 3a790f0 commit 14ed825

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

examples/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ The `examples/training.py` script demonstrates how to add a "Do Not Train" asser
88

99
### Signing and Verifying Assets
1010

11-
The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it.
11+
The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it using a callback signer. Callback signers let you define signing logic, eg. where to load keys from.
12+
13+
The `examples/sign_info.py` script shows how to sign an asset with a C2PA manifest and verify it using a "default" signer created with the needed signer information.
1214

1315
## Running the Examples
1416

@@ -21,6 +23,11 @@ Then you can run the examples with the following commands:
2123
python examples/training.py
2224

2325
# Run the signing and verification example
26+
# In this example, signing is done with a Signer created using SignerInfo
27+
python examples/sign_info.py
28+
29+
# Run the signing and verification example
30+
# In this example, signing is done using a callback signer
2431
python examples/sign.py
2532
```
2633

examples/sign.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from cryptography.hazmat.primitives.asymmetric import ec
2020
from cryptography.hazmat.backends import default_backend
2121

22+
# Note: Builder, Reader, and Signer support being used as context managers
23+
# (with 'with' statements), but this example shows manual usage which requires
24+
# explicitly calling the close() function to clean up resources.
25+
2226
fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/")
2327
output_dir = os.path.join(os.path.dirname(__file__), "../output/")
2428

@@ -30,11 +34,6 @@
3034
version = c2pa.sdk_version()
3135
print(version)
3236

33-
# Read existing C2PA metadata from the file
34-
print("\nReading existing C2PA metadata:")
35-
with open(fixtures_dir + "C.jpg", "rb") as file:
36-
reader = c2pa.Reader("image/jpeg", file)
37-
print(reader.json())
3837

3938
# Load certificates and private key (here from the test fixtures)
4039
# This is OK for development, but in production you should use a
@@ -85,6 +84,7 @@ def callback_signer_es256(data: bytes) -> bytes:
8584
"action": "c2pa.created",
8685
"parameters": {
8786
# could hold additional information about this step
87+
# eg. model used, etc.
8888
}
8989
}
9090
]
@@ -100,19 +100,21 @@ def callback_signer_es256(data: bytes) -> bytes:
100100
# which will use the callback signer
101101
print("\nSigning the image file...")
102102
builder.sign_file(
103-
source_path=fixtures_dir + "C.jpg",
104-
dest_path=output_dir + "C_signed.jpg",
103+
source_path=fixtures_dir + "A.jpg",
104+
dest_path=output_dir + "A_signed.jpg",
105105
signer=signer
106106
)
107107

108-
# Clean up the signer
108+
# Clean up
109109
signer.close()
110+
builder.close()
110111

111-
# Read the signed image to verify
112+
# Re-Read the signed image to verify
112113
print("\nReading signed image metadata:")
113-
with open(output_dir + "C_signed.jpg", "rb") as file:
114+
with open(output_dir + "A_signed.jpg", "rb") as file:
114115
reader = c2pa.Reader("image/jpeg", file)
115116
print(reader.json())
117+
reader.close()
116118

117119
print("\nExample completed successfully!")
118120

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/")
2020
output_dir = os.path.join(os.path.dirname(__file__), "../output/")
2121

22+
# Note: Builder, Reader, and Signer support being used as context managers
23+
# (with 'with' statements), but this example shows manual usage which requires
24+
# explicitly calling the close() function to clean up resources.
25+
2226
# Ensure the output directory exists
2327
if not os.path.exists(output_dir):
2428
os.makedirs(output_dir)
@@ -32,18 +36,21 @@
3236
with open(fixtures_dir + "C.jpg", "rb") as file:
3337
reader = c2pa.Reader("image/jpeg", file)
3438
print(reader.json())
39+
reader.close()
3540

3641
# Create a signer from certificate and key files
3742
certs = open(fixtures_dir + "es256_certs.pem", "rb").read()
3843
key = open(fixtures_dir + "es256_private.key", "rb").read()
3944

45+
# Define Signer information
4046
signer_info = c2pa.C2paSignerInfo(
4147
alg=b"es256", # Use bytes instead of encoded string
4248
sign_cert=certs,
4349
private_key=key,
4450
ta_url=b"http://timestamp.digicert.com" # Use bytes and add timestamp URL
4551
)
4652

53+
# Create the Signer from the information
4754
signer = c2pa.Signer.from_info(signer_info)
4855

4956
# Create a manifest definition as a dictionary
@@ -74,6 +81,7 @@
7481
]
7582
}
7683

84+
# Create the builder with the manifest definition
7785
builder = c2pa.Builder(manifest_definition)
7886

7987
# Sign the image
@@ -87,6 +95,11 @@
8795
with open(output_dir + "C_signed.jpg", "rb") as file:
8896
reader = c2pa.Reader("image/jpeg", file)
8997
print(reader.json())
98+
reader.close()
99+
100+
# Clean up resources manually, since we are not using with statements
101+
signer.close()
102+
builder.close()
90103

91104
print("\nExample completed successfully!")
92105

0 commit comments

Comments
 (0)