usecrate::error::*; usecrate::{crypto, DigestAlgorithm}; /// A utility for hashing payloads. Feed your entity body to this, then pass the `finish` /// result to a request or response. pubstruct PayloadHasher(Box<dyn crypto::Hasher>);
impl PayloadHasher { /// Create a new PayloadHasher. The `content_type` should be lower-case and should /// not include parameters. The digest is assumed to be the same as the digest used /// for the credentials in the request. pubfn new<B>(content_type: B, algorithm: DigestAlgorithm) -> Result<Self> where
B: AsRef<[u8]>,
{ letmut hasher = PayloadHasher(crypto::new_hasher(algorithm)?);
hasher.update(b"hawk.1.payload\n")?;
hasher.update(content_type.as_ref())?;
hasher.update(b"\n")?;
Ok(hasher)
}
/// Hash a single value and return it pubfn hash<B1, B2>(
content_type: B1,
algorithm: DigestAlgorithm,
payload: B2,
) -> Result<Vec<u8>> where
B1: AsRef<[u8]>,
B2: AsRef<[u8]>,
{ letmut hasher = PayloadHasher::new(content_type, algorithm)?;
hasher.update(payload)?;
hasher.finish()
}
/// Update the hash with new data. pubfn update<B>(&mutself, data: B) -> Result<()> where
B: AsRef<[u8]>,
{ self.0.update(data.as_ref())?;
Ok(())
}
/// Finish hashing and return the result /// /// Note that this appends a newline to the payload, as does the JS Hawk implementaiton. pubfn finish(mutself) -> Result<Vec<u8>> { self.update(b"\n")?;
Ok(self.0.finish()?)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.