simplify AutocryptHeader
This commit is contained in:
parent
88c92feaf3
commit
dae9c44182
2 changed files with 18 additions and 43 deletions
|
@ -54,39 +54,26 @@ class AutocryptHeader {
|
|||
.append('=').append(AutocryptHeader.AUTOCRYPT_PREFER_ENCRYPT_MUTUAL).append("; ");
|
||||
}
|
||||
builder.append(AutocryptHeader.AUTOCRYPT_PARAM_KEY_DATA).append("=");
|
||||
|
||||
appendBase64KeyData(builder);
|
||||
builder.append(createFoldedBase64KeyData(keyData));
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void appendBase64KeyData(StringBuilder builder) {
|
||||
static String createFoldedBase64KeyData(byte[] keyData) {
|
||||
String base64KeyData = ByteString.of(keyData).base64();
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
int base64Length = base64KeyData.length();
|
||||
int lineLengthBeforeKeyData = builder.length();
|
||||
int dataLengthInFirstLine = HEADER_LINE_LENGTH -lineLengthBeforeKeyData;
|
||||
|
||||
boolean keyDataFitsInFirstLine = dataLengthInFirstLine > 0 && base64Length < dataLengthInFirstLine;
|
||||
if (keyDataFitsInFirstLine) {
|
||||
builder.append(base64KeyData, 0, base64Length);
|
||||
return;
|
||||
}
|
||||
|
||||
if (dataLengthInFirstLine > 0) {
|
||||
builder.append(base64KeyData, 0, dataLengthInFirstLine).append("\r\n ");
|
||||
} else {
|
||||
builder.append("\r\n ");
|
||||
dataLengthInFirstLine = 0;
|
||||
}
|
||||
|
||||
for (int i = dataLengthInFirstLine; i < base64Length; i += HEADER_LINE_LENGTH) {
|
||||
for (int i = 0, base64Length = base64KeyData.length(); i < base64Length; i += HEADER_LINE_LENGTH) {
|
||||
if (i + HEADER_LINE_LENGTH <= base64Length) {
|
||||
builder.append(base64KeyData, i, i + HEADER_LINE_LENGTH).append("\r\n ");
|
||||
result.append("\r\n ");
|
||||
result.append(base64KeyData, i, i + HEADER_LINE_LENGTH);
|
||||
} else {
|
||||
builder.append(base64KeyData, i, base64Length);
|
||||
result.append("\r\n ");
|
||||
result.append(base64KeyData, i, base64Length);
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -5,17 +5,15 @@ import java.util.HashMap;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class AutocryptHeaderTest {
|
||||
static final HashMap<String, String> PARAMETERS = new HashMap<>();
|
||||
static final String ADDR = "addr";
|
||||
static final String ADDR_LONG = "veryveryverylongaddressthatspansmorethanalinelengthintheheader";
|
||||
static final byte[] KEY_DATA = ("theseare120charactersxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
|
||||
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").getBytes();
|
||||
static final byte[] KEY_DATA_SHORT = ("theseare15chars").getBytes();
|
||||
static final boolean IS_PREFER_ENCRYPT_MUTUAL = true;
|
||||
|
||||
|
||||
|
@ -24,19 +22,7 @@ public class AutocryptHeaderTest {
|
|||
AutocryptHeader autocryptHeader = new AutocryptHeader(PARAMETERS, ADDR, KEY_DATA, IS_PREFER_ENCRYPT_MUTUAL);
|
||||
String autocryptHeaderString = autocryptHeader.toRawHeaderString();
|
||||
|
||||
String expected = "Autocrypt: addr=addr; prefer-encrypt=mutual; keydata=dGhlc2VhcmUxMjBjaGFyYWN\r\n" +
|
||||
" 0ZXJzeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh\r\n" +
|
||||
" 4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4";
|
||||
assertEquals(expected, autocryptHeaderString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toRawHeaderString_withLongAddress_returnsExpected() throws Exception {
|
||||
AutocryptHeader autocryptHeader = new AutocryptHeader(PARAMETERS,
|
||||
ADDR_LONG, KEY_DATA, IS_PREFER_ENCRYPT_MUTUAL);
|
||||
String autocryptHeaderString = autocryptHeader.toRawHeaderString();
|
||||
|
||||
String expected = "Autocrypt: addr=veryveryverylongaddressthatspansmorethanalinelengthintheheader; prefer-encrypt=mutual; keydata=\r\n" +
|
||||
String expected = "Autocrypt: addr=addr; prefer-encrypt=mutual; keydata=\r\n" +
|
||||
" dGhlc2VhcmUxMjBjaGFyYWN0ZXJzeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\r\n" +
|
||||
" eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\r\n" +
|
||||
" eHh4eHh4";
|
||||
|
@ -44,12 +30,14 @@ public class AutocryptHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void toRawHeaderString_withShortData_returnsExpected() throws Exception {
|
||||
AutocryptHeader autocryptHeader = new AutocryptHeader(PARAMETERS,
|
||||
ADDR, KEY_DATA_SHORT, IS_PREFER_ENCRYPT_MUTUAL);
|
||||
public void gossip_toRawHeaderString_returnsExpected() throws Exception {
|
||||
AutocryptGossipHeader autocryptHeader = new AutocryptGossipHeader(ADDR, KEY_DATA);
|
||||
String autocryptHeaderString = autocryptHeader.toRawHeaderString();
|
||||
|
||||
String expected = "Autocrypt: addr=addr; prefer-encrypt=mutual; keydata=dGhlc2VhcmUxNWNoYXJz";
|
||||
String expected = "Autocrypt-Gossip: addr=addr; keydata=\r\n" +
|
||||
" dGhlc2VhcmUxMjBjaGFyYWN0ZXJzeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\r\n" +
|
||||
" eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4\r\n" +
|
||||
" eHh4eHh4";
|
||||
assertEquals(expected, autocryptHeaderString);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue