Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in browser #184

Open
bernardoadc opened this issue Sep 8, 2020 · 1 comment
Open

Use in browser #184

bernardoadc opened this issue Sep 8, 2020 · 1 comment

Comments

@bernardoadc
Copy link

So I am trying to encrypt data using public key from backend in the browser. Since it's not https, I can't really use crypto.subtle. I've tryed bundling with browserify with no success. I ended up using rollup:

import { rollup } from 'rollup'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import globals from 'rollup-plugin-node-globals'
import builtins from 'rollup-plugin-node-builtins'
import { terser } from 'rollup-plugin-terser'

export default [
  {
    input: 'node_modules/node-rsa/src/NodeRSA.js',
    output: {
      file: 'web_modules/node-rsa.js',
      format: 'iife',
      name: 'NodeRSA'
    },
    plugins: [
      resolve({
        browser: true,
        preferBuiltins: false
      }),
      commonjs(),
      globals(),
      builtins()
    ]
  },
  {
    input: 'node_modules/node-rsa/src/NodeRSA.js',
    output: {
      file: 'web_modules/node-rsa.min.js',
      format: 'iife',
      name: 'NodeRSA',
      sourcemap: true
    },
    plugins: [
      resolve({
        browser: true,
        preferBuiltins: true
      }),
      commonjs(),
      globals(),
      builtins(),
      terser()
    ]
  }
]

But something is not right. When encrypting it shows this error message:

const key = new NodeRSA()
const publicKey = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI6YyPTvCoQdVdmQh7BhweUthE3klCL9
FvPCHDTl2TGSQJyv3D/v6w8fJDDzOMziow+oEia46VSNWjkp75iZXO8CAwEAAQ==
-----END PUBLIC KEY-----`
key.importKey(publicKey, 'public')

let e = key.encrypt('hello you')

Uncaught Error: Error during encryption. Original error: TypeError: crypt.createHash is not a function
at NodeRSA.$$encryptKey (:10496:31)
at NodeRSA.encrypt (:10451:33)
at :1:13

Is the bundling missing something? (also, couldn't a browser single file version be included in future releases?)

@deju
Copy link

deju commented Jul 3, 2023

Replace 'crypto' with 'crypto-browserify' in src directory.

const rollup = require('rollup');
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const globals  = require('rollup-plugin-node-globals');
const builtins = require('rollup-plugin-node-builtins');
const json = require('@rollup/plugin-json');
const { terser } = require('rollup-plugin-terser');

module.exports = [
  {
    input: 'src/NodeRSA.js',
    output: {
      file: 'dist/node-rsa.js',
      format: 'umd',
      name: 'NodeRSA'
    },
    plugins: [
      json(),
      resolve({
        browser: true,
        preferBuiltins: false
      }),
      commonjs(),
      globals(),
      builtins()
    ]
  },
  {
    input: 'src/NodeRSA.js',
    output: {
      file: 'dist/node-rsa.min.js',
      format: 'umd',
      name: 'NodeRSA',
      sourcemap: true
    },
    plugins: [
      json(),
      resolve({
        browser: true,
        preferBuiltins: true
      }),
      commonjs(),
      globals(),
      builtins(),
      terser()
    ]
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants