Skip to main content

gix_hash/
lib.rs

1//! This crate provides types for identifying git objects using a hash digest.
2//!
3//! These are provided in [borrowed versions][oid] as well as an [owned one][ObjectId].
4//!
5//! ## Examples
6//!
7//! ```
8//! use gix_hash::{hasher, Kind, ObjectId, Prefix};
9//!
10//! let id = ObjectId::from_hex(b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391").unwrap();
11//! assert_eq!(id.kind(), Kind::Sha1);
12//! assert!(id.is_empty_blob());
13//!
14//! let prefix = Prefix::new(&id, 7).unwrap();
15//! assert_eq!(prefix.to_string(), "e69de29");
16//!
17//! let mut digest = hasher(Kind::Sha1);
18//! digest.update(id.as_slice());
19//! let hashed_bytes = digest.try_finalize().unwrap();
20//! assert_eq!(hashed_bytes.kind(), Kind::Sha1);
21//! assert_ne!(hashed_bytes, id);
22//! ```
23//! ## Feature Flags
24#![cfg_attr(
25    all(doc, feature = "document-features"),
26    doc = ::document_features::document_features!()
27)]
28#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
29#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
30
31#[cfg(all(not(feature = "sha1"), not(feature = "sha256")))]
32compile_error!("Please set either the `sha1` or the `sha256` feature flag");
33
34#[path = "oid.rs"]
35mod borrowed;
36pub use borrowed::{oid, Error};
37
38/// Hash functions and hash utilities
39pub mod hasher;
40pub use hasher::_impl::{hasher, Hasher};
41
42/// Error types for utility hash functions
43pub mod io;
44pub use io::_impl::{bytes, bytes_of_file, bytes_with_hasher};
45
46mod object_id;
47pub use object_id::{decode, ObjectId};
48
49///
50pub mod prefix;
51
52///
53pub mod verify;
54
55/// A partial, owned hash possibly identifying an object uniquely, whose non-prefix bytes are zeroed.
56///
57/// An example would `0000000000000000000000000000000032bd3242`, where `32bd3242` is the prefix,
58/// which would be able to match all hashes that *start with* `32bd3242`.
59#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy, Debug)]
60#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
61pub struct Prefix {
62    bytes: ObjectId,
63    hex_len: usize,
64}
65
66/// The size of a SHA1 hash digest in bytes.
67#[cfg(feature = "sha1")]
68const SIZE_OF_SHA1_DIGEST: usize = 20;
69/// The size of a SHA1 hash digest in hex.
70#[cfg(feature = "sha1")]
71const SIZE_OF_SHA1_HEX_DIGEST: usize = 2 * SIZE_OF_SHA1_DIGEST;
72
73/// The size of a SHA256 hash digest in bytes.
74#[cfg(feature = "sha256")]
75const SIZE_OF_SHA256_DIGEST: usize = 32;
76/// The size of a SHA256 hash digest in hex.
77#[cfg(feature = "sha256")]
78const SIZE_OF_SHA256_HEX_DIGEST: usize = 2 * SIZE_OF_SHA256_DIGEST;
79
80#[cfg(feature = "sha1")]
81const EMPTY_BLOB_SHA1: &[u8; SIZE_OF_SHA1_DIGEST] =
82    b"\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91";
83#[cfg(feature = "sha1")]
84const EMPTY_TREE_SHA1: &[u8; SIZE_OF_SHA1_DIGEST] =
85    b"\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04";
86
87#[cfg(feature = "sha256")]
88const EMPTY_BLOB_SHA256: &[u8; SIZE_OF_SHA256_DIGEST] = b"\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72\x18\x13";
89#[cfg(feature = "sha256")]
90const EMPTY_TREE_SHA256: &[u8; SIZE_OF_SHA256_DIGEST] = b"\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc\x53\x21";
91
92/// Denotes the kind of function to produce a [`ObjectId`].
93#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
94#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
95#[non_exhaustive]
96pub enum Kind {
97    /// The SHA1 hash with 160 bits.
98    #[cfg_attr(feature = "sha1", default)]
99    #[cfg(feature = "sha1")]
100    Sha1 = 1,
101    /// The SHA256 hash with 256 bits.
102    #[cfg_attr(all(not(feature = "sha1"), feature = "sha256"), default)]
103    #[cfg(feature = "sha256")]
104    Sha256 = 2,
105}
106
107mod kind;