From 0d3d38e77304655e00aa1451de61a6b193bc6169 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Wed, 28 Oct 2020 17:19:17 +0100 Subject: [PATCH] Implemented commitment creation --- src/main/java/Main/Commitment.java | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/Main/Commitment.java diff --git a/src/main/java/Main/Commitment.java b/src/main/java/Main/Commitment.java new file mode 100644 index 0000000..f97fd0d --- /dev/null +++ b/src/main/java/Main/Commitment.java @@ -0,0 +1,57 @@ +package Main; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Random; + +public class Commitment { + private static final Logger logger = LogManager.getLogger(App.class); + + final private int randomSequenceByteCount = 128; + + private String message; + private byte[] sequenceA; + private byte[] sequenceB; + private byte[] hash; + + private byte[] generateRandomSequence(){ + byte[] result = new byte[randomSequenceByteCount]; + Random r = new Random(); + r.nextBytes(result); + return result; + } + + public Commitment(String message) { + logger.log(Level.INFO, "Creating commitment..."); + this.message = message; + + this.sequenceA = generateRandomSequence(); + logger.log(Level.INFO, "Random sequence A is " + Arrays.toString(this.sequenceA)); + this.sequenceB = generateRandomSequence(); + logger.log(Level.INFO, "Random sequence B is " + Arrays.toString(this.sequenceB)); + + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + digest.update(this.sequenceA); + digest.update(this.sequenceB); + digest.update(this.message.getBytes(StandardCharsets.UTF_8)); + + this.hash = digest.digest(); + logger.log(Level.INFO, "Hash is " + Arrays.toString(this.hash)); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + + public Commitment() { + } + + + +}