Last active
October 11, 2017 18:22
-
-
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
This file contains hidden or 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
| /** | |
| * 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