0

I try to generate an RSA key and since this takes some time I want to use multithread.js for the key generation.

My code:

<script src="../js/multithread.js"></script>
<script src="../js/crypto/cryptico.js"></script> 
<script>
var keyPair;
var MT = new Multithread(2);

MT.process(
            function () {
                var bits = 4096;
                return cryptico.generateRSAKey("passphrase", bits);
            }, function (key) {
                keyPair = key;
                publicKey = cryptico.publicKeyString(keyPair);
            }
)();
</script>

I get a

ReferenceError: cryptico is not defined

error.

I know that multithread.js doesn't have the same scope as the application. So how can I access the cryptico library or pass the scope to multithread.js?

firsti
  • 93
  • 1
  • 7

1 Answers1

0

I've found a solution myself.

Just include the necessary file with importScripts within the function. Webworker has this method defined.

Like this:

MT.process(
        function () {
            importScripts('http://localhost:63342/..../js/crypto/cryptico.js');
            var bits = 4096;
            return cryptico.generateRSAKey("passphrase", bits);
        }, function (key) {
            keyPair = key;
            publicKey = cryptico.publicKeyString(keyPair);
        }
)();
firsti
  • 93
  • 1
  • 7