1use blake2::{Blake2b512, Blake2s256, Digest};
2use proc_macro::TokenStream;
3use quote::quote;
4use sha3::Sha3_512;
5use syn::LitStr;
6use whirlpool::Whirlpool;
7
8macro_rules! ts_to_litstr {
10 ($item:ident) => {{
11 let arg = syn::parse_macro_input!($item as LitStr);
12 arg.value()
13 }};
14}
15macro_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#[doc = docify::embed_run!("tests/md5.rs", test_md5)]
32#[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#[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#[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#[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#[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}