text: 'use strict';Object.defineProperty(exports,'__esModule',{value:true});exports.BYTE_ORDER_LE=exports.BYTE_ORDER_BE=exports.ATYP_V6=exports.ATYP_DOMAIN=exports.ATYP_V4=undefined;exports.numberToBuffer=numberToBuffer;exports.parseURI=parseURI;exports.getRandomInt=getRandomInt;exports.getRandomChunks=getRandomChunks;exports.getChunks=getChunks;exports.getUTC=getUTC;exports.hexStringToBuffer=hexStringToBuffer;exports.isValidHostname=isValidHostname;exports.isValidPort=isValidPort;exports.md5=md5;exports.hmac=hmac;exports.EVP_BytesToKey=EVP_BytesToKey;exports.HKDF=HKDF;var _net=require('net');var _net2=_interopRequireDefault(_net);var _crypto=require('crypto');var _crypto2=_interopRequireDefault(_crypto);var _ip=require('ip');var _ip2=_interopRequireDefault(_ip);var _urijs=require('urijs');var _urijs2=_interopRequireDefault(_urijs);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const ATYP_V4=exports.ATYP_V4=1;const ATYP_DOMAIN=exports.ATYP_DOMAIN=3;const ATYP_V6=exports.ATYP_V6=4;const BYTE_ORDER_BE=exports.BYTE_ORDER_BE=0;const BYTE_ORDER_LE=exports.BYTE_ORDER_LE=1;function numberToBuffer(num,len=2,byteOrder=BYTE_ORDER_BE){if(len<1){throw Error('len must be greater than 0')}const isOutOfRange=num>parseInt(`0x${'ff'.repeat(len)}`);if(isOutOfRange){throw Error(`Number ${num} is too long to store in a '${len}' length buffer`)}const buf=Buffer.alloc(len);if(byteOrder===BYTE_ORDER_BE){buf.writeUIntBE(num,0,len)}else{buf.writeUIntLE(num,0,len)}return buf}function parseURI(uri){let _uri=uri;let _port=null;if(_uri.startsWith('http://')){_uri=_uri.substr(7);_port=80}if(_uri.startsWith('https://')){_uri=_uri.substr(8);_port=443}const parts={};_urijs2.default.parseHost(_uri,parts);const hostname=parts.hostname,port=parts.port;const addrType=_net2.default.isIP(hostname)?_net2.default.isIPv4(hostname)?ATYP_V4:ATYP_V6:ATYP_DOMAIN;return{type:addrType,host:_net2.default.isIP(hostname)?_ip2.default.toBuffer(hostname):Buffer.from(hostname),port:numberToBuffer(port||_port||80)}}function getRandomInt(min,max){min=Math.ceil(min);max=Math.ceil(max);return Math.floor(_crypto2.default.randomBytes(1)[0]/255*(max-min+1))+min}function getRandomChunks(buffer,min,max){const totalLen=buffer.length;const bufs=[];let ptr=0;while(ptr<totalLen-1){const offset=getRandomInt(min,max);bufs.push(buffer.slice(ptr,ptr+offset));ptr+=offset}if(ptr<totalLen){bufs.push(buffer.slice(ptr))}return bufs}function getChunks(buffer,maxSize){const totalLen=buffer.length;const bufs=[];let ptr=0;while(ptr<totalLen-1){bufs.push(buffer.slice(ptr,ptr+maxSize));ptr+=maxSize}if(ptr<totalLen){bufs.push(buffer.slice(ptr))}return bufs}function getUTC(){const ts=Math.floor(new Date().getTime()/1e3);return numberToBuffer(ts,4,BYTE_ORDER_BE)}function hexStringToBuffer(str){return Buffer.from(str,'hex')}function isValidHostname(hostname){if(hostname.length<1||hostname.length>253){return false}if(/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i.test(hostname)===false){return false}if(/^[^.]{1,63}(\.[^.]{1,63})*$/.test(hostname)===false){return false}return true}function isValidPort(port){if(typeof port!=='number'){return false}if(port<0||port>65535){return false}return true}function md5(buffer){const md5=_crypto2.default.createHash('md5');md5.update(buffer);return md5.digest()}function hmac(algorithm,key,buffer){const hmac=_crypto2.default.createHmac(algorithm,key);return hmac.update(buffer).digest()}function EVP_BytesToKey(password,keyLen,ivLen){let _data=Buffer.from(password);let i=0;const bufs=[];while(Buffer.concat(bufs).length<keyLen+ivLen){if(i>0){_data=Buffer.concat([bufs[i-1],Buffer.from(password)])}bufs.push(md5(_data));i+=1}return Buffer.concat(bufs).slice(0,keyLen)}function HKDF(hash,salt,ikm,info,length){const prk=hmac(hash,salt,ikm);let t=Buffer.alloc(0);let okm=Buffer.alloc(0);for(let i=0;i<Math.ceil(length/prk.length);++i){t=hmac(hash,prk,Buffer.concat([t,info,Buffer.alloc(1,i+1)]));okm=Buffer.concat([okm,t])}return okm.slice(0,length)}