Created
July 12, 2017 13:53
-
-
Save gallegogt/c4e1233c8ae380617c828522cb3fe019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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