From 17bbd40ee1d410eb5590587127c82c96cb30ae9c Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Thu, 13 May 2021 13:03:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=EF=BC=8C=E4=BD=BF=E5=BE=97?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=E5=8F=82=E6=95=B0=20key=20=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d4c9e35..b820906 100644 --- a/index.js +++ b/index.js @@ -5,15 +5,28 @@ const crypto = require('crypto') module.exports = { createToken: function (content, key) { // content 可以是数字,非空字符串或非空对象,不可以是数组。 + // key 可以未定义,则默认设为空字符串,再转化为哈希。(jsonwebtoken 要求 key 必须有值) try { - return JsonWebToken.sign(content, crypto.createHash('sha256').update(key, 'utf8').digest('hex')) + return JsonWebToken.sign( + content, + crypto + .createHash('sha256') + .update(key || '', 'utf8') + .digest('hex') + ) } catch (exp) { return null } }, verifyToken: function (token, key) { try { - return JsonWebToken.verify(token, crypto.createHash('sha256').update(key, 'utf8').digest('hex')) + return JsonWebToken.verify( + token, + crypto + .createHash('sha256') + .update(key || '', 'utf8') + .digest('hex') + ) } catch (exp) { return null }