Full 160 bit for phrase confirm, stuffed with ones

This commit is contained in:
Dominik Schürmann
2015-09-29 14:38:30 +02:00
parent 3df9bea455
commit 2535ecb91f
3 changed files with 25 additions and 15 deletions

View File

@@ -26,7 +26,7 @@ import java.io.InputStream;
* general Java InputStream. * general Java InputStream.
* Like the various Stream-classes from Java, the BitInputStream * Like the various Stream-classes from Java, the BitInputStream
* has to be created based on another Input stream. It provides * has to be created based on another Input stream. It provides
* a function to read the next bit from the sream, as well as to read multiple * a function to read the next bit from the stream, as well as to read multiple
* bits at once and write the resulting data into an integer value. * bits at once and write the resulting data into an integer value.
* <p/> * <p/>
* source: http://developer.nokia.com/Community/Wiki/Bit_Input/Output_Stream_utility_classes_for_efficient_data_transfer * source: http://developer.nokia.com/Community/Wiki/Bit_Input/Output_Stream_utility_classes_for_efficient_data_transfer
@@ -69,11 +69,11 @@ public class BitInputStream {
* @return integer value containing the bits read from the stream. * @return integer value containing the bits read from the stream.
* @throws IOException * @throws IOException
*/ */
synchronized public int readBits(final int aNumberOfBits) synchronized public int readBits(final int aNumberOfBits, boolean stuffIfEnd)
throws IOException { throws IOException {
int value = 0; int value = 0;
for (int i = aNumberOfBits - 1; i >= 0; i--) { for (int i = aNumberOfBits - 1; i >= 0; i--) {
value |= (readBit() << i); value |= (readBit(stuffIfEnd) << i);
} }
return value; return value;
} }
@@ -92,15 +92,20 @@ public class BitInputStream {
* @return 0 if the bit is 0, 1 if the bit is 1. * @return 0 if the bit is 0, 1 if the bit is 1.
* @throws IOException * @throws IOException
*/ */
synchronized public int readBit() throws IOException { synchronized public int readBit(boolean stuffIfEnd) throws IOException {
if (iIs == null) if (iIs == null)
throw new IOException("Already closed"); throw new IOException("Already closed");
if (iNextBit == 8) { if (iNextBit == 8) {
iBuffer = iIs.read(); iBuffer = iIs.read();
if (iBuffer == -1) if (iBuffer == -1) {
throw new EOFException(); if (stuffIfEnd) {
return 1;
} else {
throw new EOFException();
}
}
iNextBit = 0; iNextBit = 0;
} }

View File

@@ -99,8 +99,12 @@ public class SentenceConfirm {
*/ */
private EntropyString getWord(List<String> words, BitInputStream bin) throws IOException { private EntropyString getWord(List<String> words, BitInputStream bin) throws IOException {
final int neededBits = log(words.size(), 2); final int neededBits = log(words.size(), 2);
Log.d(Constants.TAG, "need " + neededBits + " bits of entropy"); Log.d(Constants.TAG, "need: " + neededBits + " bits of entropy");
int bits = bin.readBits(neededBits); Log.d(Constants.TAG, "available: " + bin.available() + " bits");
if (neededBits > bin.available()) {
Log.d(Constants.TAG, "stuffed with " + (neededBits - bin.available()) + " ones!");
}
int bits = bin.readBits(neededBits, true);
Log.d(Constants.TAG, "got word " + words.get(bits) + " with " + neededBits + " bits of entropy"); Log.d(Constants.TAG, "got word " + words.get(bits) + " with " + neededBits + " bits of entropy");
return new EntropyString(words.get(bits), neededBits); return new EntropyString(words.get(bits), neededBits);
} }
@@ -108,7 +112,7 @@ public class SentenceConfirm {
private EntropyString getNounPhrase(BitInputStream bits) throws IOException { private EntropyString getNounPhrase(BitInputStream bits) throws IOException {
final EntropyString phrase = new EntropyString(); final EntropyString phrase = new EntropyString();
phrase.append(getWord(art, bits)).append(" "); phrase.append(getWord(art, bits)).append(" ");
if (bits.readBit() != 0) { if (bits.readBit(true) != 0) {
phrase.append(getWord(adj, bits)).append(" "); phrase.append(getWord(adj, bits)).append(" ");
} }
phrase.incBits(); phrase.incBits();
@@ -121,7 +125,7 @@ public class SentenceConfirm {
EntropyString getSentence(BitInputStream bits) throws IOException { EntropyString getSentence(BitInputStream bits) throws IOException {
final EntropyString sentence = new EntropyString(); final EntropyString sentence = new EntropyString();
sentence.append(getNounPhrase(bits)); // Subject sentence.append(getNounPhrase(bits)); // Subject
if (bits.readBit() != 0) { if (bits.readBit(true) != 0) {
sentence.append(" ").append(getWord(vt, bits)); // Transitive verb sentence.append(" ").append(getWord(vt, bits)); // Transitive verb
sentence.append(" ").append(getNounPhrase(bits)); // Object of transitive verb sentence.append(" ").append(getNounPhrase(bits)); // Object of transitive verb
} else { } else {
@@ -129,17 +133,17 @@ public class SentenceConfirm {
} }
sentence.incBits(); sentence.incBits();
if (bits.readBit() != 0) { if (bits.readBit(true) != 0) {
sentence.append(" ").append(getWord(adv, bits)); // Adverb sentence.append(" ").append(getWord(adv, bits)); // Adverb
} }
sentence.incBits(); sentence.incBits();
if (bits.readBit() != 0) { if (bits.readBit(true) != 0) {
sentence.append(" ").append(getWord(p, bits)); // Preposition sentence.append(" ").append(getWord(p, bits)); // Preposition
sentence.append(" ").append(getNounPhrase(bits)); // Object of preposition sentence.append(" ").append(getNounPhrase(bits)); // Object of preposition
} }
sentence.incBits(); sentence.incBits();
Log.d(Constants.TAG, "got sentence " + sentence + " with " + sentence.getBits() + " bits of entropy"); Log.d(Constants.TAG, "got sentence '" + sentence + "' with " + sentence.getBits() + " bits of entropy");
// uppercase first character, end with dot (without increasing the bits) // uppercase first character, end with dot (without increasing the bits)
sentence.getBuilder().replace(0, 1, sentence.getBuilder().replace(0, 1,

View File

@@ -191,9 +191,10 @@ public class CertifyFingerprintFragment extends LoaderFragment implements
String fingerprint; String fingerprint;
try { try {
fingerprint = new SentenceConfirm(getActivity()).fromBytes(fingerprintBlob, 16); fingerprint = new SentenceConfirm(getActivity()).fromBytes(fingerprintBlob, 20);
} catch (IOException ioe) { } catch (IOException e) {
fingerprint = "-"; fingerprint = "-";
Log.e(Constants.TAG, "Problem when creating sentence!", e);
} }
mFingerprint.setTextSize(18); mFingerprint.setTextSize(18);