[JWT] JWT Signature With RS256 - Learn The Advantages Compared to HS256
The advantage of RS256 over HS256 is RS256 no longer need to share the secret key between client and server side.
To create a token, we need to private key, which should be kept safe. We can use third-party server such as Auth0 to generate private-public key paris.
The public key is used only to validate JWT token on the server, and cannot use public key to create a JWT token, so even the server is hacked, hacker still cannot use the information create a token to access the data.
Create a token:
var jwt = require('jsonwebtoken'); var fs = require('fs'); var privateKey = fs.readFileSync('./demos/private.key'); var payload = { name: 'Alice' }; var token = jwt.sign(payload, privateKey, { algorithm: 'RS256', expiresIn: 120, subject: "1" }); console.log('RSA 256 JWT', token);
Validate a token:
var jwt = require('jsonwebtoken'); var fs = require('fs'); // verify an existing JWT var existingToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWxpY2UiLCJpYXQiOjE1MDI5MDMxNTcsImV4cCI6MTUwMjkwMzI3Nywic3ViIjoiMSJ9.KQJ-f3r4TNCLVrox1JaL5pxQAM6vSw4CNKj1lCf3HDWXGdIHW5rgD5odKpNBjrkbl1smjEL_ClLnFwG_iGDPKvu2bqktcrbXwi1-XUrY-jDKLkpoEHL2C9tGYnyDRl6Pg1SP97Hl-VWkGNyekYMerL8vh0RwgcK7y8UsuA33WgnP1DtfhKIghwcd493ARN4nBvmMJ11Zk35c7FBIN2w4Xl4ny8RU4l0_xy5DBF3JAKV1jilTHOKEvsrY8Ry3qRKaxxR6-QE_pfGOte3BRlt6544BUul1yI662tVAn1R28KXKnwCGAwo_HZ1kC-OrxmsjoXI4HDuHG2k5eRX-QC_W4Q'; var publicKey = fs.readFileSync('./demos/public.key'); console.log("verifying"); const verify = jwt.verify(existingToken, publicKey); console.log("Decoded JWT:", verify);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-09-22 [Angular 2] Select From Multiple Nested Angular 2 Elements