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