forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMAC.java
More file actions
161 lines (135 loc) · 4.67 KB
/
HMAC.java
File metadata and controls
161 lines (135 loc) · 4.67 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package act.crypto;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.session.JWT;
import org.osgl.util.Codec;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
public class HMAC {
public enum Algorithm {
SHA256, SHA384, SHA512
;
private final String javaName;
private final String jwtName;
Algorithm() {
this.javaName = S.concat("Hmac", name());
this.jwtName = S.concat("HS", name().substring(3));
}
Mac macOf(String key) {
try {
SecretKeySpec spec = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), javaName);
Mac mac = Mac.getInstance(javaName);
mac.init(spec);
return mac;
} catch (Exception e) {
throw E.unexpected(e);
}
}
public String jwtName() {
return jwtName;
}
}
private Mac mac;
private String algoName;
protected Algorithm algo;
private final Charset UTF_8 = Charset.forName("UTF-8");
protected HMAC(String algoKey) {
algo = algoLookup.get(algoKey.toUpperCase());
E.illegalArgumentIf(null == algo, "Algorithm not found");
algoName = algo.jwtName();
}
protected HMAC(Algorithm algo) {
this.algoName = algo.jwtName();
this.algo = algo;
}
public HMAC(String key, String algoKey) {
this(algoKey);
mac = algo.macOf(key);
}
public HMAC(String key, Algorithm algo) {
this(algo);
mac = algo.macOf(key);
}
public String toString(JWT.Token token) {
token.header(JWT.Header.ALGO, algoName);
String headers = token.headerJsonString();
String payloads = token.payloadJsonString();
String encodedHeaders = encodePart(headers);
String encodedPayloads = encodePart(payloads);
StringBuilder buf = new StringBuilder(encodedHeaders)
.append(".")
.append(encodedPayloads);
String hash = hash(buf.toString());
return buf.append(".").append(hash).toString();
}
public String hash(String text) {
return hash(text.getBytes(UTF_8));
}
public String hash(byte[] bytes) {
byte[] hashed = doHash(bytes);
return encodePart(hashed);
}
protected byte[] doHash(byte[] bytes) {
return doHash(bytes, mac);
}
protected final byte[] doHash(byte[] bytes, Mac mac) {
return mac.doFinal(bytes);
}
public boolean verifyHash(String content, String hash) {
int len = hash.length();
int padding = 4 - len % 4;
if (padding > 0) {
hash = S.concat(hash, S.times(Codec.URL_SAFE_BASE64_PADDING_CHAR, padding));
}
byte[] yourHash = Codec.decodeUrlSafeBase64(hash);
return verifyHash(content.getBytes(UTF_8), yourHash);
}
protected boolean verifyHash(byte[] payload, byte[] hash) {
return verifyHash(payload, hash, mac);
}
protected final boolean verifyHash(byte[] payload, byte[] hash, Mac mac) {
byte[] myHash = doHash(payload, mac);
return MessageDigest.isEqual(myHash, hash);
}
public boolean verifyArgo(String algoName) {
Algorithm algorithm = algoLookup.get(algoName);
return null != algorithm && S.eq(this.algoName, algorithm.jwtName());
}
private static Map<String, Algorithm> algoLookup; static {
algoLookup = new HashMap<>();
for (Algorithm algo : Algorithm.values()) {
algoLookup.put(algo.javaName.toUpperCase(), algo);
algoLookup.put(algo.name().toUpperCase(), algo);
algoLookup.put(algo.jwtName().toUpperCase(), algo);
}
}
protected static String encodePart(String part) {
return Codec.encodeUrlSafeBase64(part);
}
protected static String encodePart(byte[] part) {
return Codec.encodeUrlSafeBase64(part);
}
}