09-26-2018 08:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-26-2018 08:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
In order to communicate with certain APIs I need the companion to be able to create SHA256 signatures and SHA512 HMACs. Unfortunately, `require('crypto')` as you would in nodeJS does not import the cryptography library.
Is there a way to import the standard node Crypto library or otherwise generate those signatures? I don't feel smart enough to roll my own hashing functions.
Answered! Go to the Best Answer.

Accepted Solutions
09-27-2018 01:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-27-2018 01:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Found a way!
If anyone comes looking for a way to create HMAC and SHA512/SHA256 cryptography on the Fitbit companion, this answer will probably help you along. On top of that, you'll have to use Tjeerd's method of including the CryptoJS library.

09-26-2018 12:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-26-2018 12:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
You'll need an ES6 module to import. Just place the js file in your companion folder and import from it.

09-26-2018 12:59
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


09-26-2018 12:59
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I see you also saw this thread https://community.fitbit.com/t5/SDK-Development/Using-non-ES6-modules/m-p/2972514#M6292

09-26-2018 13:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-26-2018 13:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
but that library does not seem to create the same results.

09-27-2018 01:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-27-2018 01:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Found a way!
If anyone comes looking for a way to create HMAC and SHA512/SHA256 cryptography on the Fitbit companion, this answer will probably help you along. On top of that, you'll have to use Tjeerd's method of including the CryptoJS library.

09-28-2018 14:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

09-28-2018 14:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Crypto-js also supports ES6 import: https://github.com/brix/crypto-js/blob/develop/README.md
That should make 'my' alternative way of importing non ES6 modules in this case obsolete.

09-28-2018 20:10
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-28-2018 20:10
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
So I'm a little bit new to NodeJS. If I do `npm install crypto-js`, I have to copy over the library from the node_modules to the companion folder? Because if I just leave them in node_modules, the imports fail at runtime.

09-29-2018 03:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-29-2018 03:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I tried both: copying it into the companion folder as well as keeping them in the node_modules folder. I then tried importing as per the README, but the build fails:
[18:53:36][error] Error: 'sha256' is not exported by node_modules/crypto-js/sha256.js
respectively:
[18:53:36][error] Error: 'sha256' is not exported by ./crypto-js/sha256.js

09-29-2018 07:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

SunsetRunner
09-29-2018 07:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Silly me; I was using curly braces to import. But that said, I still cannot get access to the create functions without importing all of cryptoJS.
Also see this SO question

06-09-2021 04:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-09-2021 04:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'm glad you've solved your problem. Things like this happen, and it's good to know that we can find the solution here

07-05-2021 10:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-05-2021 10:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
When you receive a signed token, the WIF-based web service must trust that instance of the AD FS.

09-29-2021 02:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

09-29-2021 02:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I usually use a crypto mixer

