Compare commits

..

1 commit

Author SHA1 Message Date
48b7f0bfef
feat: stricted imei checking 2025-03-07 05:18:55 +08:00
2 changed files with 15 additions and 25 deletions

View file

@ -68,36 +68,28 @@ class DownloadWizardDetailsFragment : DownloadWizardActivity.DownloadWizardStepF
} }
private fun updateInputCompleteness() { private fun updateInputCompleteness() {
validate() inputComplete = runCatching(::validate).isSuccess
val errors = arrayOf(
smdp.editText!!.error,
imei.editText!!.error,
)
inputComplete = errors.all { it == null }
refreshButtons() refreshButtons()
} }
private fun validate() { private fun validate() {
smdp.editText!!.error = smdp.editText!!.text?.let { check(Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()) {
if (Patterns.DOMAIN_NAME.matcher(smdp.editText!!.text).matches()) return@let null "Invalid SM-DP+ address"
getString(R.string.download_wizard_error_invalid_address_format)
} }
imei.editText!!.error = imei.editText!!.text?.let { check(imei.editText!!.text.let { it.isEmpty() || isValidIMEI(it) }) {
if (it.isEmpty() || luhnValid(it)) return@let null "Invalid IMEI"
getString(R.string.download_wizard_error_invalid_imei_format)
} }
} }
} }
private fun luhnValid(number: CharSequence, mod: Int = 10): Boolean { private fun isValidIMEI(input: CharSequence): Boolean {
if (!number.all(Char::isDigit)) return false if (input.length != 15 || !input.all(Char::isDigit)) return false
var checksum = 0
for (i in number.length - 1 downTo 0 step 2) { fun sumOfDigits(input: Int): Int {
checksum += number[i] - '0' if (input % 2 == 0) return input
return (input * 2).toString().map(Char::digitToInt).sum()
} }
for (i in number.length - 2 downTo 0 step 2) {
val n: Int = (number[i] - '0') * 2 val sum = input.dropLast(1).map(Char::digitToInt).sumOf(::sumOfDigits)
checksum += if (n > 9) n - 9 else n return (sum * 9) % 10 == input.last().digitToInt()
}
return checksum % mod == 0
} }

View file

@ -98,8 +98,6 @@
<string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string> <string name="download_wizard_diagnostics_last_apdu_exception">Last APDU exception:</string>
<string name="download_wizard_diagnostics_save">Save</string> <string name="download_wizard_diagnostics_save">Save</string>
<string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string> <string name="download_wizard_diagnostics_file_template">Diagnostics at %s</string>
<string name="download_wizard_error_invalid_address_format">Invalid SM-DP+ address</string>
<string name="download_wizard_error_invalid_imei_format">Invalid IMEI format</string>
<string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string> <string name="logs_saved_message">Logs have been saved to the selected path. Would you like to share the log through another app?</string>