-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathobject_hash.js
More file actions
1 lines (1 loc) · 6.37 KB
/
object_hash.js
File metadata and controls
1 lines (1 loc) · 6.37 KB
1
!function(r){"function"==typeof define&&define.amd?define(r):r()}(function(){"use strict";var r=require("crypto").getHashes,o=require("crypto").createHash;function t(r,t){return function(r,t){var n;n="passthrough"!==t.algorithm?o(t.algorithm):new p;void 0===n.write&&(n.write=n.update,n.end=n.update);f(t,n).dispatch(r),n.update||n.end("");if(n.digest)return n.digest("buffer"===t.encoding?void 0:t.encoding);var e=n.read();return"buffer"!==t.encoding?e.toString(t.encoding):e}(r,t=e(r,t))}exports=module.exports=t,exports.sha1=function(r){return t(r)},exports.keys=function(r){return t(r,{excludeValues:!0,algorithm:"sha1",encoding:"hex"})},exports.MD5=function(r){return t(r,{algorithm:"md5",encoding:"hex"})},exports.keysMD5=function(r){return t(r,{algorithm:"md5",encoding:"hex",excludeValues:!0})};var i=r?r().slice():["sha1","md5"];i.push("passthrough");var u=["buffer","hex","binary","base64"];function e(r,t){t=t||{};var n={};if(n.algorithm=t.algorithm||"sha1",n.encoding=t.encoding||"hex",n.excludeValues=!!t.excludeValues,n.algorithm=n.algorithm.toLowerCase(),n.encoding=n.encoding.toLowerCase(),n.ignoreUnknown=!0===t.ignoreUnknown,n.respectType=!1!==t.respectType,n.respectFunctionNames=!1!==t.respectFunctionNames,n.respectFunctionProperties=!1!==t.respectFunctionProperties,n.unorderedArrays=!0===t.unorderedArrays,n.unorderedSets=!1!==t.unorderedSets,n.unorderedObjects=!1!==t.unorderedObjects,n.replacer=t.replacer||void 0,n.excludeKeys=t.excludeKeys||void 0,void 0===r)throw new Error("Object argument required.");for(var e=0;e<i.length;++e)i[e].toLowerCase()===n.algorithm.toLowerCase()&&(n.algorithm=i[e]);if(-1===i.indexOf(n.algorithm))throw new Error('Algorithm "'+n.algorithm+'" not supported. supported values: '+i.join(", "));if(-1===u.indexOf(n.encoding)&&"passthrough"!==n.algorithm)throw new Error('Encoding "'+n.encoding+'" not supported. supported values: '+u.join(", "));return n}function s(r){if("function"==typeof r){return null!=/^function\s+\w*\s*\(\s*\)\s*{\s+\[native code\]\s+}$/i.exec(Function.prototype.toString.call(r))}}function f(u,t,c){c=c||[];function a(r){return t.update?t.update(r,"utf8"):t.write(r,"utf8")}return{dispatch:function(r){u.replacer&&(r=u.replacer(r));var t=typeof r;return null===r&&(t="null"),this["_"+t](r)},_object:function(t){var r=Object.prototype.toString.call(t),n=/\[object (.*)\]/i.exec(r);n=(n=n?n[1]:"unknown:["+r+"]").toLowerCase();var e;if(0<=(e=c.indexOf(t)))return this.dispatch("[CIRCULAR:"+e+"]");if(c.push(t),"undefined"!=typeof Buffer&&Buffer.isBuffer&&Buffer.isBuffer(t))return a("buffer:"),a(t);if("object"===n||"function"===n||"asyncfunction"===n){var o=Object.keys(t);u.unorderedObjects&&(o=o.sort()),!1===u.respectType||s(t)||o.splice(0,0,"prototype","__proto__","constructor"),u.excludeKeys&&(o=o.filter(function(r){return!u.excludeKeys(r)})),a("object:"+o.length+":");var i=this;return o.forEach(function(r){i.dispatch(r),a(":"),u.excludeValues||i.dispatch(t[r]),a(",")})}if(!this["_"+n]){if(u.ignoreUnknown)return a("["+n+"]");throw new Error('Unknown object type "'+n+'"')}this["_"+n](t)},_array:function(r,t){t=void 0!==t?t:!1!==u.unorderedArrays;var n=this;if(a("array:"+r.length+":"),!t||r.length<=1)return r.forEach(function(r){return n.dispatch(r)});var e=[],o=r.map(function(r){var t=new p,n=c.slice();return f(u,t,n).dispatch(r),e=e.concat(n.slice(c.length)),t.read().toString()});return c=c.concat(e),o.sort(),this._array(o,!1)},_date:function(r){return a("date:"+r.toJSON())},_symbol:function(r){return a("symbol:"+r.toString())},_error:function(r){return a("error:"+r.toString())},_boolean:function(r){return a("bool:"+r.toString())},_string:function(r){a("string:"+r.length+":"),a(r.toString())},_function:function(r){a("fn:"),s(r)?this.dispatch("[native]"):this.dispatch(r.toString()),!1!==u.respectFunctionNames&&this.dispatch("function-name:"+String(r.name)),u.respectFunctionProperties&&this._object(r)},_number:function(r){return a("number:"+r.toString())},_xml:function(r){return a("xml:"+r.toString())},_null:function(){return a("Null")},_undefined:function(){return a("Undefined")},_regexp:function(r){return a("regex:"+r.toString())},_uint8array:function(r){return a("uint8array:"),this.dispatch(Array.prototype.slice.call(r))},_uint8clampedarray:function(r){return a("uint8clampedarray:"),this.dispatch(Array.prototype.slice.call(r))},_int8array:function(r){return a("uint8array:"),this.dispatch(Array.prototype.slice.call(r))},_uint16array:function(r){return a("uint16array:"),this.dispatch(Array.prototype.slice.call(r))},_int16array:function(r){return a("uint16array:"),this.dispatch(Array.prototype.slice.call(r))},_uint32array:function(r){return a("uint32array:"),this.dispatch(Array.prototype.slice.call(r))},_int32array:function(r){return a("uint32array:"),this.dispatch(Array.prototype.slice.call(r))},_float32array:function(r){return a("float32array:"),this.dispatch(Array.prototype.slice.call(r))},_float64array:function(r){return a("float64array:"),this.dispatch(Array.prototype.slice.call(r))},_arraybuffer:function(r){return a("arraybuffer:"),this.dispatch(new Uint8Array(r))},_url:function(r){return a("url:"+r.toString())},_map:function(r){a("map:");var t=Array.from(r);return this._array(t,!1!==u.unorderedSets)},_set:function(r){a("set:");var t=Array.from(r);return this._array(t,!1!==u.unorderedSets)},_blob:function(){if(u.ignoreUnknown)return a("[blob]");throw Error('Hashing Blob objects is currently not supported\n(see https://github.com/puleos/object-hash/issues/26)\nUse "options.replacer" or "options.ignoreUnknown"\n')},_domwindow:function(){return a("domwindow")},_process:function(){return a("process")},_timer:function(){return a("timer")},_pipe:function(){return a("pipe")},_tcp:function(){return a("tcp")},_udp:function(){return a("udp")},_tty:function(){return a("tty")},_statwatcher:function(){return a("statwatcher")},_securecontext:function(){return a("securecontext")},_connection:function(){return a("connection")},_zlib:function(){return a("zlib")},_context:function(){return a("context")},_nodescript:function(){return a("nodescript")},_httpparser:function(){return a("httpparser")},_dataview:function(){return a("dataview")},_signal:function(){return a("signal")},_fsevent:function(){return a("fsevent")},_tlswrap:function(){return a("tlswrap")}}}function p(){return{buf:"",write:function(r){this.buf+=r},end:function(r){this.buf+=r},read:function(){return this.buf}}}exports.writeToStream=function(r,t,n){return void 0===n&&(n=t,t={}),f(t=e(r,t),n).dispatch(r)}});