Skip to content
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

Support for Standard Hex dumps like Sha256 and Md5 #44

Closed
comath opened this issue Apr 17, 2020 · 6 comments
Closed

Support for Standard Hex dumps like Sha256 and Md5 #44

comath opened this issue Apr 17, 2020 · 6 comments

Comments

@comath
Copy link

comath commented Apr 17, 2020

I'm using this to save some memory working with a large amount of objects that each have a md5, sha256, sha512, etc...

I was wonder if you were interested in adding some official support for these common hex dumps?

For example, for md5:

#[derive(Debug,Hash,PartialEq,Eq,Default)]
pub struct Md5 {
    hash: [u8;16],
}
impl FromHex for Md5 {
....
}
impl FromHex for Md5 {
....
}

I'm working on getting this to integrate with Serde.

@KokaKiwi
Copy link
Owner

Actually in the latest version 0.4.2, serde is supported for serializing/de-serializing hex strings: https://docs.rs/hex/0.4.2/hex/serde/index.html

Also, FromHex is implemented for many bytes arrays of many sizes (kinda arbitrarily for the moment until const generics are a thing): https://github.com/KokaKiwi/rust-hex/blob/v0.4.2/src/lib.rs#L209-L219

(kinda hope it answers your question as i don't understand exactly what you're asking)

@comath
Copy link
Author

comath commented Apr 17, 2020

Yea, I think you have everything covered, I want to support deserializing json blobs like:

#[derive(Debug,Serialize, Deserialize)]
pub struct RlData {
    md5: Md5,
    sha1: Sha1,
    sha256: Sha256,
    sha384: Sha384,
    sha512: Sha512,
}

instead of having to specify the byte arrays at each step.
Maybe just a set of these:

pub type Md5Hex = [u8;16];
pub type Sha1Hex = [u8;20];
pub type Sha256Hex = [u8;32];
pub type Sha512Hex = [u8;64];
pub type Sha384Hex = [u8;64];

@KokaKiwi
Copy link
Owner

Well for this you can do this for example then:

#[derive(Debug,Serialize, Deserialize)]
pub struct RlData {
	#[serde(with = "hex")]
    md5: [u8;16],
	#[serde(with = "hex")]
    sha1: [u8;20],
	#[serde(with = "hex")]
    sha256: [u8;32],
	#[serde(with = "hex")]
    sha384: [u8;64],
	#[serde(with = "hex")]
    sha512: [u8;64],
}

@comath
Copy link
Author

comath commented Apr 17, 2020

I think a list of common hashes so we can code that by name (and not have to remember the sizes) would be helpful. This is just a cherry on top of a great crate, so it's just a quality of life thing.

@piegamesde
Copy link

I think the proposal here is strictly weaker than and superseded by #65. Furthermore, all cryptographic libraries that I've ever used all relied on type aliases, although they mainly used generic-array instead of plain bytes.

@comath
Copy link
Author

comath commented Mar 8, 2022

Sounds good, I'm happy to close this.

@comath comath closed this as completed Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants