Saturday, 22 April 2017

python uuid5 equivalent in javascript nodejs

I am trying to convert python code to node js. Need help in converting this code to 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)

```

Expected Output - 3e735408-7f83-53cf-b7ce-f9ef69e5ca43

I tried writing this way but output does not match

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);
});

here is the actual python function copy paste from library

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)

I guess the bytes part is missing i tried created bytes array in JavaScript/Node but still the output is off.

Please help.

Thanks in advance!



via Amitesh

No comments:

Post a Comment