From d2711135db1d78d00a42a5e5e88631717cfcfe59 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 3 Aug 2023 16:14:12 +0200 Subject: [PATCH] use SecureRamdom() --- .../nightscout/plugins/constraints/objectives/SntpClient.kt | 3 ++- .../src/main/java/info/nightscout/source/RandomBgPlugin.kt | 4 ++-- .../omnipod/dash/driver/comm/session/SessionEstablisher.kt | 3 +-- .../eros/driver/communication/action/AssignAddressAction.kt | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/SntpClient.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/SntpClient.kt index 47b8f4301d..f2d7a1d0dc 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/SntpClient.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/SntpClient.kt @@ -6,6 +6,7 @@ import info.nightscout.shared.utils.DateUtil import java.net.DatagramPacket import java.net.DatagramSocket import java.net.InetAddress +import java.security.SecureRandom import javax.inject.Inject import javax.inject.Singleton @@ -195,6 +196,6 @@ class SntpClient @Inject constructor( buffer[offset++] = (fraction shr 16).toByte() buffer[offset++] = (fraction shr 8).toByte() // low order bits should be random data - buffer[offset] = (Math.random() * 255.0).toInt().toByte() + buffer[offset] = SecureRandom().nextInt(256).toByte() } } \ No newline at end of file diff --git a/plugins/source/src/main/java/info/nightscout/source/RandomBgPlugin.kt b/plugins/source/src/main/java/info/nightscout/source/RandomBgPlugin.kt index cfee2bf8d2..a264232dee 100644 --- a/plugins/source/src/main/java/info/nightscout/source/RandomBgPlugin.kt +++ b/plugins/source/src/main/java/info/nightscout/source/RandomBgPlugin.kt @@ -22,7 +22,7 @@ import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.utils.T import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.kotlin.plusAssign -import java.lang.Math.random +import java.security.SecureRandom import java.util.Calendar import java.util.GregorianCalendar import javax.inject.Inject @@ -100,7 +100,7 @@ class RandomBgPlugin @Inject constructor( val cal = GregorianCalendar() val currentMinute = cal[Calendar.MINUTE] + (cal[Calendar.HOUR_OF_DAY] % 2) * 60 - val bgMgdl = min + ((max - min) + (max - min) * sin(currentMinute / period * 2 * PI)) / 2 + (random() - 0.5) * (max - min) * 0.4 + val bgMgdl = min + ((max - min) + (max - min) * sin(currentMinute / period * 2 * PI)) / 2 + (SecureRandom().nextDouble() - 0.5) * (max - min) * 0.4 cal[Calendar.MILLISECOND] = 0 cal[Calendar.SECOND] = 0 diff --git a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt index cf11aeb2ae..efb6c73a64 100644 --- a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt +++ b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt @@ -12,7 +12,6 @@ import info.nightscout.interfaces.Config import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag import java.security.SecureRandom -import java.util.Random class SessionEstablisher( private val aapsLogger: AAPSLogger, @@ -26,7 +25,7 @@ class SessionEstablisher( private val controllerIV = ByteArray(IV_SIZE) private var nodeIV = ByteArray(IV_SIZE) - private val identifier = Random().nextInt().toByte() + private val identifier = SecureRandom().nextInt().toByte() private val milenage = Milenage(aapsLogger, config, ltk, eapSqn) init { diff --git a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt index 46228e37b0..ecab63605e 100644 --- a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt +++ b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/driver/communication/action/AssignAddressAction.kt @@ -13,6 +13,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.manager.ErosP import info.nightscout.androidaps.plugins.pump.omnipod.eros.rileylink.manager.OmnipodRileyLinkCommunicationManager import info.nightscout.rx.logging.AAPSLogger import org.joda.time.DateTimeZone +import java.security.SecureRandom import java.util.Random class AssignAddressAction( @@ -55,7 +56,7 @@ class AssignAddressAction( companion object { - private val random: Random = Random() + private val random: Random = SecureRandom() private fun generateRandomAddress(): Int = // Create random address with 20 bits to match PDM, could easily use 24 bits instead 0x1f000000 or (random.nextInt() and 0x000fffff)