simple-c2pa

This is a fork of https://gitlab.com/guardianproject/proofmode/simple-c2pa
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/simple-c2pa.git
Log | Files | Refs | README | LICENSE

examples.rs (4486B)


      1 #[cfg(test)]
      2 pub mod tests {
      3     use simple_c2pa::{
      4         create_content_credentials_certificate, create_root_certificate, ApplicationInfo,
      5         ContentCredentials, ExifData, FileData,
      6     };
      7     use std::fs;
      8 
      9     #[test]
     10     fn basic_example() {
     11         let image_path = "tests/media/test-1.jpg";
     12         let file_name = image_path.split("/").last().unwrap().to_string();
     13         let file_data = fs::read(image_path).expect("Can't read image");
     14 
     15         let root_certificate = create_root_certificate(None, None).unwrap();
     16         let content_credentials_certificate =
     17             create_content_credentials_certificate(Some(root_certificate.clone()), None, None)
     18                 .unwrap();
     19 
     20         let file = FileData::new(None, Some(file_data), Some(file_name.clone()));
     21         let cc = ContentCredentials::new(content_credentials_certificate, file, None);
     22         cc.add_created_assertion().unwrap();
     23         let output_path = format!("outputs/c2pa-basic-{}", file_name);
     24         let file_data = cc.embed_manifest(Some(output_path.clone())).unwrap();
     25         fs::write(output_path, file_data.get_bytes().unwrap()).expect("Can't write file");
     26     }
     27 
     28     #[test]
     29     fn complex_example() {
     30         let image_path = "tests/media/test-1.jpg";
     31         let file_name = image_path.split("/").last().unwrap().to_string();
     32         let file_data = fs::read(image_path).expect("Can't read image");
     33         let fingerprint = "BA08 71E8 0200 B95D 8297  7ED0 4D1E C37F 88A7 FDCE".to_string();
     34 
     35         let organization = "Sample Organization".to_string();
     36         let root_certificate = create_root_certificate(Some(organization.clone()), None).unwrap();
     37         let root_bytes = root_certificate.get_certificate_bytes().unwrap();
     38         let root_path = "outputs/c2pa-root-certificate.crt";
     39         fs::write(root_path, root_bytes).expect("Can't write file");
     40 
     41         let content_credentials_certificate = create_content_credentials_certificate(
     42             Some(root_certificate.clone()),
     43             Some(organization.clone()),
     44             None,
     45         )
     46         .unwrap();
     47         let content_bytes = content_credentials_certificate
     48             .get_certificate_bytes()
     49             .unwrap();
     50         let content_path = "outputs/c2pa-credentials-certificate.crt";
     51         fs::write(content_path, content_bytes).expect("Can't write file");
     52 
     53         let file = FileData::new(None, Some(file_data), Some(file_name.clone()));
     54         let app_info = ApplicationInfo::new("SampleApp".to_string(), "1.0.0".to_string(), None);
     55         let cc = ContentCredentials::new(content_credentials_certificate, file, Some(app_info));
     56         cc.add_created_assertion().unwrap();
     57 
     58         let exif_data = ExifData {
     59             gps_version_id: Some("2.2.0.0".to_string()),
     60             latitude: Some("39,21.102N".to_string()),
     61             longitude: Some("74,26.5737W".to_string()),
     62             altitude_ref: Some(0),
     63             altitude: Some("100963/29890".to_string()),
     64             timestamp: Some("2019-09-22T18:22:57Z".to_string()),
     65             speed_ref: Some("K".to_string()),
     66             speed: Some("4009/161323".to_string()),
     67             direction_ref: Some("T".to_string()),
     68             direction: Some("296140/911".to_string()),
     69             destination_bearing_ref: Some("T".to_string()),
     70             destination_bearing: Some("296140/911".to_string()),
     71             positioning_error: Some("13244/2207".to_string()),
     72             exposure_time: Some("1/100".to_string()),
     73             f_number: Some(4.0),
     74             color_space: Some(1),
     75             digital_zoom_ratio: Some(2.0),
     76             make: Some("ProofMode".to_string()),
     77             model: Some("ProofMode In-App Camera v2.0".to_string()),
     78             lens_make: Some("CameraCompany".to_string()),
     79             lens_model: Some("17.0-35.0 mm".to_string()),
     80             lens_specification: Some(vec![1.55, 4.2, 1.6, 2.4]),
     81         };
     82         cc.add_exif_assertion(exif_data).unwrap();
     83         cc.add_instagram_assertion("johndoe".to_string(), "John Doe".to_string())
     84             .unwrap();
     85         cc.add_pgp_assertion(fingerprint, "88A7FDCE".to_string())
     86             .unwrap();
     87         cc.add_website_assertion("https://redaranj.com".to_string())
     88             .unwrap();
     89         let output_path = format!("outputs/c2pa-complex-{}", file_name);
     90         let file_data = cc.embed_manifest(Some(output_path.clone())).unwrap();
     91         fs::write(output_path, file_data.get_bytes().unwrap()).expect("Can't write file");
     92     }
     93 }