@@ -180,54 +180,39 @@ except Exception as err:
180180Use a ` Builder ` to add a manifest to an asset:
181181
182182``` py
183+ from c2pa import Builder, Error, Reader, SigningAlg, create_signer, sdk_version, sign_ps256, version
184+ ...
185+ data_dir = " tests/fixtures/"
183186try :
184- # Define a function to sign the claim bytes
185- # In this case we are using a pre-defined sign_ps256 method, passing in our private cert
186- # Normally this cert would be kept safe in some other location
187- def private_sign (data : bytes ) -> bytes :
188- return sign_ps256(data, " tests/fixtures/ps256.pem" )
189-
190- # read our public certs into memory
191- certs = open (data_dir + " ps256.pub" , " rb" ).read()
192-
193- # Create a signer from the private signer, certs and a time stamp service url
194- signer = create_signer(private_sign, SigningAlg.PS256 , certs, " http://timestamp.digicert.com" )
195-
196- # Create a builder add a thumbnail resource and an ingredient file.
197- builder = Builder(manifest_json)
198-
199- # Add the resource from a stream
200- a_thumbnail_jpg_stream = open (" tests/fixtures/A_thumbnail.jpg" , " rb" )
201- builder.add_resource(" image/jpeg" , a_thumbnail_jpg_stream)
202-
203- # Define an ingredient, in this case a parent ingredient named A.jpg, with a thumbnail
204- ingredient_json = {
205- " title" : " A.jpg" ,
206- " relationship" : " parentOf" , # "parentOf", "componentOf" or "inputTo"
207- " thumbnail" : {
208- " identifier" : " thumbnail" ,
209- " format" : " image/jpeg"
210- }
211- }
212-
213- # Add the ingredient from a stream
214- a_jpg_stream = open (" tests/fixtures/A.jpg" , " rb" )
215- builder.add_ingredient(" image/jpeg" , a_jpg_stream)
216-
217- # At this point we could archive or unarchive our Builder to continue later.
218- # In this example we use a bytearray for the archive stream.
219- # all ingredients and resources will be saved in the archive
220- archive = io.BytesIO(bytearray ())
221- builder.to_archive(archive)
222- archive.seek()
223- builder = builder.from_archive(archive)
224-
225- # Sign the builder with a stream and output it to a stream
226- # This returns the binary manifest data that could be uploaded to cloud storage.
227- input_stream = open (" tests/fixtures/A.jpg" , " rb" )
228- output_stream = open (" target/out.jpg" , " wb" )
229- c2pa_data = builder.sign(signer, " image/jpeg" , input_stream, output_stream)
230-
231- except Exception as err:
232- print (err)
187+ key = open (data_dir + " ps256.pem" , " rb" ).read()
188+ def sign (data : bytes ) -> bytes :
189+ return sign_ps256(data, key)
190+
191+ certs = open (data_dir + " ps256.pub" , " rb" ).read()
192+ # Create a local signer from a certificate pem file
193+ signer = create_signer(sign, SigningAlg.PS256 , certs, " http://timestamp.digicert.com" )
194+
195+ builder = Builder(manifest_def)
196+
197+ builder.add_ingredient_file(ingredient_def, data_dir + " A.jpg" )
198+ builder.add_resource_file(" A.jpg" , data_dir + " A.jpg" )
199+ builder.to_archive(open (" target/archive.zip" , " wb" ))
200+
201+ builder = Builder.from_archive(open (" target/archive.zip" , " rb" ))
202+
203+ with tempfile.TemporaryDirectory() as output_dir:
204+ output_path = output_dir + " out.jpg"
205+ if os.path.exists(output_path):
206+ os.remove(output_path)
207+ c2pa_data = builder.sign_file(signer, data_dir + " A.jpg" , output_dir + " out.jpg" )
208+ assert len (c2pa_data) > 0
209+
210+ reader = Reader.from_file(output_dir + " out.jpg" )
211+ print (reader.json())
212+ manifest_store = json.loads(reader.json())
213+ manifest = manifest_store[" manifests" ][manifest_store[" active_manifest" ]]
214+
215+ except Exception as e:
216+ print (" Failed to sign manifest store: " + str (e))
217+ exit (1 )
233218 ```
0 commit comments