Skip to content

A simple example to run openpgpjs using react-native-webview in react-native

Notifications You must be signed in to change notification settings

JungHsuan/react-native-openpgp-webview

Repository files navigation

react-native-openpgp-webview

This is a simple example to run openpgpjs using react-native-webview.
This is a full react-native project. You can simply clone and run on simulator or real devices.

Here we use react-native-webview to open a simple html which we load openpgpjs library by adding <script/> like this:

<html>
  <body>
    <script src="https://unpkg.com/openpgp@5.3.0/dist/openpgp.min.js"></script>
    <div>hello word</div>
  </body>
</html>

Then, we can inject some js code to run openpgp by calling the method injectJavaScript().
Example javaScript code to use openpgp:

const jscode = `(async () => {
    const message = await openpgp.createMessage({
        binary: new Uint8Array([0x01, 0x01, 0x01]),
    });
    const encrypted = await openpgp.encrypt({
      message, // input as Message object
      passwords: ["secret stuff"], // multiple passwords possible
      format: "binary", // don't ASCII armor (for Uint8Array output)
    });
    console.log(encrypted); // Uint8Array
 };
 true;`;

Finally, inject this code by:

webViewRef.current.injectJavaScript(jscode);

That's it!
You can find full code in App.js

About

A simple example to run openpgpjs using react-native-webview in react-native

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published