forked from mtusi2026/mtusi2026lab2
Лабораторная 2 на 5 готово
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package org.example.config;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
|
||||
public final class TokenHasher {
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
public TokenHasher() {}
|
||||
|
||||
public static String newRawToken(){
|
||||
byte[] bytes = new byte[32];
|
||||
RANDOM.nextBytes(bytes);
|
||||
return HexFormat.of().formatHex(bytes);
|
||||
}
|
||||
|
||||
public static String sha256(String rawToken) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] dig = md.digest(rawToken.getBytes(StandardCharsets.UTF_8));
|
||||
return HexFormat.of().formatHex(dig);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user