browserify / crypto-browserify

partial implementation of node's `crypto` for the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Basic usage question

KayakinKoder opened this issue · comments

Pardon my ignorance, I'm very new to browserify-ication; how do I use this (awesome looking) package? I'm not using Node, just a plain old vanilla apache webserver. The /example/index.html gives me an error "require is not defined", which makes sense, but I'm not sure how to proceed.

So far I've:

1.) browserify-ied index.js to a bundle.js
2.) included bundle.js in my index.html file (<script src=bundle.js></script>)

Have tried a number of things from this point on with no success. Thanks for any help and the great package

to use crypto browserify, all you need to do is browserify a node module that uses node's
crypto module. like many of the other core node modules, browserify will insert a javascript implementation to run in the browser.

if you have something like

//index.js
var crypto = require('crypto')
//etc

then do
browserify index.js > bundle.js
that is all. if that isn't working, you are probably missing something very basic.
you might want to try stackoverflow for questions like that?

to use crypto browserify, all you need to do is browserify a node module that uses node's
crypto module. like many of the other core node modules, browserify will insert a javascript implementation to run in the browser.

if you have something like

//index.js
var crypto = require('crypto')
//etc

then do
browserify index.js > bundle.js
that is all. if that isn't working, you are probably missing something very basic.
you might want to try stackoverflow for questions like that?

Thanks, I almost posted on stackoverflow but this seemed a bit package specific, so I thought it would be better here. I'm not using node at all. I'd like to use crypto-browserify as in the "Browser" usage example here: https://www.npmjs.com/package/simple-random

E.g. in a plain vanillla index.html file, simple-random can be used as:

<script src="simple-random/dist/simple_random.js"></script>
<script>
var randomString = window.simpleRandom();
</script>

Sorry if you answered this, since I've only used node a few times for small local pet projects I'm not making the connection here.

This will work for any package or node module:

`"use strict";

/*
The node.js modules to be ported to the client must be added
to the window.myNodeObjects object.

Once bundle has been included in html page
    <script src="bundle.js"></script>
javascript calls can be made to myNodeObjects.* eg
    var hash = myNodeObjects.crypto.createHash('md5').update("plaintext").digest("hex");;

*/

// manually add required constants to crypto (not browserified)
var crypto = require('crypto');
crypto.constants = {
RSA_PKCS1_PADDING: 1
};

// update the browser's myNodeObjects object
window.myNodeObjects = {
crypto: crypto
};
`

if you browserify this package with the -s <name> flag then this package will be available under that name, you probably don't want to use crypto as the name as that's taken but you could do browserify -s bCrypto < index.js > bundle.js

Ah, thank you guys. I was under the assumption that behavior was something that package authors had to specifically code to allow. None of the browserify tutorials I read have mentioned this, and for non-node apps this is essential. Thanks for the clarification, that helps immensely

see my fork to see some hassle free examples, i think the original repo example code is too old and has many unimplmeneted functions.

use bundle3.js on my repo

https://github.com/ziyiwang/crypto-browserify

Apart from using the bundle3.js file provided by @ziyiwang, is there an "official" way to use crypto-browserify without using browserify? I don't have a Nodejs project or module at all, my only goal is to use Nodejs's crypto module in the browser because window.crypto lacks a lot of functions.