Skip to content

Instantly share code, notes, and snippets.

@gallegogt
Created July 12, 2017 13:53
Show Gist options
  • Save gallegogt/c4e1233c8ae380617c828522cb3fe019 to your computer and use it in GitHub Desktop.
Save gallegogt/c4e1233c8ae380617c828522cb3fe019 to your computer and use it in GitHub Desktop.
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