0

I'm using an openPgpJs library in my app, created with Apache Cordova.

Here's the part of the code:

        var publicKey = openpgp.key.readArmored(_publicKey);
        openpgp.encryptMessage(publicKey.keys, text).then(function (pgpMessage) {
            // success
            callback(pgpMessage);
        }).catch(function (error) {
            // failure
            console.error(error);
        });

It works fine but not on WP8. If fails, 'cause openpgp var is undefined. In that library source there's such code at the very beginning:

!function (e) {
"object" == typeof exports ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : "undefined" != typeof window ? window.openpgp = e() : "undefined" != typeof global ? global.openpgp = e() : "undefined" != typeof self && (self.openpgp = e())

}

so openpgp should be defined. How can I make it work?

UPDATE I've added var openpgp = window.openpgp; and the error disappeared. Now it joust fails to encrypt message - and it's hard to debug, cause the library code is minified

k102
  • 7,861
  • 7
  • 49
  • 69

1 Answers1

0

So, for anyone who came here with the same problems.

  1. Use var openpgp = window.openpgp; to avoid "undefined" error.
  2. The next problem is inside getRandomValues function - there is an exception No secure random number generator available.. To avoid this error too I've changed the last (with exception) statement to

var sjcl = window.sjcl; if (sjcl.random.isReady()) { var buf = new Uint8Array(1); bytes = sjcl.random.randomWords(buf.length); buf.set(bytes); }else{ var bytes = []; for (var i = 0; i < buf.length; i++) { bytes.push(isaac.rand()); buf.set(bytes); } } according to @ZeroG answer for this question: Secure random numbers in javascript?

Hope it can help someone =)

Community
  • 1
  • 1
k102
  • 7,861
  • 7
  • 49
  • 69