1use hex;
2use hmac::{Hmac, Mac};
3use sha2::Sha256;
4use std::time::SystemTime;
5
6type HmacSha256 = Hmac<Sha256>;
7
8pub fn millis() -> u128 {
9 SystemTime::now()
10 .duration_since(SystemTime::UNIX_EPOCH)
11 .unwrap()
12 .as_millis()
13}
14
15pub fn sign(secret: &str, msg: &str) -> String {
16 hex::encode(
17 HmacSha256::new_from_slice(secret.as_bytes())
18 .expect("HMAC can take key of any size")
19 .chain_update(msg.as_bytes())
20 .finalize()
21 .into_bytes(),
22 )
23}
24
25#[cfg(test)]
26mod tests {
27 use super::*;
28
29 #[test]
30 fn test_milliseconds() {
31 assert!(millis() > 0);
32 }
33
34 #[test]
35 fn test_sign() {
36 assert_eq!(
37 sign("secret", "message"),
38 String::from("8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b")
39 );
40 }
41}