arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/cookie-signature/index.js
node_modules/cookie-signature/index.jsRaw
 1
 2/**
 3 * Module dependencies.
 4 */
 5
 6var crypto = require('crypto');
 7
 8/**
 9 * Sign the given `val` with `secret`.
10 *
11 * @param {String} val
12 * @param {String} secret
13 * @return {String}
14 * @api private
15 */
16
17exports.sign = function(val, secret){
18  if ('string' != typeof val) throw new TypeError('cookie required');
19  if ('string' != typeof secret) throw new TypeError('secret required');
20  return val + '.' + crypto
21    .createHmac('sha256', secret)
22    .update(val)
23    .digest('base64')
24    .replace(/\=+$/, '');
25};
26
27/**
28 * Unsign and decode the given `val` with `secret`,
29 * returning `false` if the signature is invalid.
30 *
31 * @param {String} val
32 * @param {String} secret
33 * @return {String|Boolean}
34 * @api private
35 */
36
37exports.unsign = function(val, secret){
38  if ('string' != typeof val) throw new TypeError('cookie required');
39  if ('string' != typeof secret) throw new TypeError('secret required');
40  var str = val.slice(0, val.lastIndexOf('.'));
41  return exports.sign(str, secret) == val ? str : false;
42};