Skip to content

Commit

Permalink
fix/binary to hex (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-trzewik authored Jun 29, 2021
1 parent fb3edac commit d257963
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<bcpkix.version>1.68</bcpkix.version>
<okhttp.version>4.9.1</okhttp.version>
<shedlock.version>4.23.0</shedlock.version>
<dgc.lib.version>1.1.1</dgc.lib.version>
<dgc.lib.version>1.1.2</dgc.lib.version>
<!-- plugins -->
<plugin.checkstyle.version>3.1.2</plugin.checkstyle.version>
<plugin.sonar.version>3.6.1.1688</plugin.sonar.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
Expand All @@ -21,15 +20,13 @@
import eu.europa.ec.dgc.gateway.connector.dto.ValidationRuleDto;
import eu.europa.ec.dgc.gateway.connector.model.ValidationRule;
import eu.europa.ec.dgc.signing.SignedStringMessageParser;
import eu.europa.ec.dgc.utils.CertificateUtils;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -65,6 +62,7 @@ public class GatewayDataDownloadBtpServiceImpl implements GatewayDataDownloadSer
private final BusinessRuleService businessRuleService;
private final ValueSetService valueSetService;
private final CountryListService countryListService;
private final CertificateUtils certificateUtils;

@Override
@Scheduled(fixedDelayString = "${dgc.businessRulesDownload.timeInterval}")
Expand Down Expand Up @@ -267,12 +265,7 @@ private boolean checkThumbprintIntegrity(TrustListItemDto trustListItem) {
private String getCertThumbprint(X509CertificateHolder x509CertificateHolder) {
try {
byte[] data = x509CertificateHolder.getEncoded();
byte[] certHashBytes = MessageDigest.getInstance("SHA-256").digest(data);
String hexString = (new BigInteger(1, certHashBytes)).toString(16);
if (hexString.length() == 63) {
hexString = "0" + hexString;
}
return hexString;
return certificateUtils.calculateHash(data);
} catch (NoSuchAlgorithmException | IOException e) {
log.error("Could not calculate thumbprint of certificate '{}': {}.",
x509CertificateHolder.getSubject(), e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,27 @@

package eu.europa.ec.dgc.businessrule.utils;

import eu.europa.ec.dgc.utils.CertificateUtils;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class BusinessRulesUtils {
private final CertificateUtils certificateUtils;

/**
* returns SHA-256 Thumbprint of the data (hex encoded).
*/
public String calculateHash(String data) throws NoSuchAlgorithmException {
return calculateHash(data.getBytes(StandardCharsets.UTF_8));
return certificateUtils.calculateHash(data.getBytes(StandardCharsets.UTF_8));
}

/**
* returns SHA-256 Thumbprint of the data (hex encoded).
*/
public String calculateHash(byte[] data) throws NoSuchAlgorithmException {
byte[] certHashBytes = MessageDigest.getInstance("SHA-256").digest(data);
String hexString = new BigInteger(1, certHashBytes).toString(16);

if (hexString.length() == 63) {
hexString = "0" + hexString;
}

return hexString;
}
}

0 comments on commit d257963

Please sign in to comment.