Revision d9441412
Added by koszko over 1 year ago
common/sha256.js | ||
---|---|---|
36 | 36 |
WINDOW = false; |
37 | 37 |
} |
38 | 38 |
var WEB_WORKER = !WINDOW && typeof self === 'object'; |
39 |
var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; |
|
40 |
if (NODE_JS) { |
|
41 |
root = global; |
|
42 |
} else if (WEB_WORKER) { |
|
39 |
if (WEB_WORKER) { |
|
43 | 40 |
root = self; |
44 | 41 |
} |
45 | 42 |
var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports; |
... | ... | |
82 | 79 |
|
83 | 80 |
var createMethod = function (is224) { |
84 | 81 |
var method = createOutputMethod('hex', is224); |
85 |
if (NODE_JS) { |
|
86 |
method = nodeWrap(method, is224); |
|
87 |
} |
|
88 | 82 |
method.create = function () { |
89 | 83 |
return new Sha256(is224); |
90 | 84 |
}; |
... | ... | |
98 | 92 |
return method; |
99 | 93 |
}; |
100 | 94 |
|
101 |
var nodeWrap = function (method, is224) { |
|
102 |
var crypto = eval("require('crypto')"); |
|
103 |
var Buffer = eval("require('buffer').Buffer"); |
|
104 |
var algorithm = is224 ? 'sha224' : 'sha256'; |
|
105 |
var nodeMethod = function (message) { |
|
106 |
if (typeof message === 'string') { |
|
107 |
return crypto.createHash(algorithm).update(message, 'utf8').digest('hex'); |
|
108 |
} else { |
|
109 |
if (message === null || message === undefined) { |
|
110 |
throw new Error(ERROR); |
|
111 |
} else if (message.constructor === ArrayBuffer) { |
|
112 |
message = new Uint8Array(message); |
|
113 |
} |
|
114 |
} |
|
115 |
if (Array.isArray(message) || ArrayBuffer.isView(message) || |
|
116 |
message.constructor === Buffer) { |
|
117 |
return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex'); |
|
118 |
} else { |
|
119 |
return method(message); |
|
120 |
} |
|
121 |
}; |
|
122 |
return nodeMethod; |
|
123 |
}; |
|
124 |
|
|
125 | 95 |
var createHmacOutputMethod = function (outputType, is224) { |
126 | 96 |
return function (key, message) { |
127 | 97 |
return new HmacSha256(key, is224, true).update(message)[outputType](); |
Also available in: Unified diff
remove node-specific code from sha256.js
this will prevent the eval warning when uploading extension to AMO