python uuid5在javascript nodejs中相当于

我试图将python代码转换为节点js。 在将此代码转换为JS时需要帮助。

“`

import uuid uuid_salt = '9909fa72-b690-55dd-ab71-a987953bb438' x = 'hello' uuid_salt = uuid.UUID(uuid_salt) salted_uuid = lambda x: str(uuid.uuid5(uuid_salt, x)) print salted_uuid(x) 

“`

预期产量 – 3e735408-7f83-53cf-b7ce-f9ef69e5ca43

我试图这样写,但输出不匹配

 var uuid_salt = '9909fa72-b690-55dd-ab71-a987953bb438' var x = 'hello' var hmac = crypto.createHmac('sha1', uuid_salt); hmac.setEncoding('hex'); hmac.end(x, function () { hash = hmac.read(); console.log('hash >>> ', hash); }); 

这里是从库实际的python函数复制粘贴

 def uuid5(namespace, name): """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" from hashlib import sha1 hash = sha1(namespace.bytes + name).digest() return UUID(bytes=hash[:16], version=5) 

我猜字节部分丢失,我尝试在JavaScript /节点创build字节数组,但仍然输出closures。

请帮忙。

提前致谢!

Interesting Posts