Last active
May 3, 2018 08:26
-
-
Save alexsaare/9ad46a1c226ecf52574a8a0dea7ef2ce to your computer and use it in GitHub Desktop.
TrustPilot Business Generated Links in Node using crypto-js and monotonic-id
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
var config = require('../config'); | |
var AES = require('crypto-js/aes'); | |
var HMAC256 = require('crypto-js/hmac-sha256'); | |
var Base64 = require('crypto-js/enc-base64'); | |
var HEX = require('crypto-js/enc-hex'); | |
let MID = require('monotonic-id'); | |
module.exports = { | |
buildData: function(name, email, ref, skus, tags){ | |
return { | |
"email": email, | |
"name": name, | |
"ref": ref, | |
"skus": skus, | |
"tags": tags | |
} | |
}, | |
buildLink : function (data){ | |
let encKey = Base64.parse(config.trustpilot.encryptionKey); | |
let authKey = Base64.parse(config.trustpilot.authenticationKey); | |
let ivString = new MID().toBuffer().toString('hex'); | |
let iv = HEX.parse(ivString); | |
let cipherText = AES.encrypt(JSON.stringify(data), encKey, {iv: iv}); | |
let ivCipher = iv.concat(cipherText.ciphertext); | |
let hmac = HMAC256(ivCipher, authKey); | |
let rawPayload = ivCipher.concat(hmac); | |
let base64Payload = Base64.stringify(rawPayload); | |
let payload = encodeURIComponent(base64Payload); | |
return config.trustpilot.baseUrl + config.trustpilot.domain + '?p=' + payload; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment