Skip to content

Commit a9bbdfb

Browse files
committed
Changed storage of secret to use an array of bytes instead of a string.
Added new constructor to accept the array of bytes.
1 parent 5387505 commit a9bbdfb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/com/auth0/jwt/JWTSigner.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
* No support for RSA encryption at present
2727
*/
2828
public class JWTSigner {
29-
private final String secret;
29+
private final byte[] secret;
3030

3131
public JWTSigner(String secret) {
32+
this(secret.getBytes());
33+
}
34+
35+
public JWTSigner(byte[] secret) {
3236
this.secret = secret;
3337
}
3438

@@ -216,7 +220,7 @@ private String base64UrlEncode(byte[] str) {
216220
/**
217221
* Switch the signing algorithm based on input, RSA not supported
218222
*/
219-
private static byte[] sign(Algorithm algorithm, String msg, String secret) throws Exception {
223+
private static byte[] sign(Algorithm algorithm, String msg, byte[] secret) throws Exception {
220224
switch (algorithm) {
221225
case HS256:
222226
case HS384:
@@ -233,9 +237,9 @@ private static byte[] sign(Algorithm algorithm, String msg, String secret) throw
233237
/**
234238
* Sign an input string using HMAC and return the encrypted bytes
235239
*/
236-
private static byte[] signHmac(Algorithm algorithm, String msg, String secret) throws Exception {
240+
private static byte[] signHmac(Algorithm algorithm, String msg, byte[] secret) throws Exception {
237241
Mac mac = Mac.getInstance(algorithm.getValue());
238-
mac.init(new SecretKeySpec(secret.getBytes(), algorithm.getValue()));
242+
mac.init(new SecretKeySpec(secret, algorithm.getValue()));
239243
return mac.doFinal(msg.getBytes());
240244
}
241245

0 commit comments

Comments
 (0)