Commit Graph

23 Commits

Author SHA1 Message Date
Christian Klaussner
af6dcad337 Add documentation for Random functions 2016-06-14 19:40:37 +02:00
Avital Oliver
f21ba1dfc3 Glasser code review on #5161 2015-09-20 13:31:39 -07:00
Avital Oliver
baaa0a0db9 Use fast PRNG to generate _id for documents created outside methods
This change harmonizes server document ID generation regardless of whether
it happens inside of a method or not, by using Alea in both cases.

This cuts time of inserting small documents outside of methods
on the server by over 30%, and more importantly makes it easier to be
confident in benchmarking numbers.

---

BACKGROUND

When calling `coll.insert()` on the server within methods, we use the Alea PRNG
(which is fast, can be seeded, and not cryptographically secure) to generate
the `_id` field for the newly created document (unless an `_id` field was
explicitly passed).

The reason we use Alea is so that we can seed the PRNG from the client, as to
ensure consistently chosen IDs for methods that create multiple documents and
run on both client and server.

Prior to this change, when calling `coll.insert()` on the server *not* inside
methods, we'd use Node's cryptographically secure `crypto.getRandomBytes()`
which is slower (due to allocating buffers that need to cross from JS
into native code).

With this change, we always use Alea when generating a document ID.

---

CRYPTOGRAPHICALLY SECURE IDS STILL AVAILABLE

If an app wants to guarantee using a cryptographically secure PRNG
when generating IDs, just generate IDs yourself:
`coll.insert({_id: Random.id(), ...})`.

`Random.id()` still uses a CSPRNG (unless you're on IE, or
on the server and not enough entropy has been collected, which is
basically never the case).

If you *want* the faster Alea algorithm, use `Random.fast.id()`
(The `Random.fast` object has all the same methods as on `Random`)

---

BENCHMARK RESULTS

Here are the measured times for inserting 5000 documents, before
and after this change (on my machine):

Benchmark                         | Before | After
--------------------------------- | ------ | ------
direct insert from `meteor shell` | 2179ms | 1520ms
a method called from a browser    | 1617ms | 1570ms
a method called from the server   | 1491ms | 1487ms
direct insert from the server     | 2272ms | 1445ms

(The benchmark can be found here:
f32ea073b7/benchmark2.sh)
2015-09-20 13:13:35 -07:00
Emily Stark
cc667a487f code review comments from glasser, nim 2014-04-17 16:12:24 -07:00
Emily Stark
e544a8e03a Increase length of security-critical random tokens. Add Random.secret. 2014-04-17 16:12:16 -07:00
Justin SB
f2a2b2eb74 Revert createRandomGenerator function creation; no longer needed 2014-03-20 17:05:04 -07:00
Justin SB
a6e9ae392b Rename create -> createWithSeeds, require parameters 2014-03-20 17:05:04 -07:00
Justin Santa Barbara
64f0c73b6b Refactor Random.create() to be consistent with the global Random creation 2014-03-20 17:05:04 -07:00
Emily Stark
d564ab4b99 Fix SeededRandom's use of Random's alea.
Thanks glasser
2013-11-21 10:14:03 -08:00
Emily Stark
2413a8d3ed Use cryptographic PRNGs when available.
This means node's crypto.randomBytes on the server, and
window.crypto.getRandomValues on the client. If node's crypto.randomBytes throws
an exception, we fall back to crypto.pseudoRandomBytes. If
window.crypto.getRandomValues isn't supported by the browser, we fall back to
the alea generator that we had been using previously.
2013-09-25 15:53:04 -07:00
David Glasser
9f38258b54 Drop all @export lines. Add api.exportSymbol instead. 2013-07-25 18:54:40 -07:00
David Glasser
8a7b1ddf35 Merge branch 'devel' into linker.
Conflicts:
	meteor
	packages/absolute-url/.gitignore
	packages/accounts-base/package.js
	packages/accounts-oauth/package.js
	packages/audit-argument-checks/.gitignore
	packages/coffeescript/.gitignore
	packages/coffeescript/package.js
	packages/localstorage-polyfill/.gitignore
	packages/oauth1/package.js
	packages/oauth2/package.js
	packages/random/random.js
	scripts/generate-dev-bundle.sh
	tools/packages.js
	tools/run.js
	tools/server/server.js
2013-05-28 15:10:48 -07:00
Andrew Wilcox
1ad813951b Allow new Random instances to be constructed with specified seed.
For repeatable unit test failures with "random" data it's useful to be
able to create deterministic random number sequences.

Introduce `Random.create(seed...)` which returns a object with the
`Random` API (`id()`, `choice()`, etc.) initialized with the passed
seed(s).
2013-05-22 21:34:39 -07:00
David Glasser
6b48e8bd8a stuff that doesn't have conflicts 2013-05-13 11:51:50 -07:00
David Glasser
5a5204e3a4 Remove closures around package files. (hint: git blame -w)
The bundler now adds closures around client files and the server adds closures
around server files.
2013-03-19 15:13:53 -07:00
David Glasser
516f4dfaf7 Move Meteor.uuid to past package. Add Random.hexString. 2013-02-13 00:42:05 -08:00
David Glasser
b3d1fe4112 move objectid generation back into objectid class 2013-02-13 00:10:26 -08:00
David Glasser
9b486b3c9b Meteor.random -> Random.fraction
add Random.choice
2013-02-13 00:08:50 -08:00
David Glasser
d2ae5f7e1c Replace almost all uses of Meteor.uuid with Random.id. 2013-02-12 23:55:19 -08:00
David Glasser
ed36db211d Use Random.id() in Spark.
We couldn't use Meteor.uuid() because it contained '-' but Random.id() is fine.
2013-02-12 23:51:34 -08:00
David Glasser
e95bcb870c Get rid of duplicate random code inside minimongo.
Temporarily move the objectid-specific function into random.js.
2013-02-12 23:49:43 -08:00
David Glasser
eb14cfceeb Rename Meteor.id() -> Random.id() 2013-02-12 23:44:04 -08:00
David Glasser
fdb1773d4a Rename uuid package to random. No further changes. 2013-02-12 23:38:22 -08:00