From 0cd03ccde98857bc0bdb7b0bf7edf5350297c307 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 1 Nov 2023 14:33:57 +0800 Subject: [PATCH] =?UTF-8?q?crypto.js=E5=8D=87=E7=BA=A7=E4=B8=BA=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 11 +++++------ package.json | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 77cfdf8..eae5e82 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,7 @@ * @date 2020/09/16 17:23:52 */ -import crypto from 'crypto.js' -import { base64encode, base64decode, sha1 } from 'crypto.js' +import { base64encode, base64decode, hmac, sha1 } from 'crypto.js' const HS256_HEADER = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' const DEFAULT_CONFIG = { @@ -14,8 +13,8 @@ const DEFAULT_CONFIG = { secret: 'it_is_secret_key' // jwt密钥, 使用时请修改 } -function hmac(str, secret) { - let buf = crypto.hmac('sha256', str, secret, 'buffer') +function hmac_base64(str, secret) { + let buf = hmac('sha256', str, secret, 'buffer') return base64encode(buf, true) } @@ -44,7 +43,7 @@ export const JwtModule = { let token = '' payload = base64encode(JSON.stringify(payload), true) - token = hmac(`${HS256_HEADER}.${payload}`, secret) + token = hmac_base64(`${HS256_HEADER}.${payload}`, secret) return `${HS256_HEADER}.${payload}.${token}` }, @@ -65,7 +64,7 @@ export const JwtModule = { return false } - if (hmac(jwt.join('.'), secret) === auth) { + if (hmac_base64(jwt.join('.'), secret) === auth) { return payload.data } diff --git a/package.json b/package.json index 6cd3465..f941084 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gm5/jwt", - "version": "2.0.1", + "version": "2.0.2", "type": "module", "description": "json web token", "main": "index.js", @@ -15,6 +15,6 @@ "repository": "https://git.wkit.fun/gm5/jwt.git", "license": "MIT", "dependencies": { - "crypto.js": "^2.0.1" + "crypto.js": "^3.1.2" } }