hashed_uri.rs (2337B)
1 // Copyright 2022 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 std::fmt; 15 16 #[cfg(feature = "json_schema")] 17 use schemars::JsonSchema; 18 use serde::{Deserialize, Serialize}; 19 20 /// Hashed Uri structure as defined by C2PA spec 21 /// It is annotated to produce the correctly tagged cbor serialization 22 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] 23 #[cfg_attr(feature = "json_schema", derive(JsonSchema))] 24 pub struct HashedUri { 25 url: String, // URI stored as tagged cbor 26 #[serde(skip_serializing_if = "Option::is_none")] 27 alg: Option<String>, 28 #[serde(with = "serde_bytes")] 29 #[cfg_attr(feature = "json_schema", schemars(with = "Vec<u8>"))] 30 hash: Vec<u8>, // hash stored as cbor byte string 31 32 // salt used to generate hash 33 #[serde(skip_deserializing, skip_serializing)] 34 salt: Option<Vec<u8>>, 35 } 36 37 impl HashedUri { 38 pub fn new(url: String, alg: Option<String>, hash_bytes: &[u8]) -> Self { 39 Self { 40 url, 41 alg, 42 hash: hash_bytes.to_vec(), 43 salt: None, 44 } 45 } 46 47 pub fn url(&self) -> String { 48 self.url.clone() 49 } 50 51 pub fn is_relative_url(&self) -> bool { 52 crate::jumbf::labels::manifest_label_from_uri(&self.url).is_none() 53 } 54 55 pub fn alg(&self) -> Option<String> { 56 self.alg.clone() 57 } 58 59 pub fn hash(&self) -> Vec<u8> { 60 self.hash.clone() 61 } 62 63 pub(crate) fn update_hash(&mut self, hash: Vec<u8>) { 64 self.hash = hash; 65 } 66 67 pub fn add_salt(&mut self, salt: Option<Vec<u8>>) { 68 self.salt = salt; 69 } 70 71 pub const fn salt(&self) -> &Option<Vec<u8>> { 72 &self.salt 73 } 74 } 75 76 impl fmt::Display for HashedUri { 77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 78 write!(f, "url: {}, alg: {:?}, hash", self.url, self.alg) 79 } 80 }