-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c836f5a
commit ee9dd81
Showing
10 changed files
with
49 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { isSharedArrayBuffer, localHeapViewU8 } from "./memory"; | ||
|
||
// batchedQuotaMax is the max number of bytes as specified by the api spec. | ||
// If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. | ||
// https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues | ||
const batchedQuotaMax = 65536; | ||
|
||
export function mono_wasm_browser_entropy(bufferPtr: number, bufferLength: number): number { | ||
if (!globalThis.crypto || !globalThis.crypto.getRandomValues) { | ||
return -1; | ||
} | ||
|
||
const memoryView = localHeapViewU8(); | ||
const targetView = memoryView.subarray(bufferPtr, bufferPtr + bufferLength); | ||
|
||
// When threading is enabled, Chrome doesn't want SharedArrayBuffer to be passed to crypto APIs | ||
const needsCopy = isSharedArrayBuffer(memoryView.buffer); | ||
const targetBuffer = needsCopy | ||
? new Uint8Array(bufferLength) | ||
: targetView; | ||
|
||
// fill the targetBuffer in batches of batchedQuotaMax | ||
for (let i = 0; i < bufferLength; i += batchedQuotaMax) { | ||
const targetBatch = targetBuffer.subarray(i, i + Math.min(bufferLength - i, batchedQuotaMax)); | ||
globalThis.crypto.getRandomValues(targetBatch); | ||
} | ||
|
||
if (needsCopy) { | ||
targetView.set(targetBuffer); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters