CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
index.js48 linesDownload Raw Back to cookie-signature
1/**2 * Module dependencies.3 */4 5var crypto = require('crypto');6 7/**8 * Sign the given `val` with `secret`.9 *10 * @param {String} val11 * @param {String|NodeJS.ArrayBufferView|crypto.KeyObject} secret12 * @return {String}13 * @api private14 */15 16exports.sign = function(val, secret){17  if ('string' != typeof val) throw new TypeError("Cookie value must be provided as a string.");18  if (null == secret) throw new TypeError("Secret key must be provided.");19  return val + '.' + crypto20    .createHmac('sha256', secret)21    .update(val)22    .digest('base64')23    .replace(/\=+$/, '');24};25 26/**27 * Unsign and decode the given `input` with `secret`,28 * returning `false` if the signature is invalid.29 *30 * @param {String} input31 * @param {String|NodeJS.ArrayBufferView|crypto.KeyObject} secret32 * @return {String|Boolean}33 * @api private34 */35 36exports.unsign = function(input, secret){37  if ('string' != typeof input) throw new TypeError("Signed cookie string must be provided.");38  if (null == secret) throw new TypeError("Secret key must be provided.");39  var tentativeValue = input.slice(0, input.lastIndexOf('.')),40      expectedInput = exports.sign(tentativeValue, secret),41      expectedBuffer = Buffer.from(expectedInput),42      inputBuffer = Buffer.from(input);43  return (44    expectedBuffer.length === inputBuffer.length &&45    crypto.timingSafeEqual(expectedBuffer, inputBuffer)46   ) ? tentativeValue : false;47};48