|
1 | 1 | ---
|
2 | 2 | id: build
|
3 | 3 | title: Building a manifest
|
| 4 | +hide_table_of_contents: true |
4 | 5 | ---
|
5 | 6 |
|
6 | 7 | import Tabs from '@theme/Tabs';
|
7 | 8 | import TabItem from '@theme/TabItem';
|
8 | 9 |
|
9 | 10 | <Tabs groupId="programming-lang">
|
10 | 11 |
|
11 |
| -<TabItem value="python" label="Python" default> |
12 |
| - This is how to build a manifest using Python. |
13 |
| -</TabItem> |
| 12 | + <TabItem value="python" label="Python" default> |
14 | 13 |
|
15 |
| -<TabItem value="cpp" label="C++"> |
16 |
| - This is how to build a manifest using C++. |
17 |
| -</TabItem> |
| 14 | +```python |
| 15 | +try: |
| 16 | + # Define a function to sign the claim bytes |
| 17 | + # In this case we are using a pre-defined sign_ps256 method, passing in our private cert |
| 18 | + # Normally this cert would be kept safe in some other location |
| 19 | + def private_sign(data: bytes) -> bytes: |
| 20 | + return sign_ps256(data, "tests/fixtures/ps256.pem") |
18 | 21 |
|
19 |
| -{' '} |
20 |
| -<TabItem value="node" label="Node.js"> |
21 |
| - This is how to build a manifest using Node.js. |
22 |
| -</TabItem> |
| 22 | + # read our public certs into memory |
| 23 | + certs = open(data_dir + "ps256.pub", "rb").read() |
| 24 | + |
| 25 | + # Create a signer from the private signer, certs and a time stamp service url |
| 26 | + signer = create_signer(private_sign, SigningAlg.PS256, certs, "http://timestamp.digicert.com") |
| 27 | + |
| 28 | + # Create a builder add a thumbnail resource and an ingredient file. |
| 29 | + builder = Builder(manifest_json) |
| 30 | + |
| 31 | + # Add the resource from a stream |
| 32 | + a_thumbnail_jpg_stream = open("tests/fixtures/A_thumbnail.jpg", "rb") |
| 33 | + builder.add_resource("image/jpeg", a_thumbnail_jpg_stream) |
| 34 | + |
| 35 | + # Define an ingredient, in this case a parent ingredient named A.jpg, with a thumbnail |
| 36 | + ingredient_json = { |
| 37 | + "title": "A.jpg", |
| 38 | + "relationship": "parentOf", # "parentOf", "componentOf" or "inputTo" |
| 39 | + "thumbnail": { |
| 40 | + "identifier": "thumbnail", |
| 41 | + "format": "image/jpeg" |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + # Add the ingredient from a stream |
| 46 | + a_jpg_stream = open("tests/fixtures/A.jpg", "rb") |
| 47 | + builder.add_ingredient("image/jpeg", a_jpg_stream) |
| 48 | + |
| 49 | + # At this point we could archive or unarchive our Builder to continue later. |
| 50 | + # In this example we use a bytearray for the archive stream. |
| 51 | + # all ingredients and resources will be saved in the archive |
| 52 | + archive = io.BytesIO(bytearray()) |
| 53 | + builder.to_archive(archive) |
| 54 | + archive.seek() |
| 55 | + builder = builder.from_archive(archive) |
| 56 | + |
| 57 | + # Sign the builder with a stream and output it to a stream |
| 58 | + # This returns the binary manifest data that could be uploaded to cloud storage. |
| 59 | + input_stream = open("tests/fixtures/A.jpg", "rb") |
| 60 | + output_stream = open("target/out.jpg", "wb") |
| 61 | + c2pa_data = builder.sign(signer, "image/jpeg", input_stream, output_stream) |
| 62 | + |
| 63 | +except Exception as err: |
| 64 | + print(err) |
| 65 | +``` |
| 66 | + |
| 67 | + </TabItem> |
| 68 | + |
| 69 | + <TabItem value="node" label="Node.js"> |
| 70 | + |
| 71 | +```ts |
| 72 | +import { ManifestBuilder } from 'c2pa-node'; |
| 73 | + |
| 74 | +const manifest = new ManifestBuilder({ |
| 75 | + claim_generator: 'my-app/1.0.0', |
| 76 | + format: 'image/jpeg', |
| 77 | + title: 'node_test_local_signer.jpg', |
| 78 | + assertions: [ |
| 79 | + { |
| 80 | + label: 'c2pa.actions', |
| 81 | + data: { |
| 82 | + actions: [ |
| 83 | + { |
| 84 | + action: 'c2pa.created', |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + label: 'com.custom.my-assertion', |
| 91 | + data: { |
| 92 | + description: 'My custom test assertion', |
| 93 | + version: '1.0.0', |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | + </TabItem> |
| 101 | + |
| 102 | + <TabItem value="cpp" label="C++"> |
| 103 | + |
| 104 | +```cpp |
| 105 | +const std::string manifest_json = R"{ |
| 106 | + "claim_generator": "c2pa_c_test/0.1", |
| 107 | + "claim_generator_info": [ |
| 108 | + { |
| 109 | + "name": "c2pa-c test", |
| 110 | + "version": "0.1" |
| 111 | + } |
| 112 | + ], |
| 113 | + "assertions": [ |
| 114 | + { |
| 115 | + "label": "c2pa.training-mining", |
| 116 | + "data": { |
| 117 | + "entries": { |
| 118 | + "c2pa.ai_generative_training": { "use": "notAllowed" }, |
| 119 | + "c2pa.ai_inference": { "use": "notAllowed" }, |
| 120 | + "c2pa.ai_training": { "use": "notAllowed" }, |
| 121 | + "c2pa.data_mining": { "use": "notAllowed" } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | + }; |
| 127 | +
|
| 128 | +auto builder = Builder(manifest_json); |
| 129 | +
|
| 130 | +
|
| 131 | +``` |
| 132 | +
|
| 133 | + </TabItem> |
23 | 134 |
|
24 | 135 | <TabItem value="rust" label="Rust">
|
25 | 136 | This is how to build a manifest using Rust.
|
|
0 commit comments