Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extern crate rust_sodium;
use rust_sodium::crypto::auth::hmacsha256;
pub mod crypto {
#[derive(Debug)]
pub enum ErrType {
BadKey,
}
//
// Generate APPSECRET_PROOF, test for Facebook API
//
// # Arguments:
// * `key` - Key for secret proof
// * `data` - Data
// # Return
// * Result<String, ErrType>
//
pub fn generate_appsecret_proof(key: &str, data: &str) -> Result<String, ErrType> {
let auth_key = ::hmacsha256::Key::from_slice(key.as_bytes());
match auth_key {
Some(k) => {
let tag = ::hmacsha256::authenticate(data.as_bytes(), &k);
let str_data = tag.as_ref()
.iter()
.map(|v| -> String {
let mut hex = format!("{:x}", v);
if hex.len() == 1 {
hex.insert(0, '0');
}
return hex;
})
.fold("".to_string(), |acc, x| acc + x.as_str());
Ok(str_data)
},
None => Err(ErrType::BadKey)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment