proc_hash/
lib.rs

1use blake2::{Blake2b512, Blake2s256, Digest};
2use proc_macro::TokenStream;
3use quote::quote;
4use sha3::Sha3_512;
5use syn::LitStr;
6use whirlpool::Whirlpool;
7
8/// Simply converts a `TokenStream` to a `LitStr`
9macro_rules! ts_to_litstr {
10    ($item:ident) => {{
11        let arg = syn::parse_macro_input!($item as LitStr);
12        arg.value()
13    }};
14}
15/// Returns a `String` of the hash of type `$algo`.
16/// Only works with hash types implementing the `Digest` trait defined in [digest](https://crates.io/crates/digest)
17macro_rules! rscrypto_hash {
18    ($algo:ty, $val:ident) => {{
19        let data = $val.as_bytes();
20        let mut hasher = <$algo>::new();
21
22        hasher.update(data);
23
24        let res = hasher.finalize();
25        format!("{:x}", res)
26    }};
27}
28
29/// Takes a string and replaces it with it's [MD5](https://en.wikipedia.org/wiki/MD5) hash at compile time
30/// # Example
31#[doc = docify::embed_run!("tests/md5.rs", test_md5)]
32/// # Security Warning
33/// MD5 [should be considered cryptographically broken](https://www.kb.cert.org/vuls/id/836068) and be avoided for security critical
34/// applications if possible.
35#[proc_macro]
36pub fn include_md5(item: TokenStream) -> TokenStream {
37    let val = ts_to_litstr!(item);
38    let hash = md5::compute(val);
39    let hash = format!("{:x}", hash);
40    let out = quote! {
41        {
42            const HASH: &str = #hash;
43            HASH
44        }
45    };
46    out.into()
47}
48
49/// Takes a string and replaces it with it's [Blake2s256](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2) hash at compile time
50/// # Example
51#[doc = docify::embed_run!("tests/blake512.rs", test_blake512)]
52#[proc_macro]
53pub fn include_blake256(item: TokenStream) -> TokenStream {
54    let val = ts_to_litstr!(item);
55    let hash = rscrypto_hash!(Blake2s256, val);
56    let out = quote! {
57        {
58            const HASH: &str = #hash;
59            HASH
60        }
61    };
62    out.into()
63}
64
65/// Takes a string and replaces it with it's [Blake2b512](https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2) hash at compile time
66/// # Example
67#[doc = docify::embed_run!("tests/blake512.rs", test_blake512)]
68#[proc_macro]
69pub fn include_blake512(item: TokenStream) -> TokenStream {
70    let val = ts_to_litstr!(item);
71    let hash = rscrypto_hash!(Blake2b512, val);
72    let out = quote! {
73        {
74            const HASH: &str = #hash;
75            HASH
76        }
77    };
78    out.into()
79}
80
81/// Takes a string and replaces it with it's [SHA-3](https://en.wikipedia.org/wiki/SHA-3) hash at compile time
82/// # Example
83#[doc = docify::embed_run!("tests/sha3.rs", test_sha3)]
84#[proc_macro]
85pub fn include_sha3(item: TokenStream) -> TokenStream {
86    let val = ts_to_litstr!(item);
87    let hash = rscrypto_hash!(Sha3_512, val);
88
89    let out = quote! {
90        {
91            const HASH: &str = #hash;
92            HASH
93        }
94    };
95    out.into()
96}
97
98/// Takes a string and replaces it with it's [Whirlpool](https://en.wikipedia.org/wiki/Whirlpool_(hash_function)) hash at compile time
99/// # Example
100#[doc = docify::embed_run!("tests/whirlpool.rs", test_whirlpool)]
101#[proc_macro]
102pub fn include_whirlpool(item: TokenStream) -> TokenStream {
103    let val = ts_to_litstr!(item);
104    let hash = rscrypto_hash!(Whirlpool, val);
105
106    let out = quote! {
107        {
108            const HASH: &str = #hash;
109            HASH
110        }
111    };
112    out.into()
113}