2020-09-01 14:09:20 +00:00
|
|
|
package io.rudefox.burrow;
|
|
|
|
|
|
|
|
import com.bjdweck.bitcoin.mnemonic.Entropy;
|
2020-09-25 09:09:04 +00:00
|
|
|
import com.diogonunes.jcolor.AnsiFormat;
|
2020-09-01 14:09:20 +00:00
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2020-09-25 09:09:04 +00:00
|
|
|
import static com.diogonunes.jcolor.Attribute.*;
|
|
|
|
|
2020-09-01 14:09:20 +00:00
|
|
|
public class Dice8EntropyGenerator {
|
|
|
|
|
2020-09-25 09:26:00 +00:00
|
|
|
public static final AnsiFormat RED_STYLE = new AnsiFormat(WHITE_TEXT(), RED_BACK(), BOLD());
|
2020-09-25 10:29:54 +00:00
|
|
|
public static final AnsiFormat GREEN_STYLE = new AnsiFormat(BLACK_TEXT(), GREEN_BACK(), BOLD());
|
|
|
|
public static final AnsiFormat BLUE_STYLE = new AnsiFormat(WHITE_TEXT(), BLUE_BACK(), BOLD());
|
2020-09-25 09:09:04 +00:00
|
|
|
|
2020-09-01 14:09:20 +00:00
|
|
|
public static final int DICE_PER_ROLL = 11;
|
|
|
|
|
|
|
|
private final int targetBitsOfEntropy;
|
|
|
|
private final Scanner inputScanner;
|
2020-09-25 09:09:04 +00:00
|
|
|
private final boolean ansiColorOutput;
|
2020-09-01 14:09:20 +00:00
|
|
|
|
|
|
|
private Entropy entropy = new Entropy();
|
|
|
|
|
2020-09-25 09:09:04 +00:00
|
|
|
public Dice8EntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner, boolean ansiColorOutput) {
|
2020-09-01 14:09:20 +00:00
|
|
|
|
|
|
|
this.targetBitsOfEntropy = targetBitsOfEntropy;
|
|
|
|
this.inputScanner = inputScanner;
|
2020-09-25 09:09:04 +00:00
|
|
|
this.ansiColorOutput = ansiColorOutput;
|
2020-09-01 14:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Entropy generate() {
|
|
|
|
|
|
|
|
entropy = new Entropy();
|
|
|
|
|
|
|
|
for (int rollSetCount = 0; entropy.getBitLength() < targetBitsOfEntropy; rollSetCount++)
|
|
|
|
doDiceRoll(rollSetCount);
|
|
|
|
|
|
|
|
return entropy.truncate(targetBitsOfEntropy).appendChecksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doDiceRoll(int currentRollSet) {
|
|
|
|
|
|
|
|
String eventString = scanNextRollSetString();
|
|
|
|
|
|
|
|
StringBuilder rollValuesLine = new StringBuilder("|");
|
|
|
|
|
|
|
|
for (int rollNumber = 0; rollNumber < DICE_PER_ROLL; rollNumber++) {
|
|
|
|
|
|
|
|
int rollValue = Integer.parseInt(eventString.charAt(rollNumber) + "");
|
|
|
|
|
|
|
|
String formatString =
|
|
|
|
rollNumber == 3 || rollNumber == 7 ? " %d |" : " %d |";
|
|
|
|
|
|
|
|
rollValuesLine.append(String.format(formatString, rollValue));
|
|
|
|
|
|
|
|
entropy = entropy.appendBits(rollValue - 1, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.printf("%n%s%n%s%n%s%n%n",
|
|
|
|
rollValuesLine,
|
|
|
|
getBitsLine(),
|
|
|
|
getSeedWordsLine(currentRollSet)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String scanNextRollSetString() {
|
|
|
|
|
|
|
|
String rollSetString = "";
|
|
|
|
Pattern rollSetPattern = Pattern.compile(String.format("[1-8]{%d}", DICE_PER_ROLL));
|
|
|
|
|
|
|
|
while (!rollSetPattern.matcher(rollSetString).matches()) {
|
|
|
|
System.out.print("Input 11 x 8-sided dice rolls [1-8]: ");
|
|
|
|
rollSetString = inputScanner.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rollSetString;
|
|
|
|
}
|
|
|
|
|
|
|
|
private StringBuilder getBitsLine() {
|
|
|
|
|
|
|
|
String entropyBitString = entropy.toString();
|
|
|
|
|
|
|
|
String currentRollSetBitString =
|
|
|
|
entropyBitString.substring(entropyBitString.length() - (DICE_PER_ROLL * 3));
|
|
|
|
|
|
|
|
StringBuilder rollSetBitsLine = new StringBuilder("|");
|
|
|
|
for (int rollSetBitIndex = 0; rollSetBitIndex < currentRollSetBitString.length(); rollSetBitIndex++) {
|
|
|
|
|
|
|
|
int entropyBitIndex = entropyBitString.length() - currentRollSetBitString.length() + rollSetBitIndex;
|
|
|
|
|
|
|
|
if (entropyBitIndex < targetBitsOfEntropy)
|
2020-09-25 11:12:20 +00:00
|
|
|
rollSetBitsLine.append(styleBit("" + currentRollSetBitString.charAt(rollSetBitIndex), entropyBitIndex, rollSetBitIndex));
|
2020-09-01 14:09:20 +00:00
|
|
|
else
|
|
|
|
rollSetBitsLine.append("-");
|
|
|
|
|
2020-09-25 11:12:20 +00:00
|
|
|
if (rollSetBitIndex == 32)
|
|
|
|
rollSetBitsLine.append("|");
|
|
|
|
else if (rollSetBitIndex % 3 == 2)
|
|
|
|
rollSetBitsLine.append(styleBit(" ", entropyBitIndex, rollSetBitIndex));
|
|
|
|
else if (rollSetBitIndex % 11 == 10)
|
|
|
|
rollSetBitsLine.append(" | ");
|
2020-09-01 14:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rollSetBitsLine;
|
|
|
|
}
|
|
|
|
|
2020-09-25 11:12:20 +00:00
|
|
|
private String styleBit(String bit, int globalEntropyBitIndex, int currentRollSetBitIndex) {
|
2020-09-25 09:09:04 +00:00
|
|
|
|
2020-09-25 11:12:20 +00:00
|
|
|
if (!ansiColorOutput || globalEntropyBitIndex >= targetBitsOfEntropy)
|
|
|
|
return bit;
|
2020-09-25 09:09:04 +00:00
|
|
|
|
2020-09-25 11:12:20 +00:00
|
|
|
return getBitFormat(currentRollSetBitIndex).format(bit);
|
2020-09-25 09:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private AnsiFormat getBitFormat(int rollSetBitIndex) {
|
|
|
|
|
|
|
|
int wordBitIndex = rollSetBitIndex % 11;
|
|
|
|
|
|
|
|
if (wordBitIndex == 0)
|
2020-09-25 10:29:54 +00:00
|
|
|
return RED_STYLE;
|
2020-09-25 09:09:04 +00:00
|
|
|
|
|
|
|
if (wordBitIndex <= 6)
|
2020-09-25 10:29:54 +00:00
|
|
|
return GREEN_STYLE;
|
2020-09-25 09:09:04 +00:00
|
|
|
|
2020-09-25 10:29:54 +00:00
|
|
|
return BLUE_STYLE;
|
2020-09-25 09:09:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 14:09:20 +00:00
|
|
|
private String getSeedWordsLine(int rollSet) {
|
|
|
|
|
|
|
|
int baseIndex = rollSet * 3;
|
|
|
|
|
|
|
|
return String.format(
|
|
|
|
"|%-15s|%-17s|%-15s|",
|
|
|
|
getFormattedSeedWord(baseIndex),
|
|
|
|
getFormattedSeedWord(baseIndex + 1),
|
|
|
|
getFormattedSeedWord(baseIndex + 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getFormattedSeedWord(int index) {
|
|
|
|
|
|
|
|
int lastWordIndex = (3 * targetBitsOfEntropy / 32) - 1;
|
|
|
|
|
|
|
|
String seedWord = index != lastWordIndex ? entropy.getWord(index) : "CHECKWORD";
|
|
|
|
|
|
|
|
return String.format(" %2d. %s", index + 1, seedWord);
|
|
|
|
}
|
2020-09-01 12:18:32 +00:00
|
|
|
}
|