javascript - JWT generate token with algorithm ES256 - Stack Overflow

I am trying to generate an accessToken with algorithm ES256 and I use the following code:const jwt = r

I am trying to generate an accessToken with algorithm ES256 and I use the following code:

const jwt = require('jsonwebtoken')

const accessToken = jwt.sign(
  { name: 'John' },
  'testsecret',
  { expiresIn: '24h' },
  { algorithm: 'ES256' }
)

console.log(accessToken)

And I got a token as below:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiIsImlhdCI6MTYxNDY4MzUxNCwiZXhwIjoxNjE0NzY5OTE0fQ.Q9quAufyTQvPvKrTUXzRDUo-o0M4yXSXjqU4vZ9nvvA

I tried pasting this to jwt.io and it seems that it is a HS256 token instead of ES256, did I miss anything?

There is something that I did not do correctly. You can always paste the above code to nodejs and you will see.

I am trying to generate an accessToken with algorithm ES256 and I use the following code:

const jwt = require('jsonwebtoken')

const accessToken = jwt.sign(
  { name: 'John' },
  'testsecret',
  { expiresIn: '24h' },
  { algorithm: 'ES256' }
)

console.log(accessToken)

And I got a token as below:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiIsImlhdCI6MTYxNDY4MzUxNCwiZXhwIjoxNjE0NzY5OTE0fQ.Q9quAufyTQvPvKrTUXzRDUo-o0M4yXSXjqU4vZ9nvvA

I tried pasting this to jwt.io and it seems that it is a HS256 token instead of ES256, did I miss anything?

There is something that I did not do correctly. You can always paste the above code to nodejs and you will see.

Share Improve this question edited Sep 24, 2024 at 3:18 Daryl Wong asked Mar 2, 2021 at 10:42 Daryl WongDaryl Wong 2,4436 gold badges36 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You just need to bine the expiresIn and algorithm arguments - the module takes a single options object:

const jwt = require('jsonwebtoken');
const privatekey = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEICXoLhGdD6jzX5ePTY9O9YBgv0ZZ6oBWDRsjKaeASXp6oAoGCCqGSM49
AwEHoUQDQgAELCnuRSU9Vf+bx65i3Vbibj123RQFrIEaXuMuXunzPXGURKge07fy
FoiMucdGZ2MZGsm37JdlnVGd5yU1h4D4Rg==
-----END EC PRIVATE KEY-----`

const payload = {"id":1}

const accessToken = jwt.sign(payload, privatekey, {
  expiresIn: "24h",
  algorithm: "ES256",
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288230a4415710.html

相关推荐

  • javascript - JWT generate token with algorithm ES256 - Stack Overflow

    I am trying to generate an accessToken with algorithm ES256 and I use the following code:const jwt = r

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信