-2

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 hmac 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="
Steve Eynon
  • 4,979
  • 2
  • 30
  • 48
  • 1
    Can you show us what you've tried so far? – unbindall Mar 04 '16 at 14:12
  • I tryed using 2 web sites: http://tools.bin63.com/hmac-generator and https://caligatio.github.io/jsSHA/ the fact is I am not understanding exactly which variables I need to pass and when? If I only get how exactly to pass variables and when to hmac...? – Omar Azzabi Mar 04 '16 at 14:38

1 Answers1

1

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:

/rollups/sha1.js
/rollups/hmac-sha1.js
/components/enc-base64-min.js

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

https://jsbin.com/luvinayoyi/edit?js,console

Steve Eynon
  • 4,979
  • 2
  • 30
  • 48