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

PRNG: remove quota restriction; don't use window. #87

Merged
merged 1 commit into from
Feb 20, 2016
Merged

Conversation

dchest
Copy link
Owner

@dchest dchest commented Feb 19, 2016

randombytes in browsers no longer throw when output length exceeds 65536
bytes. Remove mention of this from README.

Improve detection of environment (browser or Node) by also checking
process.browser.

Replace references to window with self. Fixes #65, closes #66.

@dchest dchest force-pushed the newprng branch 2 times, most recently from dc692df to 1af5522 Compare February 19, 2016 23:37
randombytes in browsers no longer throw when output length exceeds 65536
bytes. Remove mention of this from README.

Improve detection of environment (browser or Node) by also checking
process.browser.

Replace references to window with self. Fixes #65, closes #66.
dchest added a commit that referenced this pull request Feb 20, 2016
PRNG: remove quota restriction; don't use window.
@dchest dchest merged commit 3a13a3f into master Feb 20, 2016
@dchest dchest deleted the newprng branch February 20, 2016 00:55
crypto = window.msCrypto; // Internet Explorer 11+
}
if (crypto) {
if (typeof process === 'undefined' || process.browser) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nw.js (node-webkit) and electron has no process.browser. Don't tested, but this will not work I think.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my point: native crypto is better than DOM realisation. So check if
require('crypto') is present and usable, than fallback to DOM window.crypto
On Feb 20, 2016 18:37, "Dmitry Chestnykh" [email protected] wrote:

In nacl-fast.js
#87 (comment):

@@ -2361,17 +2361,16 @@ nacl.setPRNG = function(fn) {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto;

  • if (typeof window !== 'undefined') {
  • // Browser.
  • if (window.crypto && window.crypto.getRandomValues) {
  •  crypto = window.crypto; // Standard
    
  • } else if (window.msCrypto && window.msCrypto.getRandomValues) {
  •  crypto = window.msCrypto; // Internet Explorer 11+
    
  • }
  • if (crypto) {
  • if (typeof process === 'undefined' || process.browser) {

@b1rdex https://github.com/b1rdex what do you think about this:
https://github.com/dchest/tweetnacl-js/pull/90/files ?


Reply to this email directly or view it on GitHub
https://github.com/dchest/tweetnacl-js/pull/87/files#r53547295.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my point: native crypto is better than DOM realisation. So check if
require('crypto') is present and usable, than fallback to DOM window.crypto

No, both are pretty much equally good CSPRNG (window.crypto is implemented by browsers natively, Node's crypto is from OpenSSL). If window.crypto is available, I'd prefer to use it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it. Then I vote just for process.browser removal.
On Feb 20, 2016 18:43, "Dmitry Chestnykh" [email protected] wrote:

In nacl-fast.js
#87 (comment):

@@ -2361,17 +2361,16 @@ nacl.setPRNG = function(fn) {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto;

  • if (typeof window !== 'undefined') {
  • // Browser.
  • if (window.crypto && window.crypto.getRandomValues) {
  •  crypto = window.crypto; // Standard
    
  • } else if (window.msCrypto && window.msCrypto.getRandomValues) {
  •  crypto = window.msCrypto; // Internet Explorer 11+
    
  • }
  • if (crypto) {
  • if (typeof process === 'undefined' || process.browser) {

Here is my point: native crypto is better than DOM realisation. So check if
require('crypto') is present and usable, than fallback to DOM window.crypto

No, both are pretty much equally good CSPRNG (window.crypto is implemented
by browsers natively, Node's crypto is from OpenSSL).


Reply to this email directly or view it on GitHub
https://github.com/dchest/tweetnacl-js/pull/87/files#r53547362.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new logic will be:

 if (typeof process === 'undefined' || process.browser ||
      (typeof window !== 'undefined' && window.self === self)) {

Do you know if WebWorkers in Electron/nw have process defined?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is long living nw.js issue about this:
nwjs/nw.js#494
So I think that's not available. Can't check now.

2016-02-20 18:54 GMT+10:00 Dmitry Chestnykh [email protected]:

In nacl-fast.js
#87 (comment):

@@ -2361,17 +2361,16 @@ nacl.setPRNG = function(fn) {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto;

  • if (typeof window !== 'undefined') {
  • // Browser.
  • if (window.crypto && window.crypto.getRandomValues) {
  •  crypto = window.crypto; // Standard
    
  • } else if (window.msCrypto && window.msCrypto.getRandomValues) {
  •  crypto = window.msCrypto; // Internet Explorer 11+
    
  • }
  • if (crypto) {
  • if (typeof process === 'undefined' || process.browser) {

The new logic will be:

if (typeof process === 'undefined' || process.browser ||
(typeof window !== 'undefined' && window.self === self)) {

Do you know if WebWorkers in Electron/nw have process defined?


Reply to this email directly or view it on GitHub
https://github.com/dchest/tweetnacl-js/pull/87/files#r53547482.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think actually, you're right about process.browser, let's just do the normal detection of what I intended — if we're asking if self.crypto exists, let's check that, and try to fallback to require. I'll send pull request soon and post a link here.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, here it is:

https://github.com/dchest/tweetnacl-js/pull/90/files

var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
if (crypto && crypto.getRandomValues) {
...
  } else if (typeof require !== 'undefined') {

If we have self.crypto.getRandomValues, use it, otherwise try require('crypto')

@b1rdex
Copy link

b1rdex commented Feb 20, 2016

require('crypto') is working in electron, so PRNG selection is OK.
But I can't use nacl anymore, because it's initialized through module.exports and there is no way to require for content loaded through <script>.

Summary: web workers and now good, but usage problems in nw.js and electron are still present.

@dchest
Copy link
Owner Author

dchest commented Feb 20, 2016

@b1rdex strange, what changed with regards to module.exports? It was initialized like this before:

})(typeof module !== 'undefined' && module.exports ? module.exports : (window.nacl = window.nacl || {}));

and now just the later part changed (which isn't triggered if module.export exist)

})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));

Anyway, I'm looking into it.

@b1rdex
Copy link

b1rdex commented Feb 20, 2016

I know module.exports line not changed.
The problem was there already.
On Feb 20, 2016 18:22, "Dmitry Chestnykh" [email protected] wrote:

@b1rdex https://github.com/b1rdex strange, what changed with regards to
module.exports? It was initialized like this before:

})(typeof module !== 'undefined' && module.exports ? module.exports : (window.nacl = window.nacl || {}));

and now just the later part changed (which isn't triggered if
module.export exist)

})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));

Anyway, I'm looking into it.


Reply to this email directly or view it on GitHub
#87 (comment).

@dchest
Copy link
Owner Author

dchest commented Feb 20, 2016

Ah, ok.

@dchest
Copy link
Owner Author

dchest commented Feb 20, 2016

OK, they have this in FAQ:

https://github.com/atom/electron/blob/master/docs/faq/electron-faq.md#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron

so I guess if even jQuery requires deleting require etc., we can leave it as is, as I don't see how I can solve this. If you have suggestions, please let me know.

@nazar-pc nazar-pc mentioned this pull request Nov 5, 2017
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

Successfully merging this pull request may close these issues.

Need a better module wrapping Way to use nacl in web workers
2 participants