From 44e741a364af5e680c021e79d3248a33b2cf7644 Mon Sep 17 00:00:00 2001 From: Leandro Pinho Date: Fri, 25 Mar 2022 17:04:32 -0300 Subject: [PATCH] fix: combination culumative hash --- .../combination/CombinationService.java | 19 +---- .../CombinationUncornCumulativeHash.java | 40 ++++++++++ .../CombinationUncornCumulativeHashTest.java | 80 +++++++++++++++++++ 3 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHash.java create mode 100644 beacon-interface/src/test/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHashTest.java diff --git a/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationService.java b/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationService.java index c0b8ee3..03882e1 100644 --- a/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationService.java +++ b/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationService.java @@ -27,7 +27,7 @@ public class CombinationService { private final Environment env; - private List seedList; + private final List seedList; private final ICipherSuite cipherSuite; @@ -96,7 +96,7 @@ public void run(String timeStamp) throws Exception { seeds.addAll(preDefSeedCombination); seeds.addAll(seedBuilder.getHonestPartyCombination()); - seedUnicordCombinationVos = calcSeedConcat(seeds); + seedUnicordCombinationVos = new CombinationUncornCumulativeHash().calcSeedConcat(cipherSuite, seeds); final BigInteger x = new BigInteger(seedUnicordCombinationVos.get(seedUnicordCombinationVos.size() - 1).getCumulativeHash(), 16); @@ -114,21 +114,6 @@ private List getDelayedPulses(){ return seedBuilder.getPreDefSeedCombination(); } - private List calcSeedConcat(List seedList) { - - String currentValue = ""; - List out = new ArrayList<>(); - - for (SeedSourceDto dto : seedList) { - currentValue = currentValue + dto.getSeed(); - String cumulativeDigest = cipherSuite.getDigest(currentValue); - ZonedDateTime parse = ZonedDateTime.parse(dto.getTimeStamp(), DateTimeFormatter.ISO_DATE_TIME); - out.add(new SeedUnicordCombinationVo(dto.getUri(), dto.getSeed(), dto.getDescription(), cumulativeDigest, parse)); - } - - return out; - } - private void runAndPersist(BigInteger x, String timeStamp) throws Exception { combinationServiceCalcAndPersist.run(timeStamp, seedUnicordCombinationVos, x); logger.warn("Async..."); diff --git a/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHash.java b/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHash.java new file mode 100644 index 0000000..11d7b45 --- /dev/null +++ b/beacon-interface/src/main/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHash.java @@ -0,0 +1,40 @@ +package com.example.beacon.vdf.application.combination; + +import br.gov.inmetro.beacon.library.ciphersuite.suite0.ICipherSuite; +import com.example.beacon.vdf.application.combination.dto.SeedUnicordCombinationVo; +import com.example.beacon.vdf.sources.SeedSourceDto; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class CombinationUncornCumulativeHash { + + private List seedUnicordCombinationVos = new ArrayList<>(); + + public List calcSeedConcat(ICipherSuite cipherSuite, List seedList) { + + String currentValue = ""; + List out = new ArrayList<>(); + + for (int i = 0; i < seedList.size(); i++) { + SeedSourceDto dto = seedList.get(i); + + if (i == 0){ + currentValue = cipherSuite.getDigest(seedList.get(0).getSeed()); + } else { + String teste = currentValue + dto.getSeed(); + currentValue = cipherSuite.getDigest(teste); + } + + String cumulativeDigest = currentValue; + ZonedDateTime parse = ZonedDateTime.parse(dto.getTimeStamp(), DateTimeFormatter.ISO_DATE_TIME); + out.add(new SeedUnicordCombinationVo(dto.getUri(), dto.getSeed(), dto.getDescription(), cumulativeDigest, parse)); + } + + return out; + } + +} + diff --git a/beacon-interface/src/test/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHashTest.java b/beacon-interface/src/test/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHashTest.java new file mode 100644 index 0000000..f3c03f8 --- /dev/null +++ b/beacon-interface/src/test/java/com/example/beacon/vdf/application/combination/CombinationUncornCumulativeHashTest.java @@ -0,0 +1,80 @@ +package com.example.beacon.vdf.application.combination; + +import br.gov.inmetro.beacon.library.ciphersuite.suite0.CipherSuiteBuilder; +import com.example.beacon.vdf.application.combination.dto.SeedUnicordCombinationVo; +import com.example.beacon.vdf.infra.util.DateUtil; +import com.example.beacon.vdf.sources.SeedSourceDto; +import org.junit.Assert; +import org.junit.Test; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; + +public class CombinationUncornCumulativeHashTest { + + @Test + public void testOneSeed(){ + List seeds = new ArrayList<>(); + + SeedSourceDto seedSourceDto1 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed1Test", "aDescription", null); + + seeds.add(seedSourceDto1); + + CombinationUncornCumulativeHash combinationUncornCumulativeHash = new CombinationUncornCumulativeHash(); + List resultList = combinationUncornCumulativeHash.calcSeedConcat(CipherSuiteBuilder.build(0), seeds); + + Assert.assertEquals("8ee0a0899c2d82b2e5b3fdc3b6c4e7d63fb35e33194089e129a131f575c87541811f8ebdb66fceb8e84e655c2e307f592a2046fdd2ace9ffa2ce6076f35814c0", + resultList.get(0).getCumulativeHash()); + + //https://emn178.github.io/online-tools/sha512.html + } + + @Test + public void testTwoSeeds(){ + List seeds = new ArrayList<>(); + + SeedSourceDto seedSourceDto1 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed1", "aDescription", null); + SeedSourceDto seedSourceDto2 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed2", "aDescription2", null); + + seeds.add(seedSourceDto1); + seeds.add(seedSourceDto2); + + CombinationUncornCumulativeHash combinationUncornCumulativeHash = new CombinationUncornCumulativeHash(); + List resultList = combinationUncornCumulativeHash.calcSeedConcat(CipherSuiteBuilder.build(0), seeds); + + Assert.assertEquals("175a91bccfefb53dad84d4a1ea49e66e2fe846f3210e3550c1fd874bdbe283965815b6f8db2caa5e9d0fc15b6fbd8810f3017e6ac315e828e75191a94128ceda", + resultList.get(1).getCumulativeHash()); + + //https://emn178.github.io/online-tools/sha512.html + } + + @Test + public void testThreeSeeds(){ + List seeds = new ArrayList<>(); + + SeedSourceDto seedSourceDto1 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed1", "aDescription", null); + SeedSourceDto seedSourceDto2 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed2", "aDescription2", null); + SeedSourceDto seedSourceDto3 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()), + "uri", "aSeed2Test3", "aDescription2", null); + + seeds.add(seedSourceDto1); + seeds.add(seedSourceDto2); + seeds.add(seedSourceDto3); + + CombinationUncornCumulativeHash combinationUncornCumulativeHash = new CombinationUncornCumulativeHash(); + List resultList = combinationUncornCumulativeHash.calcSeedConcat(CipherSuiteBuilder.build(0), seeds); + + Assert.assertEquals("132543ac76c98f8b17c4893a275acb0871deea83726929dc391f93de435e763b9c739adaad329e0cdbc29e4c578f31ab702af4682c979f0837a7e9327299d179", + resultList.get(2).getCumulativeHash()); + + //https://emn178.github.io/online-tools/sha512.html + } + + +} \ No newline at end of file