1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use blake2::{Blake2b512, Digest};
use proc_macro::TokenStream;
use quote::quote;
use sha3::Sha3_512;
use syn::LitStr;
use whirlpool::Whirlpool;

/// Simply converts a `TokenStream` to a `LitStr`
macro_rules! ts_to_litstr {
    ($item:ident) => {{
        let arg = syn::parse_macro_input!($item as LitStr);
        arg.value()
    }};
}
/// Returns a `String` of the hash of type `$algo`.
/// Only works with hash types implementing the `Digest` trait defined in [digest](https://crates.io/crates/digest)
macro_rules! rscrypto_hash {
    ($algo:ty, $val:ident) => {{
        let data = $val.as_bytes();
        let mut hasher = <$algo>::new();

        hasher.update(data);

        let res = hasher.finalize();
        format!("{:x}", res)
    }};
}

/// Takes a string and replaces it with it's MD5 hash at compile time
/// # Example
#[doc = docify::embed_run!("tests/md5.rs", test_md5)]
/// # Security Warning
/// MD5 [should be considered cryptographically broken](https://www.kb.cert.org/uls/id/836068) and be avoided for security critical
/// applications if possible.
#[proc_macro]
pub fn include_md5(item: TokenStream) -> TokenStream {
    let val = ts_to_litstr!(item);
    let hash = md5::compute(val);
    let hash = format!("{:x}", hash);
    let out = quote! {
        {
            const HASH: &str = #hash;
            HASH
        }
    };
    out.into()
}

/// Takes a string and replaces it with it's Blake2b512 hash at compile time
/// # Example
#[doc = docify::embed_run!("tests/blake512.rs", test_blake512)]
#[proc_macro]
pub fn include_blake512(item: TokenStream) -> TokenStream {
    let val = ts_to_litstr!(item);
    let hash = rscrypto_hash!(Blake2b512, val);
    let out = quote! {
        {
            const HASH: &str = #hash;
            HASH
        }
    };
    out.into()
}

/// Takes a string and replaces it with it's Blake2b512 hash at compile time
/// # Example
#[doc = docify::embed_run!("tests/blake512.rs", test_blake512)]
#[proc_macro]
pub fn include_sha3(item: TokenStream) -> TokenStream {
    let val = ts_to_litstr!(item);
    let hash = rscrypto_hash!(Sha3_512, val);

    let out = quote! {
        {
            const HASH: &str = #hash;
            HASH
        }
    };
    out.into()
}

/// Takes a string and replaces it with it's [Whirlpool](https://en.wikipedia.org/wiki/Whirlpool_(hash_function)) hash at compile time
/// # Example
#[doc = docify::embed_run!("tests/md5.rs", test_md5)]
#[proc_macro]
pub fn include_whirlpool(item: TokenStream) -> TokenStream {
    let val = ts_to_litstr!(item);
    let hash = rscrypto_hash!(Whirlpool, val);

    let out = quote! {
        {
            const HASH: &str = #hash;
            HASH
        }
    };
    out.into()
}