All files / lib/postback index.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                5x                               3x 3x              
/**
 * @name Postback
 * @description This module exposes functions
 *              related to postback validations.
 *              This can also be found in client.security if
 *              you already used pagarme.connect to pre apply
 *              your api_key.
 *
 * @module postback
 **/
import {
  equals,
} from 'ramda'
import { createHmac } from 'crypto'
 
/**
 * Generates a hash signed with a key.
 *
 * @param {String} key the keys used to sign the hash.
 *
 * @param {String} string The string to be hashed.
 *
*/
function calculateSignature (key, postbackBody) {
  return createHmac('sha1', key)
    .update(postbackBody)
    .digest('hex')
}
 
/**
 * Verifies a hash signed with a key.
 *
 * @param {String} key the keys used to sign the hash.
 *
 * @param {String} string The string to be hashed.
 *
 * @param {String} expected The expected result.
 *
*/
function verifySignature (key, postbackBody, headerSignature) {
  const signature = calculateSignature(key, postbackBody)
  return equals(signature, headerSignature)
}
 
export default {
  calculateSignature,
  verifySignature,
}