1
+ from c2pa import Builder , Error , Reader , SigningAlg , create_signer , sdk_version , sign_ps256
2
+ import os
3
+ import io
4
+ PROJECT_PATH = os .getcwd ()
5
+
6
+ testPath = os .path .join (PROJECT_PATH , "tests" , "fixtures" , "C.jpg" )
7
+
8
+ manifestDefinition = {
9
+ "claim_generator" : "python_test" ,
10
+ "claim_generator_info" : [{
11
+ "name" : "python_test" ,
12
+ "version" : "0.0.1" ,
13
+ }],
14
+ "format" : "image/jpeg" ,
15
+ "title" : "Python Test Image" ,
16
+ "ingredients" : [],
17
+ "assertions" : [
18
+ { 'label' : 'stds.schema-org.CreativeWork' ,
19
+ 'data' : {
20
+ '@context' : 'http://schema.org/' ,
21
+ '@type' : 'CreativeWork' ,
22
+ 'author' : [
23
+ { '@type' : 'Person' ,
24
+ 'name' : 'Gavin Peacock'
25
+ }
26
+ ]
27
+ },
28
+ 'kind' : 'Json'
29
+ }
30
+ ]
31
+ }
32
+ private_key = open ("tests/fixtures/ps256.pem" ,"rb" ).read ()
33
+
34
+ # Define a function that signs data with PS256 using a private key
35
+ def sign (data : bytes ) -> bytes :
36
+ print ("date len = " , len (data ))
37
+ return sign_ps256 (data , private_key )
38
+
39
+ # load the public keys from a pem file
40
+ certs = open ("tests/fixtures/ps256.pub" ,"rb" ).read ()
41
+
42
+ # Create a local Ps256 signer with certs and a timestamp server
43
+ signer = create_signer (sign , SigningAlg .PS256 , certs , "http://timestamp.digicert.com" )
44
+
45
+ builder = Builder (manifestDefinition )
46
+
47
+ source = open (testPath , "rb" ).read ()
48
+
49
+ testPath = "/Users/gpeacock/Pictures/Lightroom Saved Photos/IMG_0483.jpg"
50
+ testPath = "tests/fixtures/c.jpg"
51
+ outputPath = "target/python_out.jpg"
52
+
53
+ def test_files_build ():
54
+ # Delete the output file if it exists
55
+ if os .path .exists (outputPath ):
56
+ os .remove (outputPath )
57
+ builder .sign_file (signer , testPath , outputPath )
58
+
59
+ def test_streams_build ():
60
+ #with open(testPath, "rb") as file:
61
+ output = io .BytesIO (bytearray ())
62
+ builder .sign (signer , "image/jpeg" , io .BytesIO (source ), output )
63
+
64
+ def test_func (benchmark ):
65
+ benchmark (test_files_build )
66
+
67
+ def test_streams (benchmark ):
68
+ benchmark (test_streams_build )
69
+
70
+ #def test_signer(benchmark):
71
+ # benchmark(sign_ps256, data, private_key)
0 commit comments