Initial commit
[yaffs-website] / node_modules / hawk / lib / crypto.js
1 // Load modules\r
2 \r
3 var Crypto = require('crypto');\r
4 var Url = require('url');\r
5 var Utils = require('./utils');\r
6 \r
7 \r
8 // Declare internals\r
9 \r
10 var internals = {};\r
11 \r
12 \r
13 // MAC normalization format version\r
14 \r
15 exports.headerVersion = '1';                        // Prevent comparison of mac values generated with different normalized string formats\r
16 \r
17 \r
18 // Supported HMAC algorithms\r
19 \r
20 exports.algorithms = ['sha1', 'sha256'];\r
21 \r
22 \r
23 // Calculate the request MAC\r
24 \r
25 /*\r
26     type: 'header',                                 // 'header', 'bewit', 'response'\r
27     credentials: {\r
28         key: 'aoijedoaijsdlaksjdl',\r
29         algorithm: 'sha256'                         // 'sha1', 'sha256'\r
30     },\r
31     options: {\r
32         method: 'GET',\r
33         resource: '/resource?a=1&b=2',\r
34         host: 'example.com',\r
35         port: 8080,\r
36         ts: 1357718381034,\r
37         nonce: 'd3d345f',\r
38         hash: 'U4MKKSmiVxk37JCCrAVIjV/OhB3y+NdwoCr6RShbVkE=',\r
39         ext: 'app-specific-data',\r
40         app: 'hf48hd83qwkj',                        // Application id (Oz)\r
41         dlg: 'd8djwekds9cj'                         // Delegated by application id (Oz), requires options.app\r
42     }\r
43 */\r
44 \r
45 exports.calculateMac = function (type, credentials, options) {\r
46 \r
47     var normalized = exports.generateNormalizedString(type, options);\r
48 \r
49     var hmac = Crypto.createHmac(credentials.algorithm, credentials.key).update(normalized);\r
50     var digest = hmac.digest('base64');\r
51     return digest;\r
52 };\r
53 \r
54 \r
55 exports.generateNormalizedString = function (type, options) {\r
56 \r
57     var resource = options.resource || '';\r
58     if (resource &&\r
59         resource[0] !== '/') {\r
60 \r
61         var url = Url.parse(resource, false);\r
62         resource = url.path;                        // Includes query\r
63     }\r
64 \r
65     var normalized = 'hawk.' + exports.headerVersion + '.' + type + '\n' +\r
66                      options.ts + '\n' +\r
67                      options.nonce + '\n' +\r
68                      (options.method || '').toUpperCase() + '\n' +\r
69                      resource + '\n' +\r
70                      options.host.toLowerCase() + '\n' +\r
71                      options.port + '\n' +\r
72                      (options.hash || '') + '\n';\r
73 \r
74     if (options.ext) {\r
75         normalized += options.ext.replace('\\', '\\\\').replace('\n', '\\n');\r
76     }\r
77 \r
78     normalized += '\n';\r
79 \r
80     if (options.app) {\r
81         normalized += options.app + '\n' +\r
82                       (options.dlg || '') + '\n';\r
83     }\r
84 \r
85     return normalized;\r
86 };\r
87 \r
88 \r
89 exports.calculatePayloadHash = function (payload, algorithm, contentType) {\r
90 \r
91     var hash = exports.initializePayloadHash(algorithm, contentType);\r
92     hash.update(payload || '');\r
93     return exports.finalizePayloadHash(hash);\r
94 };\r
95 \r
96 \r
97 exports.initializePayloadHash = function (algorithm, contentType) {\r
98 \r
99     var hash = Crypto.createHash(algorithm);\r
100     hash.update('hawk.' + exports.headerVersion + '.payload\n');\r
101     hash.update(Utils.parseContentType(contentType) + '\n');\r
102     return hash;\r
103 };\r
104 \r
105 \r
106 exports.finalizePayloadHash = function (hash) {\r
107 \r
108     hash.update('\n');\r
109     return hash.digest('base64');\r
110 };\r
111 \r
112 \r
113 exports.calculateTsMac = function (ts, credentials) {\r
114 \r
115     var hmac = Crypto.createHmac(credentials.algorithm, credentials.key);\r
116     hmac.update('hawk.' + exports.headerVersion + '.ts\n' + ts + '\n');\r
117     return hmac.digest('base64');\r
118 };\r
119 \r
120 \r
121 exports.timestampMessage = function (credentials, localtimeOffsetMsec) {\r
122 \r
123     var now = Utils.nowSecs(localtimeOffsetMsec);\r
124     var tsm = exports.calculateTsMac(now, credentials);\r
125     return { ts: now, tsm: tsm };\r
126 };\r