'use strict'; var crypto = require('crypto'); /** * hash * * @param {String} method hash method, e.g.: 'md5', 'sha1' * @param {String|Buffer|Object} s * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'. * @return {String} md5 hash string * @public */ exports.hash = function hash(method, s, format) { var sum = crypto.createHash(method); var isBuffer = Buffer.isBuffer(s); if (!isBuffer && typeof s === 'object') { s = JSON.stringify(sortObject(s)); } sum.update(s, isBuffer ? 'binary' : 'utf8'); return sum.digest(format || 'hex'); }; /** * md5 hash * * @param {String|Buffer|Object} s * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'. * @return {String} md5 hash string * @public */ exports.md5 = function md5(s, format) { return exports.hash('md5', s, format); }; /** * sha1 hash * * @param {String|Buffer|Object} s * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'. * @return {String} sha1 hash string * @public */ exports.sha1 = function sha1(s, format) { return exports.hash('sha1', s, format); }; /** * sha256 hash * * @param {String|Buffer|Object} s * @param {String} [format] output string format, could be 'hex' or 'base64'. default is 'hex'. * @return {String} sha256 hash string * @public */ exports.sha256 = function sha256(s, format) { return exports.hash('sha256', s, format); }; /** * HMAC algorithm. * * Equal bash: * * ```bash * $ echo -n "$data" | openssl dgst -binary -$algorithm -hmac "$key" | openssl $encoding * ``` * * @param {String} algorithm, dependent on the available algorithms supported by the version of OpenSSL on the platform. * Examples are 'sha1', 'md5', 'sha256', 'sha512', etc. * On recent releases, `openssl list-message-digest-algorithms` will display the available digest algorithms. * @param {String} key, the hmac key to be used. * @param {String|Buffer} data, content string. * @param {String} [encoding='base64'] * @return {String} digest string. */ exports.hmac = function hmac(algorithm, key, data, encoding) { encoding = encoding || 'base64'; var hmac = crypto.createHmac(algorithm, key); hmac.update(data, Buffer.isBuffer(data) ? 'binary' : 'utf8'); return hmac.digest(encoding); }; /** * Base64 encode string. * * @param {String|Buffer} s * @param {Boolean} [urlsafe=false] Encode string s using a URL-safe alphabet, * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. * @return {String} base64 encode format string. */ exports.base64encode = function base64encode(s, urlsafe) { if (!Buffer.isBuffer(s)) { s = typeof Buffer.from === 'function' ? Buffer.from(s) : new Buffer(s); } var encode = s.toString('base64'); if (urlsafe) { encode = encode.replace(/\+/g, '-').replace(/\//g, '_'); } return encode; }; /** * Base64 string decode. * * @param {String} encode, base64 encoding string. * @param {Boolean} [urlsafe=false] Decode string s using a URL-safe alphabet, * which substitutes - instead of + and _ instead of / in the standard Base64 alphabet. * @param {encoding} [encoding=utf8] if encoding = buffer, will return Buffer instance * @return {String|Buffer} plain text. */ exports.base64decode = function base64decode(encodeStr, urlsafe, encoding) { if (urlsafe) { encodeStr = encodeStr.replace(/\-/g, '+').replace(/_/g, '/'); } var buf = typeof Buffer.from === 'function' ? Buffer.from(encodeStr, 'base64') : new Buffer(encodeStr, 'base64'); if (encoding === 'buffer') { return buf; } return buf.toString(encoding || 'utf8'); }; function sortObject(o) { if (!o || Array.isArray(o) || typeof o !== 'object') { return o; } var keys = Object.keys(o); keys.sort(); var values = []; for (var i = 0; i < keys.length; i++) { var k = keys[i]; values.push([k, sortObject(o[k])]); } return values; }