All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whispersystems.signalservice.api.kbs.PinValidityChecker Maven / Gradle / Ivy

There is a newer version: 2.15.3_unofficial_107
Show newest version
/*
 * Copyright 2023 Signal Messenger, LLC
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package org.whispersystems.signalservice.api.kbs;

public final class PinValidityChecker {

  public static boolean valid(String pin) {
    pin = pin.trim();

    if (pin.isEmpty()) {
      return false;
    }

    if (PinString.allNumeric(pin)) {
      pin = PinString.toArabic(pin);

      return !sequential(pin) &&
             !sequential(reverse(pin)) &&
             !allTheSame(pin);
    } else {
      return true;
    }
  }

  private static String reverse(String string) {
    char[] chars = string.toCharArray();

    for (int i = 0; i < chars.length / 2; i++) {
      char temp = chars[i];
      chars[i] = chars[chars.length - i - 1];
      chars[chars.length - i - 1] = temp;
    }

    return new String(chars);
  }

  private static boolean sequential(String pin) {
    int length = pin.length();

    if (length == 0) {
      return false;
    }

    char c = pin.charAt(0);

    for (int i = 1; i < length; i++) {
      char n = pin.charAt(i);
      if (n != c + 1) {
        return false;
      }
      c = n;
    }

    return true;
  }

  private static boolean allTheSame(String pin) {
    int length = pin.length();

    if (length == 0) {
      return false;
    }

    char c = pin.charAt(0);

    for (int i = 1; i < length; i++) {
      char n = pin.charAt(i);
      if (n != c) {
        return false;
      }
    }

    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy