-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcrypt.rs
33 lines (28 loc) · 790 Bytes
/
crypt.rs
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
use crypto::digest::Digest;
use crypto::hmac::Hmac;
use crypto::mac::{
Mac,
MacResult,
};
use rustc_serialize::base64::{
FromBase64,
ToBase64,
};
use BASE_CONFIG;
pub fn sign<D: Digest>(data: &str, key: &[u8], digest: D) -> String {
let mut hmac = Hmac::new(digest, key);
hmac.input(data.as_bytes());
let mac = hmac.result();
let code = mac.code();
(*code).to_base64(BASE_CONFIG)
}
pub fn verify<D: Digest>(target: &str, data: &str, key: &[u8], digest: D) -> bool {
let target_bytes = match target.from_base64() {
Ok(x) => x,
Err(_) => return false,
};
let target_mac = MacResult::new_from_owned(target_bytes);
let mut hmac = Hmac::new(digest, key);
hmac.input(data.as_bytes());
hmac.result() == target_mac
}