From 11d3a2cf7ae7cea16f1b213449df19b67fe0d787 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Mon, 1 Jun 2020 13:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=20hash=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BD=BF=E5=BE=97=E5=89=8D=E7=AB=AF=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E4=BE=9D=E8=B5=96=20tic.crypto=20=E8=80=8C=E5=87=8F?= =?UTF-8?q?=E5=B0=8F=E4=BD=93=E7=A7=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 3d68342..e2d6efb 100644 --- a/index.js +++ b/index.js @@ -130,4 +130,14 @@ module.exports = { return format }, + 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 + let 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. + let 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) + }, + } \ No newline at end of file