File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
src/2024/SpeakersCarousel Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,29 @@ const StyledSlideText = styled.p`
3838const SpeakerSwiper : FC < React . PropsWithChildren < unknown > > = ( ) => {
3939 const { isLoading, data, error} = useFetchSpeakers ( ) ;
4040
41- const swiperSpeakers = data ?. sort ( ( ) => 0.5 - Math . random ( ) ) . slice ( 0 , 20 ) ;
41+ // Securely shuffle the speakers using Fisher-Yates algorithm with crypto API
42+ const swiperSpeakers = React . useMemo ( ( ) => {
43+ if ( ! data ) return null ;
44+
45+ // Create a copy of the data to avoid mutating the original
46+ const speakersCopy = [ ...data ] ;
47+
48+ // Fisher-Yates shuffle with crypto.getRandomValues for secure randomization
49+ for ( let i = speakersCopy . length - 1 ; i > 0 ; i -- ) {
50+ // Generate a secure random value using crypto API
51+ const randomBuffer = new Uint32Array ( 1 ) ;
52+ window . crypto . getRandomValues ( randomBuffer ) ;
53+
54+ // Use the random value to get an index between 0 and i (inclusive)
55+ const j = randomBuffer [ 0 ] % ( i + 1 ) ;
56+
57+ // Swap elements at i and j
58+ [ speakersCopy [ i ] , speakersCopy [ j ] ] = [ speakersCopy [ j ] , speakersCopy [ i ] ] ;
59+ }
60+
61+ // Return the first 20 speakers from the shuffled array
62+ return speakersCopy . slice ( 0 , 20 ) ;
63+ } , [ data ] ) ;
4264
4365 if ( error ) {
4466 Sentry . captureException ( error ) ;
You can’t perform that action at this time.
0 commit comments