aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd
diff options
context:
space:
mode:
authorFabian Henneke <FabianHenneke@users.noreply.github.com>2020-06-18 12:04:33 +0200
committerGitHub <noreply@github.com>2020-06-18 12:04:33 +0200
commit33b3f54921bba7549933c6f889fbe7ab63b61b7c (patch)
tree0c79a1bf93a0094657fc497efc80ae2c1affb518 /app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd
parente25e0035a2e4d6227f9d8071687e421e784186a4 (diff)
downloadHardenedPass-33b3f54921bba7549933c6f889fbe7ab63b61b7c.tar.gz
HardenedPass-33b3f54921bba7549933c6f889fbe7ab63b61b7c.zip
Refactor password generation (#860)
* Refactor password generation * Update Extensions.kt * Update app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt Co-authored-by: Harsh Shandilya <me@msfjarvis.dev> * Address review comments Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt65
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt48
2 files changed, 33 insertions, 80 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
index 81aa4362..0f5dcb1c 100644
--- a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
@@ -6,11 +6,11 @@ package com.zeapo.pwdstore.pwgenxkpwd
import android.content.Context
import com.zeapo.pwdstore.R
-import com.zeapo.pwdstore.pwgen.PasswordGenerator
-import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorExeption
+import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
+import com.zeapo.pwdstore.pwgen.secureRandomCharacter
+import com.zeapo.pwdstore.pwgen.secureRandomElement
+import com.zeapo.pwdstore.pwgen.secureRandomNumber
import java.io.IOException
-import java.security.SecureRandom
-import java.util.ArrayList
import java.util.Locale
class PasswordBuilder(ctx: Context) {
@@ -67,29 +67,25 @@ class PasswordBuilder(ctx: Context) {
}
private fun generateRandomNumberSequence(totalNumbers: Int): String {
- val secureRandom = SecureRandom()
val numbers = StringBuilder(totalNumbers)
-
for (i in 0 until totalNumbers) {
- numbers.append(secureRandom.nextInt(10))
+ numbers.append(secureRandomNumber(10))
}
return numbers.toString()
}
private fun generateRandomSymbolSequence(numSymbols: Int): String {
- val secureRandom = SecureRandom()
val numbers = StringBuilder(numSymbols)
-
for (i in 0 until numSymbols) {
- numbers.append(SYMBOLS[secureRandom.nextInt(SYMBOLS.length)])
+ numbers.append(SYMBOLS.secureRandomCharacter())
}
return numbers.toString()
}
- @Throws(PasswordGenerator.PasswordGeneratorExeption::class)
+ @OptIn(ExperimentalStdlibApi::class)
+ @Throws(PasswordGeneratorException::class)
fun create(): String {
- val wordBank = ArrayList<String>()
- val secureRandom = SecureRandom()
+ val wordBank = mutableListOf<String>()
val password = StringBuilder()
if (prependDigits != 0) {
@@ -101,44 +97,30 @@ class PasswordBuilder(ctx: Context) {
try {
val dictionary = XkpwdDictionary(context)
val words = dictionary.words
- for (wordLength in words.keys) {
- if (wordLength in minWordLength..maxWordLength) {
- wordBank.addAll(words[wordLength]!!)
- }
+ for (wordLength in minWordLength..maxWordLength) {
+ wordBank.addAll(words[wordLength] ?: emptyList())
}
if (wordBank.size == 0) {
- throw PasswordGeneratorExeption(context.getString(R.string.xkpwgen_builder_error, minWordLength, maxWordLength))
+ throw PasswordGeneratorException(context.getString(R.string.xkpwgen_builder_error, minWordLength, maxWordLength))
}
for (i in 0 until numWords) {
- val randomIndex = secureRandom.nextInt(wordBank.size)
- var s = wordBank[randomIndex]
-
- if (capsType != CapsType.As_iS) {
- s = s.toLowerCase(Locale.getDefault())
- when (capsType) {
- CapsType.UPPERCASE -> s = s.toUpperCase(Locale.getDefault())
- CapsType.Sentencecase -> {
- if (i == 0) {
- s = capitalize(s)
- }
- }
- CapsType.TitleCase -> {
- s = capitalize(s)
- }
- CapsType.lowercase, CapsType.As_iS -> {
- }
- }
+ val candidate = wordBank.secureRandomElement()
+ val s = when (capsType) {
+ CapsType.UPPERCASE -> candidate.toUpperCase(Locale.getDefault())
+ CapsType.Sentencecase -> if (i == 0) candidate.capitalize(Locale.getDefault()) else candidate
+ CapsType.TitleCase -> candidate.capitalize(Locale.getDefault())
+ CapsType.lowercase -> candidate.toLowerCase(Locale.getDefault())
+ CapsType.As_iS -> candidate
}
password.append(s)
- wordBank.removeAt(randomIndex)
if (i + 1 < numWords) {
password.append(separator)
}
}
} catch (e: IOException) {
- throw PasswordGeneratorExeption("Failed generating password!")
+ throw PasswordGeneratorException("Failed generating password!")
}
if (numDigits != 0) {
if (isAppendNumberSeparator) {
@@ -155,13 +137,6 @@ class PasswordBuilder(ctx: Context) {
return password.toString()
}
- private fun capitalize(s: String): String {
- var result = s
- val lower = result.toLowerCase(Locale.getDefault())
- result = lower.substring(0, 1).toUpperCase(Locale.getDefault()) + result.substring(1)
- return result
- }
-
companion object {
private const val SYMBOLS = "!@\$%^&*-_+=:|~?/.;#"
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt
index 6a3fbcc3..92438ed0 100644
--- a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/XkpwdDictionary.kt
@@ -5,51 +5,29 @@
package com.zeapo.pwdstore.pwgenxkpwd
import android.content.Context
-import android.text.TextUtils
import androidx.preference.PreferenceManager
import com.zeapo.pwdstore.R
import java.io.File
-import java.util.ArrayList
-import java.util.HashMap
class XkpwdDictionary(context: Context) {
- val words: HashMap<Int, ArrayList<String>> = HashMap()
+ val words: Map<Int, List<String>>
init {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
-
- var lines: List<String> = listOf()
-
- if (prefs.getBoolean("pref_key_is_custom_dict", false)) {
-
- val uri = prefs.getString("pref_key_custom_dict", "")
-
- if (!TextUtils.isEmpty(uri)) {
- val customDictFile = File(context.filesDir, XKPWD_CUSTOM_DICT_FILE)
-
- if (customDictFile.exists() && customDictFile.canRead()) {
- lines = customDictFile.inputStream().bufferedReader().readLines()
- }
- }
+ val uri = prefs.getString("pref_key_custom_dict", "")!!
+ val customDictFile = File(context.filesDir, XKPWD_CUSTOM_DICT_FILE)
+
+ val lines = if (prefs.getBoolean("pref_key_is_custom_dict", false) &&
+ uri.isNotEmpty() && customDictFile.canRead()) {
+ customDictFile.readLines()
+ } else {
+ context.resources.openRawResource(R.raw.xkpwdict).bufferedReader().readLines()
}
- if (lines.isEmpty()) {
- lines = context.resources.openRawResource(R.raw.xkpwdict).bufferedReader().readLines()
- }
-
- for (word in lines) {
- if (!word.trim { it <= ' ' }.contains(" ")) {
- val length = word.trim { it <= ' ' }.length
-
- if (length > 0) {
- if (!words.containsKey(length)) {
- words[length] = ArrayList()
- }
- val strings = words[length]!!
- strings.add(word.trim { it <= ' ' })
- }
- }
- }
+ words = lines.asSequence()
+ .map { it.trim() }
+ .filter { it.isNotEmpty() && !it.contains(' ') }
+ .groupBy { it.length }
}
companion object {