(secret, header, payload)
| 1449 | |
| 1450 | // 生成JWT(JSON Web Token) |
| 1451 | function generateJWT(secret, header, payload) { |
| 1452 | // 对header和payload部分进行UTF-8编码,然后转换为Base64URL格式 |
| 1453 | const encodedHeader = base64UrlSafe(btoa(JSON.stringify(header))); |
| 1454 | const encodedPayload = base64UrlSafe(btoa(JSON.stringify(payload))); |
| 1455 | // 生成 jwt 签名 |
| 1456 | let hmacsha256 = base64UrlSafe(CryptoJS.HmacSHA256(encodedHeader + "." + encodedPayload, secret).toString(CryptoJS.enc.Base64)) |
| 1457 | return `${encodedHeader}.${encodedPayload}.${hmacsha256}`; |
| 1458 | } |
| 1459 | |
| 1460 | // 将Base64字符串转换为Base64URL格式的函数 |
| 1461 | function base64UrlSafe(base64String) { |
no test coverage detected