Rearranged duplicate code fragments

This commit is contained in:
Marcin Kostrzewski 2020-10-28 17:39:26 +01:00
parent 34a1fa996d
commit 0f939b9e09

View File

@ -36,17 +36,8 @@ public class Commitment {
this.sequenceB = generateRandomSequence(); this.sequenceB = generateRandomSequence();
logger.log(Level.INFO, "Random sequence B is " + Utils.bytesToHex(this.sequenceB)); logger.log(Level.INFO, "Random sequence B is " + Utils.bytesToHex(this.sequenceB));
try { this.hash = calculateHash();
MessageDigest digest = MessageDigest.getInstance("SHA-256"); logger.log(Level.INFO, "Hash is " + Utils.bytesToHex(this.hash));
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 " + Utils.bytesToHex(this.hash));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} }
public Commitment(byte[] sequenceA, byte[] hash) { public Commitment(byte[] sequenceA, byte[] hash) {
@ -54,21 +45,24 @@ public class Commitment {
this.hash = hash; this.hash = hash;
} }
public boolean checkHash() { private byte[] calculateHash(){
boolean result = false; byte[] result = new byte[32];
try { try {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(this.sequenceA); digest.update(this.sequenceA);
digest.update(this.sequenceB); digest.update(this.sequenceB);
digest.update(this.message.getBytes(StandardCharsets.UTF_8)); digest.update(this.message.getBytes(StandardCharsets.UTF_8));
result = digest.digest();
result = Arrays.equals(this.hash, digest.digest());
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
return result; return result;
} }
public boolean checkHash() {
return Arrays.equals(this.hash, calculateHash());
}
public String getMessage() { public String getMessage() {
return message; return message;