Skip to content

Instantly share code, notes, and snippets.

@Teknologica
Last active October 11, 2017 18:22
Show Gist options
  • Save Teknologica/0a552061c7969ed9354a08186f114912 to your computer and use it in GitHub Desktop.
Save Teknologica/0a552061c7969ed9354a08186f114912 to your computer and use it in GitHub Desktop.
Sample code for generating an Authentication Signature for Rebilly in Javascript ES6 with Babel
/**
* ES6 via Babel
* See https://github.com/dankogai/js-base64 for Base64
*/
import jsBase64 from 'js-base64';
import crypto from 'crypto';
const randomString = (strLength) => {
let text = '';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < strLength; i++) {
text += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}
return text;
};
const generateSignature = (apiUser, apiKey) => {
const nonce = randomString(30);
const ts = Date.now();
const data = apiUser + nonce + ts;
const sig = crypto.createHmac('sha1', apiKey).update(data).digest('hex');
const paypload = {
'REB-APIUSER': apiUser,
'REB-NONCE': nonce,
'REB-TIMESTAMP': ts,
'REB-SIGNATURE': sig
};
return jsBase64.Base64.encode(JSON.stringify(paypload));
};
export {generateSignature};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment