Variable does not call function, WebWorker, and Scope functions

1

I have serious problems, I have a WebWorker that imports a file, but at the time of calling the function with the variable does not work. See:

importScripts(
  './libs/RSA.js'
);

self.onmessage = function (e) {
        JSEncrypt.getKey(function () {
          privateKey = RSA.getPrivateKey();
          publicKey = RSA.getPublicKey();
        });
}

The RSA file is nothing more than: link

The error returned is: Uncaught TypeError: RSA.getKey is not a function     

asked by anonymous 17.04.2015 / 23:38

1 answer

3

The getKey function does not belong to JSEncrypt , but JSEncrypt.prototype . This means that every instance of JSEncrypt - created via new JSEncrypt(...) - will have this method.

Create an instance of the mode you like best (with or without options), and call the method on it:

var window = self;
importScripts(
  'jsencrypt.js'
);

var meuRSA = new JSEncrypt();

self.onmessage = function (e) {
        meuRSA.getKey(function () {
          privateKey = meuRSA.getPrivateKey();
          publicKey = meuRSA.getPublicKey();

          // Para visualização
          console.log(privateKey);
          console.log('');
          console.log(publicKey);
        });
}
    
18.04.2015 / 00:37