Skip to content

Runtime: rely on crypto.getRandomValues when available #1209

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

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Lib: add PerformanceObserver API (#1164)
* Lib: add CSSStyleDeclaration.{setProperty, getPropertyValue, getPropertyPriority, removeProperty} (#1170)
* PPX: json can now be derived for mutable records (#1184)
* Runtime: use crypto.getRandomValues when available (#1209)

## Bug fixes
* Compiler: fix sourcemap warning for empty cma (#1169)
Expand Down
13 changes: 13 additions & 0 deletions runtime/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ function caml_sys_time_include_children(b) {
//Provides: caml_sys_random_seed mutable
//The function needs to return an array since OCaml 4.0...
function caml_sys_random_seed () {
if(globalThis.crypto) {
if(typeof globalThis.crypto.getRandomValues === 'function'){
// Webbrowsers
var a = new globalThis.Uint32Array(1);
globalThis.crypto.getRandomValues(a);
return [0,a[0]];
} else if(globalThis.crypto.randomBytes === 'function'){
// Nodejs
var buff = globalThis.crypto.randomBytes(4);
var a = new globalThis.Uint32Array(buff);
return [0,a[0]];
}
}
var now = (new Date()).getTime();
var x = now^0xffffffff*Math.random();
return [0,x];
Expand Down