react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / lib / crypto.js
81146 views// Load modules12var Crypto = require('crypto');3var Url = require('url');4var Utils = require('./utils');567// Declare internals89var internals = {};101112// MAC normalization format version1314exports.headerVersion = '1'; // Prevent comparison of mac values generated with different normalized string formats151617// Supported HMAC algorithms1819exports.algorithms = ['sha1', 'sha256'];202122// Calculate the request MAC2324/*25type: 'header', // 'header', 'bewit', 'response'26credentials: {27key: 'aoijedoaijsdlaksjdl',28algorithm: 'sha256' // 'sha1', 'sha256'29},30options: {31method: 'GET',32resource: '/resource?a=1&b=2',33host: 'example.com',34port: 8080,35ts: 1357718381034,36nonce: 'd3d345f',37hash: 'U4MKKSmiVxk37JCCrAVIjV/OhB3y+NdwoCr6RShbVkE=',38ext: 'app-specific-data',39app: 'hf48hd83qwkj', // Application id (Oz)40dlg: 'd8djwekds9cj' // Delegated by application id (Oz), requires options.app41}42*/4344exports.calculateMac = function (type, credentials, options) {4546var normalized = exports.generateNormalizedString(type, options);4748var hmac = Crypto.createHmac(credentials.algorithm, credentials.key).update(normalized);49var digest = hmac.digest('base64');50return digest;51};525354exports.generateNormalizedString = function (type, options) {5556var resource = options.resource || '';57if (resource &&58resource[0] !== '/') {5960var url = Url.parse(resource, false);61resource = url.path; // Includes query62}6364var normalized = 'hawk.' + exports.headerVersion + '.' + type + '\n' +65options.ts + '\n' +66options.nonce + '\n' +67(options.method || '').toUpperCase() + '\n' +68resource + '\n' +69options.host.toLowerCase() + '\n' +70options.port + '\n' +71(options.hash || '') + '\n';7273if (options.ext) {74normalized += options.ext.replace('\\', '\\\\').replace('\n', '\\n');75}7677normalized += '\n';7879if (options.app) {80normalized += options.app + '\n' +81(options.dlg || '') + '\n';82}8384return normalized;85};868788exports.calculatePayloadHash = function (payload, algorithm, contentType) {8990var hash = exports.initializePayloadHash(algorithm, contentType);91hash.update(payload || '');92return exports.finalizePayloadHash(hash);93};949596exports.initializePayloadHash = function (algorithm, contentType) {9798var hash = Crypto.createHash(algorithm);99hash.update('hawk.' + exports.headerVersion + '.payload\n');100hash.update(Utils.parseContentType(contentType) + '\n');101return hash;102};103104105exports.finalizePayloadHash = function (hash) {106107hash.update('\n');108return hash.digest('base64');109};110111112exports.calculateTsMac = function (ts, credentials) {113114var hmac = Crypto.createHmac(credentials.algorithm, credentials.key);115hmac.update('hawk.' + exports.headerVersion + '.ts\n' + ts + '\n');116return hmac.digest('base64');117};118119120exports.timestampMessage = function (credentials, localtimeOffsetMsec) {121122var now = Utils.nowSecs(localtimeOffsetMsec);123var tsm = exports.calculateTsMac(now, credentials);124return { ts: now, tsm: tsm };125};126127128