Omar Azzabi Omar Azzabi - 10 months ago 47
Javascript Question

Convert from Fantom to Javascript

Can someone help me convert the following Fantom code to Javascript?

// compute salted hmac
hmac := Buf().print("${username}:${userSalt}").hmac("SHA-1", password.toBuf).toBase64

// now compute login digest using nonce
digest := "${hmac}:${nonce}".toBuf.toDigest("SHA-1").toBase64

I've been able to compute the
variable using CryptoJS:

var hash = CryptoJS.HmacSHA1("alice:6s6Q5Rn0xZP0LPf89bNdv+65EmMUrTsey2fIhim/wKU=", "secret");

var hmac = hash.toString(CryptoJS.enc.Base64);

But I'm still struggling with the digest.

If you post an example, here are the variables I'm using in testing:

username : "alice"
password : "secret"
userSalt : "6s6Q5Rn0xZP0LPf89bNdv+65EmMUrTsey2fIhim/wKU="
nonce : "3da210bdb1163d0d41d3c516314cbd6e"
hmac : "z9NILqJ3QHSG5+GlDnXsV9txjgo="
digest : "B2B3mIzE/+dqcqOJJ/ejSGXRKvE="


This answer uses CryptoJS as you've already had some success with it:

var username = "alice";
var password = "secret";
var userSalt = "6s6Q5Rn0xZP0LPf89bNdv+65EmMUrTsey2fIhim/wKU=";
var nonce    = "3da210bdb1163d0d41d3c516314cbd6e";

var hmac     = CryptoJS.HmacSHA1(username + ":" + userSalt, password).toString(CryptoJS.enc.Base64);
var digest   = CryptoJS.SHA1(hmac + ":" + nonce).toString(CryptoJS.enc.Base64);

console.log(hmac);   // --> z9NILqJ3QHSG5+GlDnXsV9txjgo=
console.log(digest); // --> B2B3mIzE/+dqcqOJJ/ejSGXRKvE=

Note it uses the following CryptoJS files:


You can see a live example in this JS paste bin:,console