pctripsesp
pctripsesp

Reputation: 53

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.

I have in 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):

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. So whe have key, message and algorithm to create the hash. But in the Javascript sample code only Key and algorithm type are set. In the Javascript hmac function message is supposed to be an empty string? What should be the most accurate translation?

Upvotes: 0

Views: 431

Answers (1)

xtj7
xtj7

Reputation: 578

So you are digesting an hmac in JavaScript that has no content? 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.

Can you be more specific as why you try to use hmac without content?

Normally in JS you would do something like this:

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.

Upvotes: 1

Related Questions