c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

test_signer.rs (1676B)


      1 // Copyright 2024 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 use c2pa::{CallbackSigner, SigningAlg};
     15 
     16 const CERTS: &[u8] = include_bytes!("../../tests/fixtures/certs/ed25519.pub");
     17 const PRIVATE_KEY: &[u8] = include_bytes!("../../tests/fixtures/certs/ed25519.pem");
     18 
     19 pub fn test_signer() -> CallbackSigner {
     20     let ed_signer = |_context: *const _, data: &[u8]| ed_sign(data, PRIVATE_KEY);
     21     CallbackSigner::new(ed_signer, SigningAlg::Ed25519, CERTS)
     22         .set_context("test" as *const _ as *const ())
     23 }
     24 
     25 fn ed_sign(data: &[u8], private_key: &[u8]) -> c2pa::Result<Vec<u8>> {
     26     use ed25519_dalek::{Signature, Signer, SigningKey};
     27     use pem::parse;
     28 
     29     // Parse the PEM data to get the private key
     30     let pem = parse(private_key).map_err(|e| c2pa::Error::OtherError(Box::new(e)))?;
     31     // For Ed25519, the key is 32 bytes long, so we skip the first 16 bytes of the PEM data
     32     let key_bytes = &pem.contents()[16..];
     33     let signing_key =
     34         SigningKey::try_from(key_bytes).map_err(|e| c2pa::Error::OtherError(Box::new(e)))?;
     35     // Sign the data
     36     let signature: Signature = signing_key.sign(data);
     37 
     38     Ok(signature.to_bytes().to_vec())
     39 }