diff --git a/index.js b/index.js index f7205e2..6c66d25 100644 --- a/index.js +++ b/index.js @@ -133,6 +133,14 @@ module.exports = { return num }, + hash (data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) { + if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data) + if (salt && typeof salt === 'string') data = data + salt + const inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView. + const outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64' + return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding) + }, + /** * 用户编号转邀请码 *