Skip to content
Snippets Groups Projects
Commit 7c225b03 authored by Henry Borasch's avatar Henry Borasch
Browse files

Merge remote-tracking branch 'origin/658-collect_crypto_constants'

parents 0ec50f1c 221280fe
No related branches found
No related tags found
1 merge request!119moved crypto constants to central class
package dev.fitko.fitconnect.core.crypto;
import com.nimbusds.jose.CompressionAlgorithm;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JWEAlgorithm;
public class CryptoConstants {
public final static String DEFAULT_HASH_ALGORITHM = HashAlgorithm.SHA_512.getIdentifyer();
public final static String DEFAULT_SYMMETRIC_ENCRYPTION_ALGORITHM = SymmetricEncryptionAlgorithm.AES.getIdentifyer();
public final static String DEFAULT_HMAC_ALGORITHM = HmacAlgorithm.HMAC_SHA_512.getIdentifyer();
public final static EncryptionMethod DEFAULT_JWE_ENCRYPTION_METHOD = EncryptionMethod.A256GCM;
public static final JWEAlgorithm DEFAULT_JWE_ALGORITHM = JWEAlgorithm.RSA_OAEP_256;
public static final CompressionAlgorithm DEFAULT_JWE_COMPRESSION_ALGORITHM = CompressionAlgorithm.DEF;
}
package dev.fitko.fitconnect.core.crypto;
public enum HashAlgorithm {
SHA_512("SHA-512");
private final String identifyer;
HashAlgorithm(String identifyer) {
this.identifyer = identifyer;
}
public String getIdentifyer() {
return identifyer;
}
}
......@@ -13,10 +13,10 @@ import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashService implements MessageDigestService {
import static dev.fitko.fitconnect.core.crypto.CryptoConstants.DEFAULT_HASH_ALGORITHM;
import static dev.fitko.fitconnect.core.crypto.CryptoConstants.DEFAULT_HMAC_ALGORITHM;
static final String DEFAULT_ALGORITHM = "SHA-512"; // Currently, only SHA-512 is supported.
private static final String HMAC_SHA512 = "HmacSHA512";
public class HashService implements MessageDigestService {
private static final Logger LOGGER = LoggerFactory.getLogger(HashService.class);
......@@ -24,7 +24,7 @@ public class HashService implements MessageDigestService {
public HashService() {
try {
this.messageDigest = MessageDigest.getInstance(DEFAULT_ALGORITHM);
this.messageDigest = MessageDigest.getInstance(DEFAULT_HASH_ALGORITHM);
} catch (final NoSuchAlgorithmException e) {
throw new InitializationException(e.getMessage(), e);
}
......@@ -76,8 +76,9 @@ public class HashService implements MessageDigestService {
public String calculateHMAC(String data, String key) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
Mac mac = Mac.getInstance(HMAC_SHA512);
String hmacAlgorithm = DEFAULT_HMAC_ALGORITHM;
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), hmacAlgorithm);
Mac mac = Mac.getInstance(hmacAlgorithm);
mac.init(secretKeySpec);
return toHexString(mac.doFinal(data.getBytes()));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
......
package dev.fitko.fitconnect.core.crypto;
public enum HmacAlgorithm {
HMAC_SHA_512("HmacSHA512");
private final String identifyer;
HmacAlgorithm(String identifyer) {
this.identifyer = identifyer;
}
public String getIdentifyer() {
return identifyer;
}
}
......@@ -10,22 +10,20 @@ import dev.fitko.fitconnect.api.exceptions.DecryptionException;
import dev.fitko.fitconnect.api.exceptions.EncryptionException;
import dev.fitko.fitconnect.api.services.crypto.CryptoService;
import dev.fitko.fitconnect.api.services.crypto.MessageDigestService;
import dev.fitko.fitconnect.core.util.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dev.fitko.fitconnect.core.util.StopWatch;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import static dev.fitko.fitconnect.core.crypto.CryptoConstants.*;
public class JWECryptoService implements CryptoService {
private static final Logger LOGGER = LoggerFactory.getLogger(JWECryptoService.class);
private static final JWEAlgorithm ALGORITHM = JWEAlgorithm.RSA_OAEP_256;
private static final EncryptionMethod ENCRYPTION_METHOD = EncryptionMethod.A256GCM;
private static final ObjectMapper MAPPER = new ObjectMapper();
private final MessageDigestService messageDigestService;
......@@ -74,8 +72,8 @@ public class JWECryptoService implements CryptoService {
private String encrypt(final RSAKey publicKey, final Payload payload) throws EncryptionException {
try {
final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(ENCRYPTION_METHOD.cekBitLength());
final KeyGenerator keyGenerator = KeyGenerator.getInstance(DEFAULT_SYMMETRIC_ENCRYPTION_ALGORITHM);
keyGenerator.init(DEFAULT_JWE_ENCRYPTION_METHOD.cekBitLength());
final SecretKey cek = keyGenerator.generateKey();
final String keyID = getIdFromPublicKey(publicKey);
return encryptPayload(publicKey, payload, cek, keyID);
......@@ -101,8 +99,8 @@ public class JWECryptoService implements CryptoService {
}
private JWEHeader getJWEHeader(final String keyID) {
return new JWEHeader.Builder(ALGORITHM, ENCRYPTION_METHOD)
.compressionAlgorithm(CompressionAlgorithm.DEF)
return new JWEHeader.Builder(DEFAULT_JWE_ALGORITHM, DEFAULT_JWE_ENCRYPTION_METHOD)
.compressionAlgorithm(DEFAULT_JWE_COMPRESSION_ALGORITHM)
.contentType("application/json")
.keyID(keyID)
.build();
......
package dev.fitko.fitconnect.core.crypto;
public enum SymmetricEncryptionAlgorithm {
AES("AES");
private final String identifyer;
SymmetricEncryptionAlgorithm(String identifyer) {
this.identifyer = identifyer;
}
public String getIdentifyer() {
return identifyer;
}
}
......@@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static dev.fitko.fitconnect.core.crypto.CryptoConstants.DEFAULT_HASH_ALGORITHM;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -20,7 +21,7 @@ class HashServiceTest {
@BeforeEach
public void setup() throws NoSuchAlgorithmException {
final MessageDigest SHA_512 = MessageDigest.getInstance(HashService.DEFAULT_ALGORITHM);
final MessageDigest SHA_512 = MessageDigest.getInstance(DEFAULT_HASH_ALGORITHM);
underTest = new HashService(SHA_512);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment