Do not use a pseudo-random number to generate a secret

이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: java-security/no-pseudo-random-secret

Language: Java

Severity: Warning

Category: Security

CWE: 338

Description

Never use the Random class to generate secrets. Instead, use the SecureRandom class.

Learn More

Non-Compliant Code Examples

class MyClass{
    public String generateSecretToken() {
        Random r = new Random();
        return Long.toHexString(r.nextLong());
    }
}

Compliant Code Examples

import org.apache.commons.codec.binary.Hex;

class Class {
    String generateSecretToken() {
        SecureRandom secRandom = new SecureRandom();

        byte[] result = new byte[32];
        secRandom.nextBytes(result);
        return Hex.encodeHexString(result);
    }
}
PREVIEWING: aliciascott/DOCS-9725-Cloudcraft