-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMACService.java
More file actions
32 lines (29 loc) · 1.35 KB
/
HMACService.java
File metadata and controls
32 lines (29 loc) · 1.35 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
package ftn.security.minikms.service;
import ftn.security.minikms.entity.KeyMaterial;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class HMACService implements ICryptoService {
public KeyMaterial generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");
keyGenerator.init(256, SecureRandom.getInstanceStrong());
var key = keyGenerator.generateKey();
return KeyMaterial.of(key);
}
public String computeHmac(String message, KeyMaterial key) throws Exception {
Mac mac = Mac.getInstance("HmacSHA512");
mac.init(new SecretKeySpec(key.getKey(),"HmacSHA512"));
byte[] hmacBytes = mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hmacBytes);
}
public boolean verifyHmac(String message, String hmacBase64, KeyMaterial key) throws Exception {
String computedHmac = computeHmac(message, key);
return MessageDigest.isEqual(computedHmac.getBytes(StandardCharsets.UTF_8),
hmacBase64.getBytes(StandardCharsets.UTF_8));
}
}