-
-
Notifications
You must be signed in to change notification settings - Fork 409
Add ObjectId::Sha256 and Kind::Sha256
#2292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e2a1e4d
8afb22e
3eed1e6
ed3ac81
53d3b4c
0c9c0de
71021ad
5fc293c
e011f8b
90a3e57
3b05f1d
96231d8
9dd88d7
14a6cd3
770d922
7de47e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use crate::{oid, Kind, ObjectId}; | ||
| use crate::{oid, Kind, ObjectId, SIZE_OF_SHA1_DIGEST, SIZE_OF_SHA1_HEX_DIGEST}; | ||
|
|
||
| #[cfg(feature = "sha256")] | ||
| use crate::{SIZE_OF_SHA256_DIGEST, SIZE_OF_SHA256_HEX_DIGEST}; | ||
|
|
||
| impl TryFrom<u8> for Kind { | ||
| type Error = u8; | ||
|
|
||
| fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
| Ok(match value { | ||
| 1 => Kind::Sha1, | ||
| #[cfg(feature = "sha256")] | ||
| 2 => Kind::Sha256, | ||
| unknown => return Err(unknown), | ||
| }) | ||
| } | ||
|
|
@@ -19,6 +24,8 @@ impl FromStr for Kind { | |
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "sha1" | "SHA1" => Kind::Sha1, | ||
| #[cfg(feature = "sha256")] | ||
| "sha256" | "SHA256" => Kind::Sha256, | ||
| other => return Err(other.into()), | ||
| }) | ||
| } | ||
|
|
@@ -28,6 +35,8 @@ impl std::fmt::Display for Kind { | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| Kind::Sha1 => f.write_str("SHA1"), | ||
| #[cfg(feature = "sha256")] | ||
| Kind::Sha256 => f.write_str("SHA256"), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -36,13 +45,27 @@ impl Kind { | |
| /// Returns the shortest hash we support. | ||
| #[inline] | ||
| pub const fn shortest() -> Self { | ||
| Self::Sha1 | ||
| #[cfg(all(not(feature = "sha1"), feature = "sha256"))] | ||
| { | ||
| Self::Sha256 | ||
| } | ||
| #[cfg(feature = "sha1")] | ||
| { | ||
| Self::Sha1 | ||
| } | ||
| } | ||
|
|
||
| /// Returns the longest hash we support. | ||
| #[inline] | ||
| pub const fn longest() -> Self { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that there is this logic, it feels like tests for |
||
| Self::Sha1 | ||
| #[cfg(feature = "sha256")] | ||
| { | ||
| Self::Sha256 | ||
| } | ||
| #[cfg(all(not(feature = "sha256"), feature = "sha1"))] | ||
| { | ||
| Self::Sha1 | ||
| } | ||
| } | ||
|
|
||
| /// Returns a buffer suitable to hold the longest possible hash in hex. | ||
|
|
@@ -61,23 +84,31 @@ impl Kind { | |
| #[inline] | ||
| pub const fn len_in_hex(&self) -> usize { | ||
| match self { | ||
| Kind::Sha1 => 40, | ||
| Kind::Sha1 => SIZE_OF_SHA1_HEX_DIGEST, | ||
| #[cfg(feature = "sha256")] | ||
| Kind::Sha256 => SIZE_OF_SHA256_HEX_DIGEST, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the amount of bytes taken up by the hash of this instance. | ||
| #[inline] | ||
| pub const fn len_in_bytes(&self) -> usize { | ||
| match self { | ||
| Kind::Sha1 => 20, | ||
| Kind::Sha1 => SIZE_OF_SHA1_DIGEST, | ||
| #[cfg(feature = "sha256")] | ||
| Kind::Sha256 => SIZE_OF_SHA256_DIGEST, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the kind of hash that would fit the given `hex_len`, or `None` if there is no fitting hash. | ||
| /// Note that `0` as `hex_len` up to 40 always yields `Sha1`. | ||
| /// Note that `0` as `hex_len` up to 40 always yields `SHA1` while anything in the range 41..64 | ||
| /// always yields `SHA256` if it is enabled. | ||
| #[inline] | ||
| pub const fn from_hex_len(hex_len: usize) -> Option<Self> { | ||
| Some(match hex_len { | ||
| 0..=40 => Kind::Sha1, | ||
| 0..=SIZE_OF_SHA1_HEX_DIGEST => Kind::Sha1, | ||
| #[cfg(feature = "sha256")] | ||
| 0..=SIZE_OF_SHA256_HEX_DIGEST => Kind::Sha256, | ||
| _ => return None, | ||
| }) | ||
| } | ||
|
|
@@ -93,7 +124,9 @@ impl Kind { | |
| #[inline] | ||
| pub(crate) fn from_len_in_bytes(bytes: usize) -> Self { | ||
| match bytes { | ||
| 20 => Kind::Sha1, | ||
| SIZE_OF_SHA1_DIGEST => Kind::Sha1, | ||
| #[cfg(feature = "sha256")] | ||
| SIZE_OF_SHA256_DIGEST => Kind::Sha256, | ||
| _ => panic!("BUG: must be called only with valid hash lengths produced by len_in_bytes()"), | ||
| } | ||
| } | ||
|
|
@@ -103,6 +136,8 @@ impl Kind { | |
| pub fn null_ref(&self) -> &'static oid { | ||
| match self { | ||
| Kind::Sha1 => oid::null_sha1(), | ||
| #[cfg(feature = "sha256")] | ||
| Kind::Sha256 => oid::null_sha256(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -111,6 +146,8 @@ impl Kind { | |
| pub const fn null(&self) -> ObjectId { | ||
| match self { | ||
| Kind::Sha1 => ObjectId::null_sha1(), | ||
| #[cfg(feature = "sha256")] | ||
| Kind::Sha256 => ObjectId::null_sha256(), | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right, I'd put the PR back to draft until a Sha256 hashes is available.
I also don't think it can be
unimplemented!(), but… maybe it would be an intermediate option if there is no other way now and there is a plan to fix it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that! I should have noticed, but didn’t that
Hasheris particular to SHA1. I was too focused on other things. :-)