diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2021-03-09 14:53:11 +0530 |
|---|---|---|
| committer | Harsh Shandilya <me@msfjarvis.dev> | 2021-03-18 11:40:30 +0530 |
| commit | 774fda83ac276a635e3402034b1eedbd10be916f (patch) | |
| tree | 57783a953fcd193d32f1e3dc8df26e07f03df328 /openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt | |
| parent | be31ae37f443982b377d027cd613f04d0926568d (diff) | |
| download | HardenedPass-774fda83ac276a635e3402034b1eedbd10be916f.tar.gz HardenedPass-774fda83ac276a635e3402034b1eedbd10be916f.zip | |
all: reformat with ktfmt
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt')
| -rw-r--r-- | openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt | 165 |
1 files changed, 79 insertions, 86 deletions
diff --git a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt index 03ee11fc..f277615e 100644 --- a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt +++ b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpUtils.kt @@ -12,103 +12,96 @@ import java.util.regex.Pattern public object OpenPgpUtils { - private val PGP_MESSAGE: Pattern = Pattern.compile( - ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", - Pattern.DOTALL + private val PGP_MESSAGE: Pattern = + Pattern.compile(".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL) + private val PGP_SIGNED_MESSAGE: Pattern = + Pattern.compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL ) - private val PGP_SIGNED_MESSAGE: Pattern = Pattern.compile( - ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", - Pattern.DOTALL - ) - private val USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$") - private val EMAIL_PATTERN = Pattern.compile("^<?\"?([^<>\"]*@[^<>\"]*\\.[^<>\"]*)\"?>?$") - public const val PARSE_RESULT_NO_PGP: Int = -1 - public const val PARSE_RESULT_MESSAGE: Int = 0 - public const val PARSE_RESULT_SIGNED_MESSAGE: Int = 1 + private val USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$") + private val EMAIL_PATTERN = Pattern.compile("^<?\"?([^<>\"]*@[^<>\"]*\\.[^<>\"]*)\"?>?$") + public const val PARSE_RESULT_NO_PGP: Int = -1 + public const val PARSE_RESULT_MESSAGE: Int = 0 + public const val PARSE_RESULT_SIGNED_MESSAGE: Int = 1 - public fun parseMessage(message: String): Int { - val matcherSigned = PGP_SIGNED_MESSAGE.matcher(message) - val matcherMessage = PGP_MESSAGE.matcher(message) - return when { - matcherMessage.matches() -> PARSE_RESULT_MESSAGE - matcherSigned.matches() -> PARSE_RESULT_SIGNED_MESSAGE - else -> PARSE_RESULT_NO_PGP - } + public fun parseMessage(message: String): Int { + val matcherSigned = PGP_SIGNED_MESSAGE.matcher(message) + val matcherMessage = PGP_MESSAGE.matcher(message) + return when { + matcherMessage.matches() -> PARSE_RESULT_MESSAGE + matcherSigned.matches() -> PARSE_RESULT_SIGNED_MESSAGE + else -> PARSE_RESULT_NO_PGP } + } - public fun isAvailable(context: Context): Boolean { - val intent = Intent(OpenPgpApi.SERVICE_INTENT_2) - val resInfo = - context.packageManager.queryIntentServices(intent, 0) - return resInfo.isNotEmpty() - } + public fun isAvailable(context: Context): Boolean { + val intent = Intent(OpenPgpApi.SERVICE_INTENT_2) + val resInfo = context.packageManager.queryIntentServices(intent, 0) + return resInfo.isNotEmpty() + } - public fun convertKeyIdToHex(keyId: Long): String { - return "0x" + convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit( - keyId - ) - } + public fun convertKeyIdToHex(keyId: Long): String { + return "0x" + convertKeyIdToHex32bit(keyId shr 32) + convertKeyIdToHex32bit(keyId) + } - private fun convertKeyIdToHex32bit(keyId: Long): String { - var hexString = - java.lang.Long.toHexString(keyId and 0xffffffffL).toLowerCase(Locale.ENGLISH) - while (hexString.length < 8) { - hexString = "0$hexString" - } - return hexString + private fun convertKeyIdToHex32bit(keyId: Long): String { + var hexString = java.lang.Long.toHexString(keyId and 0xffffffffL).toLowerCase(Locale.ENGLISH) + while (hexString.length < 8) { + hexString = "0$hexString" } + return hexString + } - /** - * Splits userId string into naming part, email part, and comment part. - * See SplitUserIdTest for examples. - */ - public fun splitUserId(userId: String): UserId { - if (userId.isNotEmpty()) { - val matcher = USER_ID_PATTERN.matcher(userId) - if (matcher.matches()) { - var name = if (matcher.group(1).isEmpty()) null else matcher.group(1) - val comment = matcher.group(2) - var email = matcher.group(3) - if (email != null && name != null) { - val emailMatcher = EMAIL_PATTERN.matcher(name) - if (emailMatcher.matches() && email == emailMatcher.group(1)) { - email = emailMatcher.group(1) - name = null - } - } - if (email == null && name != null) { - val emailMatcher = EMAIL_PATTERN.matcher(name) - if (emailMatcher.matches()) { - email = emailMatcher.group(1) - name = null - } - } - return UserId(name, email, comment) - } + /** + * Splits userId string into naming part, email part, and comment part. See SplitUserIdTest for + * examples. + */ + public fun splitUserId(userId: String): UserId { + if (userId.isNotEmpty()) { + val matcher = USER_ID_PATTERN.matcher(userId) + if (matcher.matches()) { + var name = if (matcher.group(1).isEmpty()) null else matcher.group(1) + val comment = matcher.group(2) + var email = matcher.group(3) + if (email != null && name != null) { + val emailMatcher = EMAIL_PATTERN.matcher(name) + if (emailMatcher.matches() && email == emailMatcher.group(1)) { + email = emailMatcher.group(1) + name = null + } + } + if (email == null && name != null) { + val emailMatcher = EMAIL_PATTERN.matcher(name) + if (emailMatcher.matches()) { + email = emailMatcher.group(1) + name = null + } } - return UserId(null, null, null) + return UserId(name, email, comment) + } } + return UserId(null, null, null) + } - /** - * Returns a composed user id. Returns null if name, email and comment are empty. - */ - public fun createUserId(userId: UserId): String? { - val userIdBuilder = StringBuilder() - if (!userId.name.isNullOrEmpty()) { - userIdBuilder.append(userId.name) - } - if (!userId.comment.isNullOrEmpty()) { - userIdBuilder.append(" (") - userIdBuilder.append(userId.comment) - userIdBuilder.append(")") - } - if (!userId.email.isNullOrEmpty()) { - userIdBuilder.append(" <") - userIdBuilder.append(userId.email) - userIdBuilder.append(">") - } - return if (userIdBuilder.isEmpty()) null else userIdBuilder.toString() + /** Returns a composed user id. Returns null if name, email and comment are empty. */ + public fun createUserId(userId: UserId): String? { + val userIdBuilder = StringBuilder() + if (!userId.name.isNullOrEmpty()) { + userIdBuilder.append(userId.name) + } + if (!userId.comment.isNullOrEmpty()) { + userIdBuilder.append(" (") + userIdBuilder.append(userId.comment) + userIdBuilder.append(")") + } + if (!userId.email.isNullOrEmpty()) { + userIdBuilder.append(" <") + userIdBuilder.append(userId.email) + userIdBuilder.append(">") } + return if (userIdBuilder.isEmpty()) null else userIdBuilder.toString() + } - public class UserId(public val name: String?, public val email: String?, public val comment: String?) : Serializable + public class UserId(public val name: String?, public val email: String?, public val comment: String?) : Serializable } |
