@@ -13,7 +13,7 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1313
1414// Maximum buffer size allowed in `Crypto.getRandomValuesSize` is 65536 bytes.
1515// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
16- const MAX_BUFFER_SIZE : u16 = 256 ;
16+ const MAX_BUFFER_SIZE : u32 = 65536 ;
1717
1818pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
1919 CRYPTO . with ( |crypto| {
@@ -24,7 +24,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2424
2525#[ cfg( not( target_feature = "atomics" ) ) ]
2626fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
27- for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
27+ for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE as usize ) {
2828 if crypto. get_random_values ( chunk) . is_err ( ) {
2929 return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
3030 }
@@ -36,15 +36,19 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3636fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
3737 // getRandomValues does not work with all types of WASM memory,
3838 // so we initially write to browser memory to avoid exceptions.
39- let buf = Uint8Array :: new_with_length ( MAX_BUFFER_SIZE . into ( ) ) ;
40- for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
39+ let buf_len = u32:: min (
40+ dest. len ( ) . try_into ( ) . unwrap_or ( MAX_BUFFER_SIZE ) ,
41+ MAX_BUFFER_SIZE ,
42+ ) ;
43+ let buf = Uint8Array :: new_with_length ( buf_len) ;
44+ for chunk in dest. chunks_mut ( buf_len as usize ) {
4145 let chunk_len: u32 = chunk
4246 . len ( )
4347 . try_into ( )
4448 . expect ( "chunk length is bounded by MAX_BUFFER_SIZE" ) ;
4549 // The chunk can be smaller than buf's length, so we call to
4650 // JS to create a smaller view of buf without allocation.
47- let sub_buf = if chunk_len == u32 :: from ( MAX_BUFFER_SIZE ) {
51+ let sub_buf = if chunk_len == buf_len {
4852 & buf
4953 } else {
5054 & buf. subarray ( 0 , chunk_len)
0 commit comments