Commit c23dad8c authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

Use RsaIdentity::from_hex() and hex::decode_to_slice in more places

These aren't critical-path, but they do make the code a little nicer.
parent caf372ac
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -59,9 +59,8 @@ impl Authority {
pub(crate) fn default_authorities() -> Vec<Authority> {
    /// Build an authority; panic if input is bad.
    fn auth(name: &str, key: &str) -> Authority {
        let v3ident = hex::decode(key).expect("Built-in authority identity had bad hex!?");
        let v3ident = RsaIdentity::from_bytes(&v3ident)
            .expect("Built-in authority identity had wrong length!?");
        let v3ident =
            RsaIdentity::from_hex(key).expect("Built-in authority identity had bad hex!?");
        AuthorityBuilder::new()
            .name(name)
            .v3ident(v3ident)
+1 −3
Original line number Diff line number Diff line
@@ -331,9 +331,7 @@ mod fallbacks {
    pub(crate) fn default_fallbacks() -> Vec<super::FallbackDir> {
        /// Build a fallback directory; panic if input is bad.
        fn fallback(rsa: &str, ed: &str, ports: &[&str]) -> FallbackDir {
            let rsa = hex::decode(rsa).expect("Bad hex in built-in fallback list");
            let rsa =
                RsaIdentity::from_bytes(&rsa).expect("Wrong length in built-in fallback list");
            let rsa = RsaIdentity::from_hex(rsa).expect("Bad hex in built-in fallback list");
            let ed = base64::decode_config(ed, base64::STANDARD_NO_PAD)
                .expect("Bad hex in built-in fallback list");
            let ed =
+1 −2
Original line number Diff line number Diff line
@@ -1005,8 +1005,7 @@ mod test {
        datetime!(2020-08-07 12:42:45 UTC).into()
    }
    fn rsa(s: &str) -> RsaIdentity {
        let k = hex::decode(s).unwrap();
        RsaIdentity::from_bytes(&k[..]).unwrap()
        RsaIdentity::from_hex(s).unwrap()
    }
    fn test_authorities() -> Vec<Authority> {
        fn a(s: &str) -> Authority {
+4 −8
Original line number Diff line number Diff line
@@ -608,20 +608,16 @@ impl Drop for Unlinker {

/// Convert a hexadecimal sha3-256 digest from the database into an array.
fn digest_from_hex(s: &str) -> Result<[u8; 32]> {
    hex::decode(s)
        .map_err(Error::BadHexInCache)?
        .try_into()
        .map_err(|_| Error::CacheCorruption("Invalid digest in database"))
    let mut bytes = [0_u8; 32];
    hex::decode_to_slice(s, &mut bytes[..]).map_err(Error::BadHexInCache)?;
    Ok(bytes)
}

/// Convert a hexadecimal sha3-256 "digest string" as used in the
/// digest column from the database into an array.
fn digest_from_dstr(s: &str) -> Result<[u8; 32]> {
    if let Some(stripped) = s.strip_prefix("sha3-256-") {
        hex::decode(stripped)
            .map_err(Error::BadHexInCache)?
            .try_into()
            .map_err(|_| Error::CacheCorruption("Invalid digest in database"))
        digest_from_hex(stripped)
    } else {
        Err(Error::CacheCorruption("Invalid digest in database"))
    }