0
0
mirror of https://github.com/signalapp/libsignal.git synced 2024-09-20 03:52:17 +02:00

Merge pull request #336 from signalapp/cody/bugfix/hex-match-expected-output

Java: Make toStringCondensed match output from other Signal implementations.
This commit is contained in:
Jordan Rose 2021-07-09 12:46:03 -07:00 committed by GitHub
commit 528aec7e26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 24 deletions

View File

@ -30,14 +30,6 @@ public class Hex {
}
public static String toStringCondensed(byte[] bytes) {
StringBuffer buf = new StringBuffer();
for (int i=0;i<bytes.length;i++) {
appendHexCharWithPrefix(buf, bytes[i]);
}
return buf.toString();
}
public static String toHexString(byte[] bytes) {
StringBuffer buf = new StringBuffer();
for (int i=0;i<bytes.length;i++) {
appendHexChar(buf, bytes[i]);

View File

@ -36,6 +36,6 @@ public class Aes256Ctr32Tests extends TestCase {
byte[] ciphertext = plaintext.clone();
ctr.process(ciphertext);
assertEquals(Hex.toHexString(ciphertext), hex_ciphertext);
assertEquals(Hex.toStringCondensed(ciphertext), hex_ciphertext);
}
}

View File

@ -40,13 +40,13 @@ public class Aes256GcmEncryptionTests extends TestCase {
byte[] ciphertext = plaintext.clone();
gcmEnc.encrypt(ciphertext);
byte[] tag = gcmEnc.computeTag();
assertEquals(Hex.toHexString(ciphertext), hex_ciphertext);
assertEquals(Hex.toHexString(tag), hex_tag);
assertEquals(Hex.toStringCondensed(ciphertext), hex_ciphertext);
assertEquals(Hex.toStringCondensed(tag), hex_tag);
Aes256GcmDecryption gcmDec = new Aes256GcmDecryption(key, nonce, ad);
byte[] decrypted = ciphertext.clone();
gcmDec.decrypt(decrypted);
assertEquals(Hex.toHexString(decrypted), hex_plaintext);
assertEquals(Hex.toStringCondensed(decrypted), hex_plaintext);
assertEquals(gcmDec.verifyTag(tag), true);
Aes256GcmEncryption gcmEnc2 = new Aes256GcmEncryption(key, nonce, ad);
@ -54,14 +54,14 @@ public class Aes256GcmEncryptionTests extends TestCase {
gcmEnc2.encrypt(ciphertextSplit, 0, 1);
gcmEnc2.encrypt(ciphertextSplit, 1, plaintext.length - 1);
byte[] tag2 = gcmEnc2.computeTag();
assertEquals(Hex.toHexString(ciphertextSplit), hex_ciphertext);
assertEquals(Hex.toHexString(tag2), hex_tag);
assertEquals(Hex.toStringCondensed(ciphertextSplit), hex_ciphertext);
assertEquals(Hex.toStringCondensed(tag2), hex_tag);
Aes256GcmDecryption gcmDec2 = new Aes256GcmDecryption(key, nonce, ad);
byte[] decryptedSplit = ciphertext.clone();
gcmDec2.decrypt(decryptedSplit, 0, 1);
gcmDec2.decrypt(decryptedSplit, 1, ciphertext.length - 1);
assertEquals(Hex.toHexString(decryptedSplit), hex_plaintext);
assertEquals(Hex.toStringCondensed(decryptedSplit), hex_plaintext);
assertEquals(gcmDec2.verifyTag(tag), true);
}
}

View File

@ -61,10 +61,10 @@ public class Aes256GcmSivTests extends TestCase {
Aes256GcmSiv gcm_siv = new Aes256GcmSiv(key);
byte[] ciphertext = gcm_siv.encrypt(plaintext, nonce, ad);
assertEquals(Hex.toHexString(ciphertext), hex_ciphertext);
assertEquals(Hex.toStringCondensed(ciphertext), hex_ciphertext);
byte[] recovered = gcm_siv.decrypt(ciphertext, nonce, ad);
assertEquals(Hex.toHexString(recovered), hex_plaintext);
assertEquals(Hex.toStringCondensed(recovered), hex_plaintext);
try {
ciphertext[0] ^= 1;

View File

@ -18,18 +18,18 @@ public class CryptographicHashTests extends TestCase {
hash.update(input);
byte[] digestAllInOne = hash.finish();
assertEquals(Hex.toHexString(digestAllInOne), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(digestAllInOne), hexExpectedOutput);
if(input.length > 1) {
hash.update(input, 0, 1);
hash.update(input, 1, input.length - 1);
byte[] digestSplit = hash.finish();
assertEquals(Hex.toHexString(digestSplit), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(digestSplit), hexExpectedOutput);
hash.update(input, 0, input.length - 1);
hash.update(input, input.length - 1, 1);
byte[] digestSplit2 = hash.finish();
assertEquals(Hex.toHexString(digestSplit2), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(digestSplit2), hexExpectedOutput);
}
}

View File

@ -17,18 +17,18 @@ public class CryptographicMacTests extends TestCase {
hmac.update(input);
byte[] macAllInOne = hmac.finish();
assertEquals(Hex.toHexString(macAllInOne), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(macAllInOne), hexExpectedOutput);
if(input.length > 1) {
hmac.update(input, 0, 1);
hmac.update(input, 1, input.length - 1);
byte[] macSplit = hmac.finish();
assertEquals(Hex.toHexString(macSplit), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(macSplit), hexExpectedOutput);
hmac.update(input, 0, input.length - 1);
hmac.update(input, input.length - 1, 1);
byte[] macSplit2 = hmac.finish();
assertEquals(Hex.toHexString(macSplit2), hexExpectedOutput);
assertEquals(Hex.toStringCondensed(macSplit2), hexExpectedOutput);
}
}

View File

@ -225,7 +225,7 @@ public class SealedSessionCipherTest extends TestCase {
assertEquals(e.getSender(), "+14151111111");
assertEquals(e.getSenderDevice(), 1);
assertEquals(e.getContentHint(), UnidentifiedSenderMessageContent.CONTENT_HINT_RESENDABLE);
assertEquals(Hex.toHexString(e.getGroupId().get()), Hex.toHexString(new byte[]{42, 1}));
assertEquals(Hex.toStringCondensed(e.getGroupId().get()), Hex.toStringCondensed(new byte[]{42, 1}));
}
}