Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Encryption in nodejs

Encryption in nodejs


I'm trying to port the following php Code to javascript on node.js:

$mac = hash_hmac('SHA256', 'string', 'secret', true);
$coded = base64_encode($mac);

I've tried the following:

var Crypto = require('crypto');
var code = Crypto.util.bytesToBase64(Crypto.HMAC(Crypto.SHA256, 'string', 'secret', { asBytes: true }));

I get the error:

TypeError: Object #Object has no method 'HMAC'

I'm new to node.js, what am I doing wrong?


var code = Crypto.createHmac('SHA256', secret_key).update(to_encode).digest('base64');

Problem courtesy of: Alex


You want to use the createHmac function instead.

Crypto.createHmac("SHA256", 'secret').update('string').digest('base64')
Solution courtesy of: Tesserex


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Encryption in nodejs


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription