JungHsuan / react-native-openpgp-webview

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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


Languages

Language:Java 40.4%Language:C++ 18.2%Language:JavaScript 16.5%Language:Objective-C++ 9.5%Language:Objective-C 6.3%Language:Makefile 4.0%Language:Ruby 3.4%Language:Starlark 1.6%