Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JwtSigner.java
Go to the documentation of this file.
1package dev.rafex.ether.jwt.internal;
2
3import java.nio.charset.StandardCharsets;
4import java.security.MessageDigest;
5import java.security.Signature;
6import java.util.Base64;
7
8import javax.crypto.Mac;
9import javax.crypto.spec.SecretKeySpec;
10
11/*-
12 * #%L
13 * ether-jwt
14 * %%
15 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
16 * %%
17 * Permission is hereby granted, free of charge, to any person obtaining a copy
18 * of this software and associated documentation files (the "Software"), to deal
19 * in the Software without restriction, including without limitation the rights
20 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 * copies of the Software, and to permit persons to whom the Software is
22 * furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 * THE SOFTWARE.
34 * #L%
35 */
36
37import dev.rafex.ether.jwt.JwtAlgorithm;
38import dev.rafex.ether.jwt.JwtConfig;
39import dev.rafex.ether.jwt.KeyProvider;
40
41public final class JwtSigner {
42
43 private JwtSigner() {
44 }
45
46 public static String sign(final String signingInput, final JwtConfig config) {
47 try {
48 final KeyProvider keyProvider = config.keyProvider();
49 if (keyProvider.algorithm() == JwtAlgorithm.HS256) {
50 final byte[] signature = signHmac(signingInput.getBytes(StandardCharsets.UTF_8),
51 keyProvider.hmacSecret());
52 return Base64.getUrlEncoder().withoutPadding().encodeToString(signature);
53 }
54 final Signature rsa = Signature.getInstance("SHA256withRSA");
55 rsa.initSign(keyProvider.privateKey());
56 rsa.update(signingInput.getBytes(StandardCharsets.UTF_8));
57 return Base64.getUrlEncoder().withoutPadding().encodeToString(rsa.sign());
58 } catch (final Exception e) {
59 throw new IllegalStateException("error while signing token", e);
60 }
61 }
62
63 public static boolean verify(final String signingInput, final String encodedSignature, final JwtConfig config) {
64 try {
65 final KeyProvider keyProvider = config.keyProvider();
66 if (keyProvider.algorithm() == JwtAlgorithm.HS256) {
67 final byte[] expected = signHmac(signingInput.getBytes(StandardCharsets.UTF_8),
68 keyProvider.hmacSecret());
69 final byte[] provided = Base64.getUrlDecoder().decode(encodedSignature);
70 return MessageDigest.isEqual(expected, provided);
71 }
72 final Signature rsa = Signature.getInstance("SHA256withRSA");
73 rsa.initVerify(keyProvider.publicKey());
74 rsa.update(signingInput.getBytes(StandardCharsets.UTF_8));
75 return rsa.verify(Base64.getUrlDecoder().decode(encodedSignature));
76 } catch (final Exception e) {
77 return false;
78 }
79 }
80
81 private static byte[] signHmac(final byte[] data, final byte[] secret) throws Exception {
82 final Mac mac = Mac.getInstance("HmacSHA256");
83 mac.init(new SecretKeySpec(secret, "HmacSHA256"));
84 return mac.doFinal(data);
85 }
86}
Configures JWT signing and verification behavior.
static boolean verify(final String signingInput, final String encodedSignature, final JwtConfig config)
static String sign(final String signingInput, final JwtConfig config)
Supported JWT signature algorithms.
Provides cryptographic material used to sign and verify JWT tokens.
Typed configuration entry points and composition APIs for Ether applications.