-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeracaoSegura.java
More file actions
52 lines (42 loc) · 2.07 KB
/
Copy pathGeracaoSegura.java
File metadata and controls
52 lines (42 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.security.DrbgParameters;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.UUID;
import static java.security.DrbgParameters.Capability.RESEED_ONLY;
/**
* Como gerar valores seguros com SecureRandom.
* Rode com Java 17+ : java src/GeracaoSegura.java
*/
public class GeracaoSegura {
// UMA instancia, semeada uma vez, reutilizada (thread-safe).
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
public static void main(String[] args) throws NoSuchAlgorithmException {
// 1) Algoritmo padrao depende do provider/SO (em Linux, normalmente NativePRNG).
System.out.println("algoritmo padrao = " + SECURE_RANDOM.getAlgorithm());
// 2) Token de 128 bits em hexadecimal (32 chars).
System.out.println("codigo (hex) = " + gerarCodigoHex());
// 3) Mesmos 128 bits em Base64 URL-safe (22 chars), bom para URLs.
System.out.println("codigo (base64) = " + gerarCodigoBase64Url());
// 4) UUID v4 ja usa SecureRandom internamente (122 bits efetivos).
System.out.println("uuid v4 = " + UUID.randomUUID());
// 5) DRBG (NIST SP 800-90A) com forca de 128 bits, quando ha requisito de conformidade.
SecureRandom drbg = SecureRandom.getInstance(
"DRBG", DrbgParameters.instantiation(128, RESEED_ONLY, null));
System.out.println("algoritmo drbg = " + drbg.getAlgorithm());
}
/** 16 bytes aleatorios -> 32 hex chars (128 bits de entropia). */
static String gerarCodigoHex() {
byte[] bytes = new byte[16];
SECURE_RANDOM.nextBytes(bytes);
StringBuilder sb = new StringBuilder(32);
for (byte b : bytes) sb.append(String.format("%02X", b));
return sb.toString();
}
/** Mesma entropia, codificacao mais compacta e segura para URL. */
static String gerarCodigoBase64Url() {
byte[] bytes = new byte[16];
SECURE_RANDOM.nextBytes(bytes);
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
}
}