- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: kotlin-security/no-pseudo-random
Language: Kotlin
Severity: Error
Category: Security
CWE: 330
This rule enforces the use of secure and unpredictable random numbers in Kotlin applications. Using pseudo-random numbers can make your code vulnerable to attacks because pseudo-random numbers follow a deterministic sequence that can be predicted if the initial seed is known. This is especially crucial in contexts such as generating encryption keys, generating random identifiers, or performing any other security-related functionalities.
To adhere to this rule, avoid using SecureRandom
with a fixed seed using the setSeed()
method or passing a byte array to the SecureRandom
constructor. Both of these methods produce pseudo-random numbers, which can lead to vulnerabilities in your code. Also, avoid reseeding a SecureRandom
instance with a predictable value, such as the current time.
Instead, create a SecureRandom
instance without a set seed, or use SecureRandom.getInstanceStrong()
. Following these best practices helps you generate secure and unpredictable random numbers in your Kotlin applications.
import java.security.SecureRandom
// Setting a fixed numeric seed
val random1 = SecureRandom()
random1.setSeed(123456L) // Noncompliant
// Setting a fixed string seed
val random2 = SecureRandom("myseed".toByteArray()) // Noncompliant
// Reseeding with predictable value
val random3 = SecureRandom()
val time = System.currentTimeMillis()
random3.setSeed(time) // Noncompliant: timestamp is predictable
import java.security.SecureRandom
// Let SecureRandom choose its own seed
val random1 = SecureRandom()
val bytes = random1.nextBytes(32)
// Use strong instance (preferred)
val random2 = SecureRandom.getInstanceStrong()
val number = random2.nextInt()