package io.rudefox.burrow; import com.bjdweck.bitcoin.mnemonic.Entropy; import com.bjdweck.math.UnsignedInt; class EventBuffer { private final int eventBase; private final StringBuilder buffer; private final int targetBitsOfEntropy; EventBuffer(int targetBitsOfEntropy, int eventBase) { this.eventBase = eventBase; this.targetBitsOfEntropy = targetBitsOfEntropy; this.buffer = new StringBuilder(getRequiredEvents()); } int getRequiredEvents() { return (int) Math.ceil(this.targetBitsOfEntropy * Math.log(2) / Math.log(eventBase)); } void appendEvents(String eventString) { for (char inChar : eventString.toCharArray()) if (inChar >= '1' && inChar <= '6' && events() < getRequiredEvents()) append((char) (inChar - 1)); } Entropy toEntropy() { UnsignedInt entropy = new UnsignedInt(toString(), eventBase); byte[] entropyBytes = entropy.getLowestOrderBits(this.targetBitsOfEntropy).toBigEndianByteArray(); return Entropy.fromRawEntropy(entropyBytes); } int events() { return buffer.length(); } private void append(char c) { buffer.append(c); } @Override public String toString() { return buffer.toString(); } }