英文:
How to create token using Hmac in Node js
问题
const crypto = require('crypto');
const mysecret = 'your_secret_key'; // Replace with your secret key
const texttoEncode = 'text_to_encode'; // Replace with the text you want to encode
const hmac = crypto.createHmac('sha256', mysecret);
const hash = hmac.update(texttoEncode, 'utf-8').digest('hex');
英文:
I am trying to convert Java code to Node js to generate token using Hmac.
Java code-
Mac mac = Mac.getInstance("HmacSHA256")
SecretKeySpec key = new SecretKeySpec(mysecret.getBytes("UTF-8","HmacSHA256")
mac.init(key)
byte[] hash = mac.doFinal(texttoEncode.getBytes(UTF-8))
Can anyone please suggest what will be its Javascript /node js equivalent.
答案1
得分: 3
The crypto
内置模块提供了以下签名的 createHmac
方法:
crypto.createHmac(algorithm, key[, options])
要创建一个令牌:
const Crypto = require('crypto');
const token = Crypto.createHmac('sha256', 'a secret').update('data').digest('hex');
console.log(token); // 5da263f0f0ee86707c7c3f590d20066b7107e5ac70a41560926fa634bc78b137
英文:
The crypto
built-in module provides createHmac
method with the following signature:
crypto.createHmac(algorithm, key[, options])
To create a token:
const Crypto = require('crypto');
const token = Crypto.createHmac('sha256', 'a secret').update('data').digest('hex');
console.log(token); // 5da263f0f0ee86707c7c3f590d20066b7107e5ac70a41560926fa634bc78b137
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论