Unify our Base64 utilities.

This commit is contained in:
Greyson Parrelli
2023-10-13 09:36:29 -07:00
committed by Cody Henthorne
parent e636e38ba1
commit 4fe6d79fff
122 changed files with 549 additions and 542 deletions

View File

@@ -8,6 +8,7 @@ import android.content.pm.Signature;
import androidx.annotation.NonNull;
import org.signal.core.util.Base64;
import org.signal.core.util.logging.Log;
import java.nio.charset.StandardCharsets;
@@ -56,7 +57,7 @@ public final class AppSignatureUtil {
byte[] hashSignature = messageDigest.digest();
hashSignature = Arrays.copyOfRange(hashSignature, 0, HASH_LENGTH_BYTES);
String base64Hash = Base64.encodeBytes(hashSignature);
String base64Hash = Base64.encodeWithPadding(hashSignature);
base64Hash = base64Hash.substring(0, HASH_LENGTH_CHARS);
return base64Hash;

View File

@@ -1,48 +0,0 @@
package org.thoughtcrime.securesms.util;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.IOException;
public final class Base64 {
private Base64() {
}
public static @NonNull byte[] decode(@NonNull String s) throws IOException {
return org.whispersystems.util.Base64.decode(s);
}
public static @NonNull byte[] decodeWithoutPadding(@NonNull String s) throws IOException {
return org.whispersystems.util.Base64.decodeWithoutPadding(s);
}
public static @NonNull String encodeBytes(@NonNull byte[] source) {
return org.whispersystems.util.Base64.encodeBytes(source);
}
public static @NonNull String encodeBytesWithoutPadding(@NonNull byte[] source) {
return org.whispersystems.util.Base64.encodeBytesWithoutPadding(source);
}
public static @NonNull byte[] decodeOrThrow(@NonNull String s) {
try {
return org.whispersystems.util.Base64.decode(s);
} catch (IOException e) {
throw new AssertionError();
}
}
public static @Nullable byte[] decodeNullableOrThrow(@Nullable String s) {
if (s == null) {
return null;
}
try {
return org.whispersystems.util.Base64.decode(s);
} catch (IOException e) {
throw new AssertionError();
}
}
}

View File

@@ -6,6 +6,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.signal.core.util.Base64;
import org.signal.core.util.logging.Log;
import org.signal.libsignal.protocol.IdentityKey;
import org.signal.libsignal.protocol.IdentityKeyPair;

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.util
import androidx.annotation.WorkerThread
import org.signal.core.util.Base64
import org.signal.core.util.logging.Log
import org.signal.libsignal.usernames.BaseUsernameException
import org.signal.libsignal.usernames.Username
@@ -11,7 +12,6 @@ import org.whispersystems.signalservice.api.push.ServiceId
import org.whispersystems.signalservice.api.push.UsernameLinkComponents
import org.whispersystems.signalservice.api.util.UuidUtil
import org.whispersystems.signalservice.api.util.toByteArray
import org.whispersystems.util.Base64UrlSafe
import java.io.IOException
import java.util.Locale
import java.util.Optional
@@ -85,7 +85,7 @@ object UsernameUtil {
Log.d(TAG, "No local user with this username. Searching remotely.")
return try {
fetchAciForUsernameHash(Base64UrlSafe.encodeBytesWithoutPadding(Username.hash(username)))
fetchAciForUsernameHash(Base64.encodeUrlSafeWithoutPadding(Username.hash(username)))
} catch (e: BaseUsernameException) {
Optional.empty()
}
@@ -97,7 +97,7 @@ object UsernameUtil {
*/
@Throws(BaseUsernameException::class)
fun hashUsernameToBase64(username: String?): String {
return Base64UrlSafe.encodeBytesWithoutPadding(Username.hash(username))
return Base64.encodeUrlSafeWithoutPadding(Username.hash(username))
}
@JvmStatic
@@ -117,7 +117,7 @@ object UsernameUtil {
*/
fun generateLink(components: UsernameLinkComponents): String {
val combined: ByteArray = components.entropy + components.serverId.toByteArray()
val base64 = Base64UrlSafe.encodeBytesWithoutPadding(combined)
val base64 = Base64.encodeUrlSafeWithoutPadding(combined)
return BASE_URL + base64
}
@@ -128,7 +128,7 @@ object UsernameUtil {
fun parseLink(url: String): UsernameLinkComponents? {
val match: MatchResult = URL_PATTERN.find(url) ?: return null
val path: String = match.groups[2]?.value ?: return null
val allBytes: ByteArray = Base64UrlSafe.decodePaddingAgnostic(path)
val allBytes: ByteArray = Base64.decode(path)
if (allBytes.size != 48) {
return null

View File

@@ -44,6 +44,7 @@ import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import org.signal.core.util.Base64;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
@@ -359,7 +360,7 @@ public class Util {
public static String getSecret(int size) {
byte[] secret = getSecretBytes(size);
return Base64.encodeBytes(secret);
return Base64.encodeWithPadding(secret);
}
public static byte[] getSecretBytes(int size) {