That fuck shit the fascists are using
at master 56 lines 2.1 kB view raw
1package org.tm.archive.backup; 2 3import android.content.Context; 4import android.os.Build; 5 6import androidx.annotation.NonNull; 7import androidx.annotation.Nullable; 8 9import org.signal.core.util.logging.Log; 10import org.tm.archive.crypto.KeyStoreHelper; 11import org.tm.archive.util.TextSecurePreferences; 12 13/** 14 * Allows the getting and setting of the backup passphrase, which is stored encrypted on API >= 23. 15 */ 16public final class BackupPassphrase { 17 18 private BackupPassphrase() { 19 } 20 21 private static final String TAG = Log.tag(BackupPassphrase.class); 22 23 public static @Nullable String get(@NonNull Context context) { 24 String passphrase = TextSecurePreferences.getBackupPassphrase(context); 25 String encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context); 26 27 if (Build.VERSION.SDK_INT < 23 || (passphrase == null && encryptedPassphrase == null)) { 28 return stripSpaces(passphrase); 29 } 30 31 if (encryptedPassphrase == null) { 32 Log.i(TAG, "Migrating to encrypted passphrase."); 33 set(context, passphrase); 34 encryptedPassphrase = TextSecurePreferences.getEncryptedBackupPassphrase(context); 35 if (encryptedPassphrase == null) throw new AssertionError("Passphrase migration failed"); 36 } 37 38 KeyStoreHelper.SealedData data = KeyStoreHelper.SealedData.fromString(encryptedPassphrase); 39 return stripSpaces(new String(KeyStoreHelper.unseal(data))); 40 } 41 42 public static void set(@NonNull Context context, @Nullable String passphrase) { 43 if (passphrase == null || Build.VERSION.SDK_INT < 23) { 44 TextSecurePreferences.setBackupPassphrase(context, passphrase); 45 TextSecurePreferences.setEncryptedBackupPassphrase(context, null); 46 } else { 47 KeyStoreHelper.SealedData encryptedPassphrase = KeyStoreHelper.seal(passphrase.getBytes()); 48 TextSecurePreferences.setEncryptedBackupPassphrase(context, encryptedPassphrase.serialize()); 49 TextSecurePreferences.setBackupPassphrase(context, null); 50 } 51 } 52 53 private static String stripSpaces(@Nullable String passphrase) { 54 return passphrase != null ? passphrase.replace(" ", "") : null; 55 } 56}