Skip to content

Commit

Permalink
refactor(api): use secure RNG, closes #1356 (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 30, 2021
1 parent 4e9d31c commit c8992bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changes/secure-rng.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Use secure RNG on callback function names.
25 changes: 5 additions & 20 deletions api/src/tauri.ts
Expand Up @@ -7,27 +7,12 @@ declare global {
}
}

function s4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}

function uid(): string {
return (
s4() +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
s4() +
s4()
)
const length = new Int8Array(1)
window.crypto.getRandomValues(length)
const array = new Uint8Array(Math.max(16, Math.abs(length[0])))
window.crypto.getRandomValues(array)
return array.join('')
}

function transformCallback(
Expand Down
2 changes: 1 addition & 1 deletion tauri/scripts/bundle.js

Large diffs are not rendered by default.

27 changes: 6 additions & 21 deletions tauri/scripts/core.js
Expand Up @@ -7,29 +7,14 @@ if (!String.prototype.startsWith) {
}

(function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
function uid() {
const length = new Int8Array(1)
window.crypto.getRandomValues(length)
const array = new Uint8Array(Math.max(16, Math.abs(length[0])))
window.crypto.getRandomValues(array)
return array.join('')
}

var uid = function () {
return (
s4() +
s4() +
"-" +
s4() +
"-" +
s4() +
"-" +
s4() +
"-" +
s4() +
s4() +
s4()
);
};

function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
Expand Down

0 comments on commit c8992bb

Please sign in to comment.