bybit/
util.rs

1use hex;
2use ring::hmac;
3use std::time::SystemTime;
4
5pub fn millis() -> u128 {
6    SystemTime::now()
7        .duration_since(SystemTime::UNIX_EPOCH)
8        .unwrap()
9        .as_millis()
10}
11
12pub fn sign(secret: &str, msg: &str) -> String {
13    let key = hmac::Key::new(hmac::HMAC_SHA256, secret.as_bytes());
14    let tag = hmac::sign(&key, msg.as_bytes());
15    hex::encode(tag.as_ref())
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn test_millseconds() {
24        assert!(millis() > 0);
25    }
26
27    #[test]
28    fn test_sign() {
29        assert_eq!(
30            sign("secret", "message"),
31            String::from("8b5f48702995c1598c573db1e21866a9b825d4a794d169d7060a03605796360b")
32        );
33    }
34}