Skip to content

Commit

Permalink
fix: combination culumative hash
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrofpk committed Mar 25, 2022
1 parent 405d961 commit 44e741a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CombinationService {

private final Environment env;

private List<SeedPostDto> seedList;
private final List<SeedPostDto> seedList;

private final ICipherSuite cipherSuite;

Expand Down Expand Up @@ -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);

Expand All @@ -114,21 +114,6 @@ private List<SeedSourceDto> getDelayedPulses(){
return seedBuilder.getPreDefSeedCombination();
}

private List<SeedUnicordCombinationVo> calcSeedConcat(List<SeedSourceDto> seedList) {

String currentValue = "";
List<SeedUnicordCombinationVo> 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...");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SeedUnicordCombinationVo> seedUnicordCombinationVos = new ArrayList<>();

public List<SeedUnicordCombinationVo> calcSeedConcat(ICipherSuite cipherSuite, List<SeedSourceDto> seedList) {

String currentValue = "";
List<SeedUnicordCombinationVo> 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;
}

}

Original file line number Diff line number Diff line change
@@ -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<SeedSourceDto> seeds = new ArrayList<>();

SeedSourceDto seedSourceDto1 = new SeedSourceDto(DateUtil.getTimeStampFormated(ZonedDateTime.now()),
"uri", "aSeed1Test", "aDescription", null);

seeds.add(seedSourceDto1);

CombinationUncornCumulativeHash combinationUncornCumulativeHash = new CombinationUncornCumulativeHash();
List<SeedUnicordCombinationVo> 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<SeedSourceDto> 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<SeedUnicordCombinationVo> 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<SeedSourceDto> 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<SeedUnicordCombinationVo> resultList = combinationUncornCumulativeHash.calcSeedConcat(CipherSuiteBuilder.build(0), seeds);

Assert.assertEquals("132543ac76c98f8b17c4893a275acb0871deea83726929dc391f93de435e763b9c739adaad329e0cdbc29e4c578f31ab702af4682c979f0837a7e9327299d179",
resultList.get(2).getCumulativeHash());

//https://emn178.github.io/online-tools/sha512.html
}


}

0 comments on commit 44e741a

Please sign in to comment.