diff --git a/lib/cam.js b/lib/cam.js index a0415730..68c87768 100755 --- a/lib/cam.js +++ b/lib/cam.js @@ -387,23 +387,37 @@ Cam.prototype.updateNC = function() { return String(this._nc).padStart(8, '0'); }; +/** + * Get the hash algorithm to use for digest authentication. + * If Node.js is running in FIPS mode, returns sha256, otherwise returns md5. + * @returns {md5 | sha256} hash algorithm + * @private + */ +Cam.prototype._getHashAlgorithm = function() { + if (crypto.getFips() === 1) { + return 'sha256'; + } else { + return 'md5'; + } +}; + Cam.prototype.digestAuth = function(wwwAuthenticate, reqOptions) { const challenge = this._parseChallenge(wwwAuthenticate); - const ha1 = crypto.createHash('md5'); + const ha1 = crypto.createHash(this._getHashAlgorithm()); ha1.update([this.username, challenge.realm, this.password].join(':')); - const ha2 = crypto.createHash('md5'); + const ha2 = crypto.createHash(this._getHashAlgorithm()); ha2.update([reqOptions.method, reqOptions.path].join(':')); let cnonce = null; let nc = null; if (typeof challenge.qop === 'string') { - const cnonceHash = crypto.createHash('md5'); + const cnonceHash = crypto.createHash(this._getHashAlgorithm()); cnonceHash.update(Math.random().toString(36)); cnonce = cnonceHash.digest('hex').substring(0, 8); nc = this.updateNC(); } - const response = crypto.createHash('md5'); + const response = crypto.createHash(this._getHashAlgorithm()); const responseParams = [ ha1.digest('hex'), challenge.nonce diff --git a/package.json b/package.json index ddeeaca2..46f87491 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onvif", - "version": "0.8.1", + "version": "0.9.0", "author": "Andrew D.Laptev ", "description": "Client to ONVIF NVT devices Profile S: cameras", "main": "lib/onvif.js", @@ -8,6 +8,7 @@ "jsdoc": "jsdoc ./lib/*.js --readme ./README.md --destination ./docs", "gh-pages": "jsdoc ./lib/*.js --readme ./README.md --destination ./", "lint": "eslint lib/*.js", + "lint:fix": "eslint --fix lib/*.js", "pretest": "npm run lint", "test": "nyc mocha", "mockserver": "node startServerMockup.js"