Skip to content

Commit

Permalink
Merge pull request #1482 from OSGP/feature/hex-conversion-loosing-lea…
Browse files Browse the repository at this point in the history
…ding-zeros

hex-conversion-loosing-leading-zeros
  • Loading branch information
harrymiddelburgetall authored Dec 11, 2024
2 parents bc05b65 + bd564b8 commit 5ff79fd
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import static org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString;
import static org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getStringList;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import org.apache.commons.codec.binary.Hex;
import org.opensmartgridplatform.adapter.ws.schema.core.firmwaremanagement.ChangeableFirmware;
import org.opensmartgridplatform.adapter.ws.schema.core.firmwaremanagement.DeviceModel;
import org.opensmartgridplatform.adapter.ws.schema.core.firmwaremanagement.Firmware;
Expand Down Expand Up @@ -227,7 +227,7 @@ private String calculateHash(final String hashType, final byte[] firmwareFile)
throws NoSuchAlgorithmException {
final MessageDigest messageDigest = MessageDigest.getInstance(hashType);
final byte[] digest = messageDigest.digest(firmwareFile);
return new BigInteger(1, digest).toString(16);
return Hex.encodeHexString(digest);
}

protected void assertFirmwareFileHasModuleVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -21,6 +20,7 @@
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.junit.jupiter.api.Assertions;
import org.opensmartgridplatform.adapter.ws.schema.smartmetering.common.OsgpResultType;
import org.opensmartgridplatform.adapter.ws.schema.smartmetering.configuration.UpdateFirmwareAsyncRequest;
Expand Down Expand Up @@ -188,7 +188,7 @@ private String calcHash(final byte[] filecontent, final String algorithm) {
try {
final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
final byte[] digest = messageDigest.digest(filecontent);
return new BigInteger(1, digest).toString(16);
return Hex.encodeHexString(digest);
} catch (final NoSuchAlgorithmException e) {
log.error("Error calculating digest", e);
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
package org.opensmartgridplatform.adapter.ws.core.application.services;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.opensmartgridplatform.shared.exceptionhandling.ComponentType;
import org.opensmartgridplatform.shared.exceptionhandling.TechnicalException;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -112,7 +112,7 @@ public String createDigest(final String hashType, final String firmwareIdentific
final MessageDigest messageDigest = MessageDigest.getInstance(hashType);
final byte[] firmwareFile = this.readFirmwareFile(firmwareIdentification);

digest = new BigInteger(1, messageDigest.digest(firmwareFile)).toString(16);
digest = Hex.encodeHexString(messageDigest.digest(firmwareFile));
}
} catch (final NoSuchAlgorithmException e) {
throw new TechnicalException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package org.opensmartgridplatform.adapter.protocol.dlms.application.services;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.firmware.GetFirmwareVersionsCommandExecutor;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.firmware.UpdateFirmwareCommandExecutor;
import org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.firmware.firmwarefile.FirmwareFile;
Expand Down Expand Up @@ -161,7 +161,7 @@ private String calcDigest(
try {
final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
final byte[] digest = messageDigest.digest(content);
digestStr = new BigInteger(1, digest).toString(16);
digestStr = Hex.toHexString(digest);
} catch (final NoSuchAlgorithmException e) {
log.error("Error calculating digest", e);
throw new ProtocolAdapterException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.firmware;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
Expand Down Expand Up @@ -316,7 +315,7 @@ private String calculateHash(final byte[] content, final String algorithm) {
try {
final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
final byte[] digest = messageDigest.digest(content);
return new BigInteger(1, digest).toString(16);
return Hex.toHexString(digest);
} catch (final NoSuchAlgorithmException e) {
log.error("Error calculating digest", e);
return "";
Expand Down

0 comments on commit 5ff79fd

Please sign in to comment.