Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created May 24, 2017 17:20
Show Gist options
  • Save jcipriano/373d4d441e8041f775b6227c0e2fd1f3 to your computer and use it in GitHub Desktop.
Save jcipriano/373d4d441e8041f775b6227c0e2fd1f3 to your computer and use it in GitHub Desktop.
Creates a valid CRC response for Twitter webhook integration.
crypto = require('crypto');
// Example app consumer secret found in apps.twitter.com
APP_CONSUMER_SECRET = 'z3ZX4v7mAAUGykl3EcmkqbartmuW8VFOOzCloLx9Q45P0hLrFu';
// Example token provided by incoming GET request
TOKEN = '9b4507b3-9040-4669-9ca3-6b94edb50553';
/**
* Creates a HMAC SHA-256 hash created from the app TOKEN and
* your app Consumer Secret.
* @param token the token provided by the incoming GET request
* @return string
*/
function get_challenge_response(token) {
hmac = crypto.createHmac('sha256', APP_CONSUMER_SECRET).update(TOKEN).digest('base64');
response = {
"response_token": "sha256=" + hmac
}
return JSON.stringify(response);
}
// prints result
console.log(get_challenge_response(TOKEN));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment