swap hexlower for base64
diff --git a/src/crypto.rs b/src/crypto.rs
index 25dd528..3314eba 100644
--- a/src/crypto.rs
+++ b/src/crypto.rs
@@ -1,6 +1,6 @@
//! Cryptographic structures and functions.
-use data_encoding::HEXLOWER;
+use data_encoding::BASE64;
use derp::{self, Der, Tag};
use pem::{self, Pem};
use ring;
@@ -39,10 +39,10 @@
/// let mut map = HashMap::new();
/// assert!(hash_preference(&map).is_err());
///
-/// let _ = map.insert(HashAlgorithm::Sha512, HashValue::from_hex("abcd").unwrap());
+/// let _ = map.insert(HashAlgorithm::Sha512, HashValue::new(vec![0x00, 0x01]));
/// assert_eq!(hash_preference(&map).unwrap().0, &HashAlgorithm::Sha512);
///
-/// let _ = map.insert(HashAlgorithm::Sha256, HashValue::from_hex("0123").unwrap());
+/// let _ = map.insert(HashAlgorithm::Sha256, HashValue::new(vec![0x02, 0x03]));
/// assert_eq!(hash_preference(&map).unwrap().0, &HashAlgorithm::Sha512);
/// ```
pub fn hash_preference<'a>(
@@ -104,13 +104,13 @@
"Hex key ID must be 64 characters long".into(),
));
}
- Ok(KeyId(HEXLOWER.decode(string.as_bytes())?))
+ Ok(KeyId(BASE64.decode(string.as_bytes())?))
}
}
impl Debug for KeyId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "KeyId {{ \"{}\" }}", HEXLOWER.encode(&self.0))
+ write!(f, "KeyId {{ \"{}\" }}", BASE64.encode(&self.0))
}
}
@@ -120,7 +120,7 @@
S: Serializer,
{
let mut s = ser.serialize_tuple_struct("KeyId", 1)?;
- s.serialize_field(&HEXLOWER.encode(&self.0))?;
+ s.serialize_field(&BASE64.encode(&self.0))?;
s.end()
}
}
@@ -196,13 +196,13 @@
/// Create a new `SignatureValue` from the given hex-lower string.
pub fn from_string(string: &str) -> Result<Self> {
- Ok(SignatureValue(HEXLOWER.decode(string.as_bytes())?))
+ Ok(SignatureValue(BASE64.decode(string.as_bytes())?))
}
}
impl Debug for SignatureValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "SignatureValue {{ \"{}\" }}", HEXLOWER.encode(&self.0))
+ write!(f, "SignatureValue {{ \"{}\" }}", BASE64.encode(&self.0))
}
}
@@ -212,7 +212,7 @@
S: Serializer,
{
let mut s = ser.serialize_tuple_struct("SignatureValue", 1)?;
- s.serialize_field(&HEXLOWER.encode(&self.0))?;
+ s.serialize_field(&BASE64.encode(&self.0))?;
s.end()
}
}
@@ -309,7 +309,7 @@
}
}
-/// A structure containing information about a public key.
+/// A structure containing information about a private key.
pub struct PrivateKey {
private: PrivateKeyType,
public: PublicKey,
@@ -622,16 +622,6 @@
pub struct HashValue(Vec<u8>);
impl HashValue {
- /// Parse a hex-lower string and return a `HashValue`.
- ///
- /// ```
- /// use tuf::crypto::HashValue;
- /// assert_eq!(HashValue::from_hex("abcd").unwrap().value(), &[0xab, 0xcd]);
- /// ```
- pub fn from_hex(s: &str) -> Result<Self> {
- Ok(HashValue(HEXLOWER.decode(s.as_bytes())?))
- }
-
/// Create a new `HashValue` from the given digest bytes.
pub fn new(bytes: Vec<u8>) -> Self {
HashValue(bytes)
@@ -645,13 +635,13 @@
impl Debug for HashValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "HashValue {{ \"{}\" }}", HEXLOWER.encode(&self.0))
+ write!(f, "HashValue {{ \"{}\" }}", BASE64.encode(&self.0))
}
}
impl Display for HashValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}", HEXLOWER.encode(&self.0))
+ write!(f, "{}", BASE64.encode(&self.0))
}
}
diff --git a/src/metadata.rs b/src/metadata.rs
index 5f85b07..9397a91 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -605,7 +605,7 @@
/// assert_eq!(path.components::<JsonDataInterchange>(&MetadataVersion::Number(1)),
/// ["foo".to_string(), "1.bar.json".to_string()]);
/// assert_eq!(path.components::<JsonDataInterchange>(
- /// &MetadataVersion::Hash(HashValue::from_hex("abcd").unwrap())),
+ /// &MetadataVersion::Hash(HashValue::new(vec![0x69, 0xb7, 0x1d]))),
/// ["foo".to_string(), "abcd.bar.json".to_string()]);
/// ```
pub fn components<D>(&self, version: &MetadataVersion) -> Vec<String>
@@ -872,23 +872,29 @@
/// Read the from the given reader and calculate the length and hash values.
///
/// ```
+ /// extern crate data_encoding;
+ /// extern crate tuf;
+ /// use data_encoding::BASE64;
/// use tuf::crypto::{HashAlgorithm,HashValue};
/// use tuf::metadata::TargetDescription;
///
- /// let bytes: &[u8] = b"it was a pleasure to burn";
- /// let target_description = TargetDescription::from_reader(bytes).unwrap();
+ /// fn main() {
+ /// let bytes: &[u8] = b"it was a pleasure to burn";
+ /// let target_description = TargetDescription::from_reader(bytes).unwrap();
///
- /// // $ printf 'it was a pleasure to burn' | sha256sum
- /// let sha256 = HashValue::from_hex("45df7395bceb7567de2fb8272048b4\
- /// e57f98bf64c2a72e2aa9933537bd99590b").unwrap();
- /// // $ printf 'it was a pleasure to burn' | sha512sum
- /// let sha512 = HashValue::from_hex("b6e231c0ac9b61dbc9a56b948fa76e6efa70864028\
- /// cd607a84c248473aa7da339476d0d3060dfcd5bad5e0a054d7328ff064a1a0\
- /// 9b9712d09fe4d9034c210981").unwrap();
+ /// // $ printf 'it was a pleasure to burn' | sha256sum
+ /// let s = "Rd9zlbzrdWfeL7gnIEi05X+Yv2TCpy4qqZM1N72ZWQs=";
+ /// let sha256 = HashValue::new(BASE64.decode(s.as_bytes()).unwrap());
///
- /// assert_eq!(target_description.length(), bytes.len() as u64);
- /// assert_eq!(target_description.hashes().get(&HashAlgorithm::Sha256), Some(&sha256));
- /// assert_eq!(target_description.hashes().get(&HashAlgorithm::Sha512), Some(&sha512));
+ /// // $ printf 'it was a pleasure to burn' | sha512sum
+ /// let s ="tuIxwKybYdvJpWuUj6dubvpwhkAozWB6hMJIRzqn2jOUdtDTBg381brV4K\
+ /// BU1zKP8GShoJuXEtCf5NkDTCEJgQ==";
+ /// let sha512 = HashValue::new(BASE64.decode(s.as_bytes()).unwrap());
+ ///
+ /// assert_eq!(target_description.length(), bytes.len() as u64);
+ /// assert_eq!(target_description.hashes().get(&HashAlgorithm::Sha256), Some(&sha256));
+ /// assert_eq!(target_description.hashes().get(&HashAlgorithm::Sha512), Some(&sha512));
+ /// }
/// ```
pub fn from_reader<R>(mut read: R) -> Result<Self>
where