Use node crypto library?

Hello, I’m developing an addon and have to create a hmac sha512 hash but jpm says that the crypto module can’t be found (https://nodejs.org/api/crypto.html#crypto_crypto_createhmac_algorithm_key) - can’t I use standard node modules in firefox addon development?
Sorry I’m very new to this.
If not, is there another way to create the hash?

Best Regards, Michael

Most node modules won’t work on an extension because they are designed to work server side rather than client side. It could be that you just need to get the non-node version of the library, since I know many add-ons use this crypto lib.

I have the same problem. I’m developing an add-on using jpm and need cryptography functionality. I’m confused by your response because it’s not clear to me how I can load a .js file containing global functions into my node.js namespace. How do I do that?

Do I rewrite all the functions in the crypto lib to become “exports.” functions? So that “function foo(){}” becomes “exports.foo = function(){}”.

I need to do something like this, otherwise I can’t use this lib. I’m a noob at node.js so excuse me if this question is stupid.

Any help would be much appreciated.

Thanks,
Andrew

I don’t know if this helps but we have a something that goes by the name of crypto - https://developer.mozilla.org/en-US/docs/Web/API/Crypto

Thanks. But it’s not clear to me if that is available for jpm add-on development though.

You can use it in jpm addons like this:

var {Cu} = require('chrome');
Cu..importGlobalProperties(["crypto"]);
1 Like

Thanks for helping me. I’m confused by the different APIs. Is this the “chrome Low- Level API” or some other thing?

Where is this documented? OR What crypto library am I loading when I do this?

Also, what’s up with the double dots (…)?

Sorry if these are noob questions, but the documentation feels scattered and incomplete.

Thanks again for your help. I really appreciated it.

–Andrew

I’m not sure myself, I would search github to see how people use it:

https://github.com/search?q=importGlobalProperties+crypto&type=Code&utf8=✓

and also dxr:

https://dxr.mozilla.org/mozilla-central/search?q=crypto&redirect=false&case=false

But what are you trying to do? If you could create a topic asking exactly what you want to do I think you can get better help.

Thanks Notidart.

I think I’ve got what I’m trying to do with this code. I wanted to be able to generate RSA keys.

let { Cu, Cc, Ci } = require(‘chrome’);
Cu.importGlobalProperties([“crypto”]); // Bring in our crypto libraries

// Many thanks to https://github.com/diafygi/webcrypto-examples
function genKey(){
crypto.subtle.generateKey(
{
name: “RSASSA-PKCS1-v1_5”,
modulusLength: 2048,
publicExponent: new Uint8Array([0x001, 0x00, 0x01]),
hash: {name: “SHA-256”}
},
true,
[“sign”, “verify”]
)
.then(function(key){ // Returns a keypair object
return key;
})
.catch(function(err){
dump("\ngenKey() Error:" + err);
});
}

I’ve got an issue with simple storage not actually storing anything now, but I’ll start another thread for the if I can’t figure it out.

Thanks,
Andrew