oj
Version:
A unified templating language for the people. Thirsty people.
1 lines • 10.1 kB
JavaScript
function toBuf(e,t){return t=t||"binary",util.isString(e)&&(t==="buffer"&&(t="binary"),e=new Buffer(e,t)),e}function Credentials(e,t,n){if(!(this instanceof Credentials))return new Credentials(e,t,n);n?this.context=n:(this.context=new SecureContext,e?this.context.init(e):this.context.init()),t&&this.context.setOptions(t)}function LazyTransform(e){this._options=e}function Hash(e,t){if(!(this instanceof Hash))return new Hash(e,t);this._binding=new binding.Hash(e),LazyTransform.call(this,t)}function Hmac(e,t,n){if(!(this instanceof Hmac))return new Hmac(e,t,n);this._binding=new binding.Hmac,this._binding.init(e,toBuf(t)),LazyTransform.call(this,n)}function getDecoder(e,t){return t==="utf-8"&&(t="utf8"),e=e||new StringDecoder(t),assert(e.encoding===t,"Cannot change encoding"),e}function Cipher(e,t,n){if(!(this instanceof Cipher))return new Cipher(e,t,n);this._binding=new binding.CipherBase(!0),this._binding.init(e,toBuf(t)),this._decoder=null,LazyTransform.call(this,n)}function Cipheriv(e,t,n,r){if(!(this instanceof Cipheriv))return new Cipheriv(e,t,n,r);this._binding=new binding.CipherBase(!0),this._binding.initiv(e,toBuf(t),toBuf(n)),this._decoder=null,LazyTransform.call(this,r)}function Decipher(e,t,n){if(!(this instanceof Decipher))return new Decipher(e,t,n);this._binding=new binding.CipherBase(!1),this._binding.init(e,toBuf(t)),this._decoder=null,LazyTransform.call(this,n)}function Decipheriv(e,t,n,r){if(!(this instanceof Decipheriv))return new Decipheriv(e,t,n,r);this._binding=new binding.CipherBase(!1),this._binding.initiv(e,toBuf(t),toBuf(n)),this._decoder=null,LazyTransform.call(this,r)}function Sign(e,t){if(!(this instanceof Sign))return new Sign(e,t);this._binding=new binding.Sign,this._binding.init(e),stream.Writable.call(this,t)}function Verify(e,t){if(!(this instanceof Verify))return new Verify(e,t);this._binding=new binding.Verify,this._binding.init(e),stream.Writable.call(this,t)}function DiffieHellman(e,t){if(!(this instanceof DiffieHellman))return new DiffieHellman(e,t);e?(t=t||exports.DEFAULT_ENCODING,e=toBuf(e,t),this._binding=new binding.DiffieHellman(e)):this._binding=new binding.DiffieHellman}function DiffieHellmanGroup(e){if(!(this instanceof DiffieHellmanGroup))return new DiffieHellmanGroup(e);this._binding=new binding.DiffieHellmanGroup(e)}function dhGenerateKeys(e){var t=this._binding.generateKeys();return e=e||exports.DEFAULT_ENCODING,e&&e!=="buffer"&&(t=t.toString(e)),t}function dhComputeSecret(e,t,n){t=t||exports.DEFAULT_ENCODING,n=n||exports.DEFAULT_ENCODING;var r=this._binding.computeSecret(toBuf(e,t));return n&&n!=="buffer"&&(r=r.toString(n)),r}function dhGetPrime(e){var t=this._binding.getPrime();return e=e||exports.DEFAULT_ENCODING,e&&e!=="buffer"&&(t=t.toString(e)),t}function dhGetGenerator(e){var t=this._binding.getGenerator();return e=e||exports.DEFAULT_ENCODING,e&&e!=="buffer"&&(t=t.toString(e)),t}function dhGetPublicKey(e){var t=this._binding.getPublicKey();return e=e||exports.DEFAULT_ENCODING,e&&e!=="buffer"&&(t=t.toString(e)),t}function dhGetPrivateKey(e){var t=this._binding.getPrivateKey();return e=e||exports.DEFAULT_ENCODING,e&&e!=="buffer"&&(t=t.toString(e)),t}function pbkdf2(e,t,n,r,i){e=toBuf(e),t=toBuf(t);if(exports.DEFAULT_ENCODING==="buffer")return binding.PBKDF2(e,t,n,r,i);var s=exports.DEFAULT_ENCODING;if(!i){var o=binding.PBKDF2(e,t,n,r);return o.toString(s)}binding.PBKDF2(e,t,n,r,function(e,t){t&&(t=t.toString(s)),i(e,t)})}function filterDuplicates(e){var t={};return e.forEach(function(e){/^[0-9A-Z\-]+$/.test(e)&&(e=e.toLowerCase()),t[e]=!0}),Object.getOwnPropertyNames(t).sort()}exports.DEFAULT_ENCODING="buffer";try{var binding=process.binding("crypto"),SecureContext=binding.SecureContext,randomBytes=binding.randomBytes,pseudoRandomBytes=binding.pseudoRandomBytes,getCiphers=binding.getCiphers,getHashes=binding.getHashes}catch(e){throw new Error("node.js not compiled with openssl crypto support.")}var stream=require("stream"),util=require("util"),assert=require("assert"),StringDecoder=require("string_decoder").StringDecoder;exports.Credentials=Credentials,exports.createCredentials=function(e,t){e||(e={});var n=new Credentials(e.secureProtocol,e.secureOptions,t);if(t)return n;e.key&&(e.passphrase?n.context.setKey(e.key,e.passphrase):n.context.setKey(e.key)),e.cert&&n.context.setCert(e.cert),e.ciphers&&n.context.setCiphers(e.ciphers);if(e.ca)if(util.isArray(e.ca))for(var r=0,i=e.ca.length;r<i;r++)n.context.addCACert(e.ca[r]);else n.context.addCACert(e.ca);else n.context.addRootCerts();if(e.crl)if(util.isArray(e.crl))for(var r=0,i=e.crl.length;r<i;r++)n.context.addCRL(e.crl[r]);else n.context.addCRL(e.crl);e.sessionIdContext&&n.context.setSessionIdContext(e.sessionIdContext);if(e.pfx){var s=e.pfx,o=e.passphrase;s=toBuf(s),o&&(o=toBuf(o)),o?n.context.loadPKCS12(s,o):n.context.loadPKCS12(s)}return n},util.inherits(LazyTransform,stream.Transform),["_readableState","_writableState","_transformState"].forEach(function(e,t,n){Object.defineProperty(LazyTransform.prototype,e,{get:function(){return stream.Transform.call(this,this._options),this._writableState.decodeStrings=!1,this._writableState.defaultEncoding="binary",this[e]},set:function(t){Object.defineProperty(this,e,{value:t,enumerable:!0,configurable:!0,writable:!0})},configurable:!0,enumerable:!0})}),exports.createHash=exports.Hash=Hash,util.inherits(Hash,LazyTransform),Hash.prototype._transform=function(e,t,n){this._binding.update(e,t),n()},Hash.prototype._flush=function(e){var t=this._readableState.encoding||"buffer";this.push(this._binding.digest(t),t),e()},Hash.prototype.update=function(e,t){return t=t||exports.DEFAULT_ENCODING,t==="buffer"&&util.isString(e)&&(t="binary"),this._binding.update(e,t),this},Hash.prototype.digest=function(e){return e=e||exports.DEFAULT_ENCODING,this._binding.digest(e)},exports.createHmac=exports.Hmac=Hmac,util.inherits(Hmac,LazyTransform),Hmac.prototype.update=Hash.prototype.update,Hmac.prototype.digest=Hash.prototype.digest,Hmac.prototype._flush=Hash.prototype._flush,Hmac.prototype._transform=Hash.prototype._transform,exports.createCipher=exports.Cipher=Cipher,util.inherits(Cipher,LazyTransform),Cipher.prototype._transform=function(e,t,n){this.push(this._binding.update(e,t)),n()},Cipher.prototype._flush=function(e){this.push(this._binding.final()),e()},Cipher.prototype.update=function(e,t,n){t=t||exports.DEFAULT_ENCODING,n=n||exports.DEFAULT_ENCODING;var r=this._binding.update(e,t);return n&&n!=="buffer"&&(this._decoder=getDecoder(this._decoder,n),r=this._decoder.write(r)),r},Cipher.prototype.final=function(e){e=e||exports.DEFAULT_ENCODING;var t=this._binding.final();return e&&e!=="buffer"&&(this._decoder=getDecoder(this._decoder,e),t=this._decoder.end(t)),t},Cipher.prototype.setAutoPadding=function(e){return this._binding.setAutoPadding(e),this},exports.createCipheriv=exports.Cipheriv=Cipheriv,util.inherits(Cipheriv,LazyTransform),Cipheriv.prototype._transform=Cipher.prototype._transform,Cipheriv.prototype._flush=Cipher.prototype._flush,Cipheriv.prototype.update=Cipher.prototype.update,Cipheriv.prototype.final=Cipher.prototype.final,Cipheriv.prototype.setAutoPadding=Cipher.prototype.setAutoPadding,exports.createDecipher=exports.Decipher=Decipher,util.inherits(Decipher,LazyTransform),Decipher.prototype._transform=Cipher.prototype._transform,Decipher.prototype._flush=Cipher.prototype._flush,Decipher.prototype.update=Cipher.prototype.update,Decipher.prototype.final=Cipher.prototype.final,Decipher.prototype.finaltol=Cipher.prototype.final,Decipher.prototype.setAutoPadding=Cipher.prototype.setAutoPadding,exports.createDecipheriv=exports.Decipheriv=Decipheriv,util.inherits(Decipheriv,LazyTransform),Decipheriv.prototype._transform=Cipher.prototype._transform,Decipheriv.prototype._flush=Cipher.prototype._flush,Decipheriv.prototype.update=Cipher.prototype.update,Decipheriv.prototype.final=Cipher.prototype.final,Decipheriv.prototype.finaltol=Cipher.prototype.final,Decipheriv.prototype.setAutoPadding=Cipher.prototype.setAutoPadding,exports.createSign=exports.Sign=Sign,util.inherits(Sign,stream.Writable),Sign.prototype._write=function(e,t,n){this._binding.update(e,t),n()},Sign.prototype.update=Hash.prototype.update,Sign.prototype.sign=function(e,t){t=t||exports.DEFAULT_ENCODING;var n=this._binding.sign(toBuf(e));return t&&t!=="buffer"&&(n=n.toString(t)),n},exports.createVerify=exports.Verify=Verify,util.inherits(Verify,stream.Writable),Verify.prototype._write=Sign.prototype._write,Verify.prototype.update=Sign.prototype.update,Verify.prototype.verify=function(e,t,n){return n=n||exports.DEFAULT_ENCODING,this._binding.verify(toBuf(e),toBuf(t,n))},exports.createDiffieHellman=exports.DiffieHellman=DiffieHellman,exports.DiffieHellmanGroup=exports.createDiffieHellmanGroup=exports.getDiffieHellman=DiffieHellmanGroup,DiffieHellmanGroup.prototype.generateKeys=DiffieHellman.prototype.generateKeys=dhGenerateKeys,DiffieHellmanGroup.prototype.computeSecret=DiffieHellman.prototype.computeSecret=dhComputeSecret,DiffieHellmanGroup.prototype.getPrime=DiffieHellman.prototype.getPrime=dhGetPrime,DiffieHellmanGroup.prototype.getGenerator=DiffieHellman.prototype.getGenerator=dhGetGenerator,DiffieHellmanGroup.prototype.getPublicKey=DiffieHellman.prototype.getPublicKey=dhGetPublicKey,DiffieHellmanGroup.prototype.getPrivateKey=DiffieHellman.prototype.getPrivateKey=dhGetPrivateKey,DiffieHellman.prototype.setPublicKey=function(e,t){return t=t||exports.DEFAULT_ENCODING,this._binding.setPublicKey(toBuf(e,t)),this},DiffieHellman.prototype.setPrivateKey=function(e,t){return t=t||exports.DEFAULT_ENCODING,this._binding.setPrivateKey(toBuf(e,t)),this},exports.pbkdf2=function(e,t,n,r,i){if(!util.isFunction(i))throw new Error("No callback provided to pbkdf2");return pbkdf2(e,t,n,r,i)},exports.pbkdf2Sync=function(e,t,n,r){return pbkdf2(e,t,n,r)},exports.randomBytes=randomBytes,exports.pseudoRandomBytes=pseudoRandomBytes,exports.rng=randomBytes,exports.prng=pseudoRandomBytes,exports.getCiphers=function(){return filterDuplicates(getCiphers.call(null,arguments))},exports.getHashes=function(){return filterDuplicates(getHashes.call(null,arguments))}