简体   繁体   English

HMAC函数Javascript转python

[英]HMAC function Javascript to python

I am trying to translate some crypto function from javascript to python, but I don't know what is the equivalent for Hmac function. 我正在尝试将一些加密函数从javascript转换为python,但是我不知道什么等同于Hmac函数。

I have in javascript: 我在javascript中:

var crypto = require('crypto');
var myHash = function(seed){
    var hash = crypto.createHmac('sha256', seed).digest('hex');
(...)

And in python It is supposed to be (I guess): 在python中应该是(我猜):

from hashlib import sha256
import hmac
def myHash(seed):
    hash = hmac.new(serverSeed,b"",sha256).hexdigest()

The thing is that in Python hmac function a third parameter is needed: the message. 事实是,在Python hmac函数中,需要第三个参数:消息。 So whe have key, message and algorithm to create the hash. 因此,具有创建哈希的密钥,消息和算法。 But in the Javascript sample code only Key and algorithm type are set. 但是在Javascript示例代码中,仅设置了密钥和算法类型。 In the Javascript hmac function message is supposed to be an empty string? 在Javascript中,hmac函数消息应该是空字符串吗? What should be the most accurate translation? 什么是最准确的翻译?

So you are digesting an hmac in JavaScript that has no content? 因此,您要在JavaScript中消化没有内容的hmac吗? What is the purpose of that? 目的是什么?

You usually want to use hmac for message authentication, but if you don't have a message, there isn't really anything to authenticate. 通常,您希望使用hmac进行消息身份验证,但是如果没有消息,则实际上没有任何要进行身份验证的内容。

Can you be more specific as why you try to use hmac without content? 您能否更具体地说明为什么尝试不使用内容的hmac?

Normally in JS you would do something like this: 通常在JS中,您将执行以下操作:

var crypto = require('crypto');
var myHash = function(seed){
    var hash = crypto.createHmac('sha256', seed).update('my message').digest('hex');
(...)

And that would be pretty much exactly the same thing in Python. 这在Python中几乎是完全一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM