You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Read and validate C2PA data in a file or stream
49
+
### Define manifest JSON
50
50
51
-
Use the `Reader` to read C2PA data from the specified file.
52
-
This examines the specified media file for C2PA data and generates a report of any data it finds. If there are validation errors, the report includes a `validation_status` field. For a summary of supported media types, see [Supported file formats](#supported-file-formats).
51
+
The Python library works with both file-based and stream-based operations.
52
+
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset; for example:
53
53
54
-
A media file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the `active_manifest` field in the manifests map.
The `sign_ps256` function is [defined in the library](https://github.com/contentauth/c2pa-python/blob/main/c2pa/c2pa_api/c2pa_api.py#L209) and is reproduced here to show how signing is performed.
76
+
77
+
```py
78
+
# Example of using Python crypto to sign data using openssl with Ps256
79
+
from cryptography.hazmat.primitives import hashes, serialization
80
+
from cryptography.hazmat.primitives.asymmetric import padding
**Read and validate C2PA data from an asset file**
102
+
103
+
Use the `Reader` to read C2PA data from the specified asset file (see [supported file formats](#supported-file-formats)).
55
104
56
-
The manifests may contain binary resources such as thumbnails which can be retrieved with `resource_to_stream` or `resource_to_file` using the associated `identifier` field values and a `uri`.
105
+
This examines the specified media file for C2PA data and generates a report of any data it finds. If there are validation errors, the report includes a `validation_status` field.
106
+
107
+
An asset file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the `active_manifest` field in the manifests map. The manifests may contain binary resources such as thumbnails which can be retrieved with `resource_to_stream` or `resource_to_file` using the associated `identifier` field values and a `uri`.
57
108
58
109
NOTE: For a comprehensive reference to the JSON manifest structure, see the [Manifest store reference](https://opensource.contentauthenticity.org/docs/manifest/manifest-ref).
# It's also possible to create a reader from a format and stream
64
-
# Note that these two readers are functionally equivalent
65
-
stream =open("path/to/media_file.jpg", "rb")
66
-
reader = c2pa.Reader("image/jpeg", stream)
67
115
68
116
# Print the JSON for a manifest.
69
117
print("manifest store:", reader.json())
@@ -80,9 +128,9 @@ except Exception as err:
80
128
print(err)
81
129
```
82
130
83
-
### Add a signed manifest to a media file or stream
131
+
**Add a signed manifest to an asset file**
84
132
85
-
**WARNING**: This example accesses the private key and security certficate directly from the local file system. This is fine during development, but doing so in production may be insecure. Instead use a Key Management Service (KMS) or a hardware security module (HSM) to access the certificate and key; for example as show in the [C2PA Python Example](https://github.com/contentauth/c2pa-python-example).
133
+
**WARNING**: This example accesses the private key and security certificate directly from the local file system. This is fine during development, but doing so in production may be insecure. Instead use a Key Management Service (KMS) or a hardware security module (HSM) to access the certificate and key; for example as show in the [C2PA Python Example](https://github.com/contentauth/c2pa-python-example).
86
134
87
135
Use a `Builder` to add a manifest to an asset:
88
136
@@ -100,42 +148,12 @@ try:
100
148
# Create a signer from the private signer, certs and a time stamp service url
Instead of working with files, you can read, validate, and add a signed manifest to streamed data. This example code does the same thing as the file-based example.
189
+
190
+
**Read and validate C2PA data from a stream**
191
+
192
+
```py
193
+
try:
194
+
# It's also possible to create a reader from a format and stream
195
+
# Note that these two readers are functionally equivalent
196
+
stream =open("path/to/media_file.jpg", "rb")
197
+
reader = c2pa.Reader("image/jpeg", stream)
198
+
199
+
# Print the JSON for a manifest.
200
+
print("manifest store:", reader.json())
201
+
202
+
# Get the active manifest.
203
+
manifest = reader.get_active_manifest()
204
+
if manifest !=None:
205
+
206
+
# get the uri to the manifest's thumbnail and write it to a file
207
+
uri = manifest["thumbnail"]["identifier"]
208
+
reader.resource_to_file(uri, "thumbnail_v2.jpg")
172
209
173
210
exceptExceptionas err:
174
211
print(err)
175
-
```
212
+
```
176
213
177
-
### Creating a manifest JSON definition file
214
+
**Add a signed manifest to a stream**
178
215
179
-
The manifest JSON string defines the C2PA manifest to add to the file.
216
+
**WARNING**: This example accesses the private key and security certificate directly from the local file system. This is fine during development, but doing so in production may be insecure. Instead use a Key Management Service (KMS) or a hardware security module (HSM) to access the certificate and key; for example as show in the [C2PA Python Example](https://github.com/contentauth/c2pa-python-example).
0 commit comments