@@ -13,25 +13,18 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
13
13
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
14
14
const MAX_BUFFER_SIZE : usize = 65536 ;
15
15
16
- pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
17
- CRYPTO . with ( |crypto| {
18
- let crypto = crypto. as_ref ( ) . ok_or ( Error :: WEB_CRYPTO ) ?;
19
- inner ( crypto, dest)
20
- } )
21
- }
22
-
23
16
#[ cfg( not( target_feature = "atomics" ) ) ]
24
- fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
17
+ pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
25
18
for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE ) {
26
- if crypto . get_random_values ( chunk) . is_err ( ) {
27
- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
19
+ if get_random_values ( chunk) . is_err ( ) {
20
+ return Err ( Error :: WEB_CRYPTO ) ;
28
21
}
29
22
}
30
23
Ok ( ( ) )
31
24
}
32
25
33
26
#[ cfg( target_feature = "atomics" ) ]
34
- fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
27
+ pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
35
28
// getRandomValues does not work with all types of WASM memory,
36
29
// so we initially write to browser memory to avoid exceptions.
37
30
let buf_len = usize:: min ( dest. len ( ) , MAX_BUFFER_SIZE ) ;
@@ -52,8 +45,8 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
52
45
& buf. subarray ( 0 , chunk_len)
53
46
} ;
54
47
55
- if crypto . get_random_values ( sub_buf) . is_err ( ) {
56
- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
48
+ if get_random_values ( sub_buf) . is_err ( ) {
49
+ return Err ( Error :: WEB_CRYPTO ) ;
57
50
}
58
51
59
52
// SAFETY: `sub_buf`'s length is the same length as `chunk`
@@ -64,16 +57,11 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
64
57
65
58
#[ wasm_bindgen]
66
59
extern "C" {
67
- // Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
68
- type Crypto ;
69
- // Holds the global `Crypto` object.
70
- #[ wasm_bindgen( thread_local_v2, js_namespace = globalThis, js_name = crypto) ]
71
- static CRYPTO : Option < Crypto > ;
72
60
// Crypto.getRandomValues()
73
61
#[ cfg( not( target_feature = "atomics" ) ) ]
74
- #[ wasm_bindgen( method , js_name = getRandomValues, catch) ]
75
- fn get_random_values ( this : & Crypto , buf : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , JsValue > ;
62
+ #[ wasm_bindgen( js_namespace = [ "globalThis" , "crypto" ] , js_name = getRandomValues, catch) ]
63
+ fn get_random_values ( buf : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , JsValue > ;
76
64
#[ cfg( target_feature = "atomics" ) ]
77
- #[ wasm_bindgen( method , js_name = getRandomValues, catch) ]
78
- fn get_random_values ( this : & Crypto , buf : & js_sys:: Uint8Array ) -> Result < ( ) , JsValue > ;
65
+ #[ wasm_bindgen( js_namespace = [ "globalThis" , "crypto" ] , js_name = getRandomValues, catch) ]
66
+ fn get_random_values ( buf : & js_sys:: Uint8Array ) -> Result < ( ) , JsValue > ;
79
67
}
0 commit comments