The secret of the application is the base-16 string, so you need to convert it to an array of bytes. Take a look at How to convert a hexadecimal string to an array of bytes? for details on how to do this. Access_token must be converted to an array of bytes using ASCII encoding. Once you have created the HMAC, then encode it as a base-16 string for use as apps apps. The following code converts an array of bytes to base16.
public static class Base16 { private static readonly char[] encoding; static Base16() { encoding = new char[16] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; } public static string Encode(byte[] data) { char[] text = new char[data.Length * 2]; for (int i = 0, j = 0; i < data.Length; i++) { text[j++] = encoding[data[i] >> 4]; text[j++] = encoding[data[i] & 0xf]; } return new string(text); }
Then the code to create appsecret_proof will be
private string GenerateAppSecretProof(string accessToken, string appSecret) { byte[] key = Base16.Decode(appSecret); byte[] hash; using (HMAC hmacAlg = new HMACSHA1(key)) { hash = hmacAlg.ComputeHash(Encoding.ASCII.GetBytes(accessToken)); } return Base16.Encode(hash); }
Facebook seems to accept either the SHA256 HMAC or the SHA1 HMAC.
Steve Dec 17 '13 at 12:00 2013-12-17 12:00
source share