/
Signature.java
63 lines (56 loc) Β· 2.34 KB
/
Signature.java
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
53
54
55
56
57
58
59
60
61
62
63
package co.airy.crypto;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Signature {
public static final String CONTENT_SIGNATURE_HEADER = "X-Airy-Content-Signature";
/**
* Computes a signature of the content send to user webhooks so that they can verify its integrity and authenticity.
*
* @param key secret key to use for computing the hmac
* @param content message body
* @return hmac (sha256) of the content given the key in lowercase hex representation
* @throws InvalidKeyException Malformed user secret key
*/
public static String getSignature(String key, String content) throws InvalidKeyException {
Mac mac;
try {
mac = Mac.getInstance("HmacSHA256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "HmacSHA256");
mac.init(secretKeySpec);
byte[] hmac = mac.doFinal(content.getBytes());
return byteToHex(hmac);
}
public static String byteToHex(byte[] payload) {
StringBuilder builder = new StringBuilder();
for (byte b : payload) {
// TODO This is slow compared to the DataTypeConverter implementation
builder.append(String.format("%02X", b).toLowerCase());
}
return builder.toString();
}
public static String getSha1(String content) {
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(content.getBytes(StandardCharsets.UTF_8));
return byteToHex(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static String getHmac(String key, String content) {
try {
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
return byteToHex(hmac.doFinal(content.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
}