[Android] Use SecureRandom instead of Random
Robert Stone
git at videolan.org
Thu Aug 5 06:31:28 UTC 2021
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Jul 21 20:39:15 2021 -0700| [c547ab5a9a8547c973bdd1514b00d2068e630048] | committer: Nicolas Pomepuy
Use SecureRandom instead of Random
> https://code.videolan.org/videolan/vlc-android/commit/c547ab5a9a8547c973bdd1514b00d2068e630048
---
application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt | 3 ++-
.../vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt | 3 ++-
application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt | 9 +++++----
.../vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt | 5 ++---
application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt | 3 ++-
5 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt b/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
index 043af6646..cdfb1c060 100644
--- a/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/ArtworkProvider.kt
@@ -47,6 +47,7 @@ import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.nio.ByteBuffer
+import java.security.SecureRandom
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*
@@ -253,7 +254,7 @@ class ArtworkProvider : ContentProvider() {
/* Shuffle All */
val audioCount = ctx.getFromMl { audioCount }
/* Show cover art from the whole library */
- val offset = Random().nextInt((audioCount - MediaSessionBrowser.MAX_COVER_ART_ITEMS).coerceAtLeast(1))
+ val offset = SecureRandom().nextInt((audioCount - MediaSessionBrowser.MAX_COVER_ART_ITEMS).coerceAtLeast(1))
val list = ctx.getFromMl { getPagedAudio(Medialibrary.SORT_ALPHA, false, false, MediaSessionBrowser.MAX_COVER_ART_ITEMS, offset) }
return at runBlocking getHomeImage(ctx, SHUFFLE_ALL, list)
}
diff --git a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 1c69856b4..5ed8bfc75 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -25,6 +25,7 @@ import org.videolan.vlc.gui.helpers.MediaComparators
import org.videolan.vlc.media.MediaSessionBrowser
import org.videolan.vlc.util.VoiceSearchParams
import org.videolan.vlc.util.awaitMedialibraryStarted
+import java.security.SecureRandom
import java.util.*
import kotlin.math.min
@@ -136,7 +137,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
val tracks = context.getFromMl { audio }
if (tracks.isNotEmpty() && isActive) {
tracks.sortWith(MediaComparators.ANDROID_AUTO)
- loadMedia(tracks.toList(), Random().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
+ loadMedia(tracks.toList(), SecureRandom().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
if (!playbackService.isShuffling) playbackService.shuffle()
} else {
playbackService.displayPlaybackError(R.string.search_no_result)
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index 0c4550350..1db1bb856 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -46,6 +46,7 @@ import org.videolan.vlc.util.Permissions
import org.videolan.vlc.util.generateResolutionClass
import org.videolan.vlc.util.isSchemeStreaming
import java.io.File
+import java.security.SecureRandom
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.min
@@ -199,7 +200,7 @@ object MediaUtils {
}
}
}?.takeIf { it.isNotEmpty() }?.let { list ->
- service.load(list, if (shuffle) Random().nextInt(count) else position)
+ service.load(list, if (shuffle) SecureRandom().nextInt(count) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
}
@@ -210,7 +211,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
- service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
+ service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
@@ -237,7 +238,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
- service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
+ service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
@@ -268,7 +269,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
- service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
+ service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 0b387e395..a5b8531e2 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -29,6 +29,7 @@ import org.videolan.vlc.R
import org.videolan.vlc.gui.video.VideoPlayerActivity
import org.videolan.vlc.util.*
import org.videolan.vlc.util.FileUtils
+import java.security.SecureRandom
import java.util.*
import kotlin.math.max
@@ -64,7 +65,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var isHardware = false
private var parsed = false
var savedTime = 0L
- private var random = Random(System.currentTimeMillis())
+ private var random = SecureRandom()
private var newMedia = false
@Volatile
private var expanding = false
@@ -611,10 +612,8 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
return
} else {
previous.clear()
- random = Random(System.currentTimeMillis())
}
}
- random = Random(System.currentTimeMillis())
// Find a new index not in previous.
do {
nextIndex = random.nextInt(size)
diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 6df9c59e3..118f894bf 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -48,6 +48,7 @@ import org.videolan.vlc.R
import java.io.File
import java.net.URI
import java.net.URISyntaxException
+import java.security.SecureRandom
import java.util.*
fun String.validateLocation(): Boolean {
@@ -117,7 +118,7 @@ fun MediaWrapper?.isBrowserMedia() = this != null && (isMedia() || type == Media
fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!!
-fun Long.random() = (Random().nextFloat() * this).toLong()
+fun Long.random() = (SecureRandom().nextFloat() * this).toLong()
suspend fun Context.awaitMedialibraryStarted() = getFromMl { isStarted }
More information about the Android
mailing list