revisionstore: add sha1 and blake2 ContentHash methods

Summary: Add utility methods to ContentHash for use in computing aux data

Reviewed By: DurhamG

Differential Revision: D29659855

fbshipit-source-id: fb5c9749899147ea03dbb9e0e19b492c62bde2dd
This commit is contained in:
Meyer Jacobs 2021-07-13 15:15:39 -07:00 committed by Facebook GitHub Bot
parent 8757d2804f
commit 98e7e342c6
3 changed files with 35 additions and 1 deletions

View File

@ -61,6 +61,7 @@ pub use crate::history::{
pub use crate::metadata::{
AnyFileContentId, ContentId, DirectoryMetadata, DirectoryMetadataRequest, FileMetadata,
FileMetadataRequest, FileType, FsnodeId, Sha1, Sha256, CONTENT_ID_HASH_LENGTH_BYTES,
SHA1_HASH_LENGTH_BYTES,
};
pub use crate::token::{
FileContentTokenMetadata, UploadToken, UploadTokenData, UploadTokenMetadata,

View File

@ -10,6 +10,7 @@ async-runtime = { path = "../async-runtime" }
async-trait = "0.1.45"
auth = { path = "../auth" }
bincode = "1.3.3"
blake2 = "0.8"
byteorder = "1.3"
configparser = { path = "../configparser" }
crossbeam = "0.8"

View File

@ -5,13 +5,15 @@
* GNU General Public License version 2.
*/
use std::io::Write;
use minibytes::Bytes;
use serde_derive::{Deserialize, Serialize};
use sha2::Digest;
#[cfg(any(test, feature = "for-tests"))]
use rand::Rng;
use edenapi_types::{ContentId, Sha1, CONTENT_ID_HASH_LENGTH_BYTES, SHA1_HASH_LENGTH_BYTES};
use types::{Key, Sha256};
/// Kind of content hash stored in the LFS pointer. Adding new types is acceptable, re-ordering or
@ -41,6 +43,8 @@ pub enum StoreKey {
impl ContentHash {
pub fn sha256(data: &Bytes) -> Self {
use sha2::Digest;
let mut hash = sha2::Sha256::new();
hash.input(data);
@ -48,6 +52,34 @@ impl ContentHash {
ContentHash::Sha256(Sha256::from(bytes))
}
pub(crate) fn content_id(data: &Bytes) -> ContentId {
use blake2::{digest::Input, digest::VariableOutput, VarBlake2b};
// cribbed from pyedenapi
let mut hash = VarBlake2b::new_keyed(b"content", CONTENT_ID_HASH_LENGTH_BYTES);
hash.input(data);
let mut ret = [0u8; CONTENT_ID_HASH_LENGTH_BYTES];
hash.variable_result(|res| {
if let Err(e) = ret.as_mut().write_all(res) {
panic!(
"{}-byte array must work with {}-byte blake2b: {:?}",
CONTENT_ID_HASH_LENGTH_BYTES, CONTENT_ID_HASH_LENGTH_BYTES, e
);
}
});
ContentId(ret)
}
pub(crate) fn sha1(data: &Bytes) -> Sha1 {
use sha1::Digest;
let mut hash = sha1::Sha1::new();
hash.input(data);
let bytes: [u8; SHA1_HASH_LENGTH_BYTES] = hash.result().into();
Sha1::from(bytes)
}
pub fn unwrap_sha256(self) -> Sha256 {
match self {
ContentHash::Sha256(hash) => hash,