0

I'm trying to print the contents of my digest variable but not having any luck. The below is the code I have. I've read a few tutorials and can't see why it wouldn't work.

  // Store Credentials
  var userName = "username";
  var sharedSecret = "secret";

  // Build Header
    var date = new Date();
    var nonce = md5(Math.random());
    var nonce_ts = date.toISOString().replace(/(\.\d\d\dZ)/ ,'Z');
    var digest = (new Buffer(sha1(nonce + nonce_ts + sharedSecret)).toString('base64'));

  alert(digest);

It was taken from nodeJS and I am trying to make it javascript.

4
  • 3
    Is this Node.js? Buffer and md5 aren't standard JS Commented Aug 14, 2018 at 10:40
  • This looks like NodeJS, NodeJS doesn't have a native alert function, try console.log Commented Aug 14, 2018 at 10:41
  • @Cerbrus Ok I think that is the problem. I took from nodeJS to try and make JS. Do I need to recreate this function in JS? Commented Aug 14, 2018 at 10:43
  • node (like a browser) has a JavaScript engine to run JS code. alert, md5, Buffer are not part of the JavaScript language specification, but are part of an API provided directly by the environment or by a foreign library. Commented Aug 14, 2018 at 10:46

1 Answer 1

3

It was taken from nodeJS and I am trying to make it JavaScript.

Then you need to make sure md5, sha1 and Buffer are ported over to your JavaScript environment. These aren't normally available in a browser.

The reason you're not getting an alert window, is because the code is crashing on those function. You can easily see that by opening your browser's developer console (Usually F12)

Sign up to request clarification or add additional context in comments.

1 Comment

(I've found this library for Buffer, and another one for md5 and sha1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.