mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Switch to libsignal for PIN hashing.
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package org.thoughtcrime.securesms.lock;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.signal.argon2.Argon2;
|
||||
import org.signal.argon2.Argon2Exception;
|
||||
import org.signal.argon2.MemoryCost;
|
||||
import org.signal.argon2.Type;
|
||||
import org.signal.argon2.UnknownTypeException;
|
||||
import org.signal.argon2.Version;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.signalservice.api.KeyBackupService;
|
||||
import org.whispersystems.signalservice.api.kbs.HashedPin;
|
||||
import org.whispersystems.signalservice.internal.registrationpin.PinHasher;
|
||||
|
||||
public final class PinHashing {
|
||||
|
||||
private PinHashing() {
|
||||
}
|
||||
|
||||
public static HashedPin hashPin(@NonNull String pin, @NonNull KeyBackupService.HashSession hashSession) {
|
||||
return PinHasher.hashPin(PinHasher.normalize(pin), password -> {
|
||||
try {
|
||||
return new Argon2.Builder(Version.V13)
|
||||
.type(Type.Argon2id)
|
||||
.memoryCost(MemoryCost.MiB(16))
|
||||
.parallelism(1)
|
||||
.iterations(32)
|
||||
.hashLength(64)
|
||||
.build()
|
||||
.hash(password, hashSession.hashSalt())
|
||||
.getHash();
|
||||
} catch (Argon2Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static String localPinHash(@NonNull String pin) {
|
||||
byte[] normalized = PinHasher.normalize(pin);
|
||||
try {
|
||||
return new Argon2.Builder(Version.V13)
|
||||
.type(Type.Argon2i)
|
||||
.memoryCost(MemoryCost.KiB(256))
|
||||
.parallelism(1)
|
||||
.iterations(50)
|
||||
.hashLength(32)
|
||||
.build()
|
||||
.hash(normalized, Util.getSecretBytes(16))
|
||||
.getEncoded();
|
||||
} catch (Argon2Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean verifyLocalPinHash(@NonNull String localPinHash, @NonNull String pin) {
|
||||
byte[] normalized = PinHasher.normalize(pin);
|
||||
try {
|
||||
return Argon2.verify(localPinHash, normalized);
|
||||
} catch (UnknownTypeException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import org.thoughtcrime.securesms.lock.v2.CreateKbsPinActivity;
|
||||
import org.thoughtcrime.securesms.lock.v2.KbsConstants;
|
||||
import org.thoughtcrime.securesms.util.ServiceUtil;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
import org.whispersystems.signalservice.api.kbs.PinHashUtil;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -121,7 +122,7 @@ public final class SignalPinReminderDialog {
|
||||
if (text.length() >= KbsConstants.MINIMUM_PIN_LENGTH) {
|
||||
submit.setEnabled(true);
|
||||
|
||||
if (PinHashing.verifyLocalPinHash(localHash, text)) {
|
||||
if (PinHashUtil.verifyLocalPinHash(localHash, text)) {
|
||||
dialog.dismiss();
|
||||
mainCallback.onReminderCompleted(text, callback.hadWrongGuess());
|
||||
}
|
||||
@@ -180,7 +181,7 @@ public final class SignalPinReminderDialog {
|
||||
|
||||
if (pin.length() < KbsConstants.MINIMUM_PIN_LENGTH) return;
|
||||
|
||||
if (PinHashing.verifyLocalPinHash(localPinHash, pin)) {
|
||||
if (PinHashUtil.verifyLocalPinHash(localPinHash, pin)) {
|
||||
callback.onPinCorrect(pin);
|
||||
} else {
|
||||
callback.onPinWrong();
|
||||
|
||||
@@ -8,7 +8,7 @@ import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.thoughtcrime.securesms.util.SingleLiveEvent;
|
||||
import org.whispersystems.signalservice.internal.registrationpin.PinValidityChecker;
|
||||
import org.whispersystems.signalservice.api.kbs.PinValidityChecker;
|
||||
|
||||
public final class CreateKbsPinViewModel extends ViewModel implements BaseKbsPinViewModel {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user