Rearranged duplicate code fragments
This commit is contained in:
parent
34a1fa996d
commit
0f939b9e09
@ -36,17 +36,8 @@ public class Commitment {
|
||||
this.sequenceB = generateRandomSequence();
|
||||
logger.log(Level.INFO, "Random sequence B is " + Utils.bytesToHex(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();
|
||||
this.hash = calculateHash();
|
||||
logger.log(Level.INFO, "Hash is " + Utils.bytesToHex(this.hash));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Commitment(byte[] sequenceA, byte[] hash) {
|
||||
@ -54,21 +45,24 @@ public class Commitment {
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
public boolean checkHash() {
|
||||
boolean result = false;
|
||||
private byte[] calculateHash(){
|
||||
byte[] result = new byte[32];
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
digest.update(this.sequenceA);
|
||||
digest.update(this.sequenceB);
|
||||
digest.update(this.message.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
result = Arrays.equals(this.hash, digest.digest());
|
||||
result = digest.digest();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean checkHash() {
|
||||
return Arrays.equals(this.hash, calculateHash());
|
||||
}
|
||||
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
|
Loading…
Reference in New Issue
Block a user