From 549ee790d3e52bc62565ddf92e6a556e98b5195e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 15 Jul 2022 00:53:48 +0530 Subject: all: re-do package structure yet again --- passgen/diceware/build.gradle.kts | 2 +- .../diceware/DicewarePassphraseGenerator.kt | 39 ++++++++++++++++++ .../app/passwordstore/passgen/diceware/Die.kt | 30 ++++++++++++++ .../passgen/diceware/RandomIntGenerator.kt | 15 +++++++ .../passgen/diceware/WordListParser.kt | 21 ++++++++++ .../diceware/DicewarePassphraseGenerator.kt | 39 ------------------ .../dev/msfjarvis/aps/passgen/diceware/Die.kt | 30 -------------- .../aps/passgen/diceware/RandomIntGenerator.kt | 15 ------- .../aps/passgen/diceware/WordListParser.kt | 21 ---------- .../diceware/DicewarePassphraseGeneratorTest.kt | 29 +++++++++++++ .../app/passwordstore/passgen/diceware/DieTest.kt | 47 ++++++++++++++++++++++ .../passgen/diceware/WordListParserTest.kt | 37 +++++++++++++++++ .../diceware/DicewarePassphraseGeneratorTest.kt | 29 ------------- .../dev/msfjarvis/aps/passgen/diceware/DieTest.kt | 47 ---------------------- .../aps/passgen/diceware/WordListParserTest.kt | 37 ----------------- 15 files changed, 219 insertions(+), 219 deletions(-) create mode 100644 passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGenerator.kt create mode 100644 passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/Die.kt create mode 100644 passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/RandomIntGenerator.kt create mode 100644 passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/WordListParser.kt delete mode 100644 passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt delete mode 100644 passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/Die.kt delete mode 100644 passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/RandomIntGenerator.kt delete mode 100644 passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt create mode 100644 passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGeneratorTest.kt create mode 100644 passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DieTest.kt create mode 100644 passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/WordListParserTest.kt delete mode 100644 passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGeneratorTest.kt delete mode 100644 passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt delete mode 100644 passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt (limited to 'passgen/diceware') diff --git a/passgen/diceware/build.gradle.kts b/passgen/diceware/build.gradle.kts index 40c703c5..f612b691 100644 --- a/passgen/diceware/build.gradle.kts +++ b/passgen/diceware/build.gradle.kts @@ -11,7 +11,7 @@ plugins { android { sourceSets { getByName("test") { resources.srcDir("src/main/res/raw") } } - namespace = "dev.msfjarvis.aps.passgen.diceware" + namespace = "app.passwordstore.passgen.diceware" } dependencies { diff --git a/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGenerator.kt b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGenerator.kt new file mode 100644 index 00000000..841eb31b --- /dev/null +++ b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGenerator.kt @@ -0,0 +1,39 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import java.io.InputStream +import javax.inject.Inject + +/** + * Password generator implementing the Diceware passphrase generation mechanism. For detailed + * information on how this works, see https://theworld.com/~reinhold/diceware.html. + */ +public class DicewarePassphraseGenerator +@Inject +constructor( + private val die: Die, + wordList: InputStream, +) { + + private val wordMap = WordListParser.parse(wordList) + + /** Generates a passphrase with [wordCount] words. */ + public fun generatePassphrase(wordCount: Int, separator: Char): String { + return buildString { + repeat(wordCount) { idx -> + append(wordMap[die.rollMultiple(DIGITS)]) + if (idx < wordCount - 1) append(separator) + } + } + } + + private companion object { + + /** Number of digits used by indices in the default wordlist. */ + const val DIGITS: Int = 5 + } +} diff --git a/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/Die.kt b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/Die.kt new file mode 100644 index 00000000..e3db4b80 --- /dev/null +++ b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/Die.kt @@ -0,0 +1,30 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import javax.inject.Inject + +/** Basic implementation of a die with configurable number of sides. */ +public class Die +@Inject +constructor( + private val sides: Int, + private val random: RandomIntGenerator, +) { + + /** Roll the die to return a single number. */ + public fun roll(): Int { + return random.get(1..sides) + } + + /** + * Roll the die multiple times, concatenating each result to obtain a number with [iterations] + * digits. + */ + public fun rollMultiple(iterations: Int): Int { + return StringBuilder().apply { repeat(iterations) { append(roll()) } }.toString().toInt() + } +} diff --git a/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/RandomIntGenerator.kt b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/RandomIntGenerator.kt new file mode 100644 index 00000000..18a828a6 --- /dev/null +++ b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/RandomIntGenerator.kt @@ -0,0 +1,15 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +/** + * SAM interface that takes in an [IntRange] and returns a randomly chosen [Int] within its bounds. + * This is used as a replacement for [kotlin.random.Random] since there is no CSPRNG-backed + * implementation of it in the Kotlin stdlib. + */ +public fun interface RandomIntGenerator { + public fun get(range: IntRange): Int +} diff --git a/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/WordListParser.kt b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/WordListParser.kt new file mode 100644 index 00000000..6c7f5310 --- /dev/null +++ b/passgen/diceware/src/main/kotlin/app/passwordstore/passgen/diceware/WordListParser.kt @@ -0,0 +1,21 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import java.io.InputStream + +internal object WordListParser { + fun parse(wordlistStream: InputStream) = + wordlistStream + .bufferedReader() + .lineSequence() + .map { line -> line.split(DELIMITER) } + .filter { items -> items.size == 2 && items[0].toIntOrNull() != null } + .map { items -> items[0].toInt() to items[1] } + .toMap() + + private const val DELIMITER = "\t" +} diff --git a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt deleted file mode 100644 index 62e3b97b..00000000 --- a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import java.io.InputStream -import javax.inject.Inject - -/** - * Password generator implementing the Diceware passphrase generation mechanism. For detailed - * information on how this works, see https://theworld.com/~reinhold/diceware.html. - */ -public class DicewarePassphraseGenerator -@Inject -constructor( - private val die: Die, - wordList: InputStream, -) { - - private val wordMap = WordListParser.parse(wordList) - - /** Generates a passphrase with [wordCount] words. */ - public fun generatePassphrase(wordCount: Int, separator: Char): String { - return buildString { - repeat(wordCount) { idx -> - append(wordMap[die.rollMultiple(DIGITS)]) - if (idx < wordCount - 1) append(separator) - } - } - } - - private companion object { - - /** Number of digits used by indices in the default wordlist. */ - const val DIGITS: Int = 5 - } -} diff --git a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/Die.kt b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/Die.kt deleted file mode 100644 index d6e5aa23..00000000 --- a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/Die.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import javax.inject.Inject - -/** Basic implementation of a die with configurable number of sides. */ -public class Die -@Inject -constructor( - private val sides: Int, - private val random: RandomIntGenerator, -) { - - /** Roll the die to return a single number. */ - public fun roll(): Int { - return random.get(1..sides) - } - - /** - * Roll the die multiple times, concatenating each result to obtain a number with [iterations] - * digits. - */ - public fun rollMultiple(iterations: Int): Int { - return StringBuilder().apply { repeat(iterations) { append(roll()) } }.toString().toInt() - } -} diff --git a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/RandomIntGenerator.kt b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/RandomIntGenerator.kt deleted file mode 100644 index 7f957419..00000000 --- a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/RandomIntGenerator.kt +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -/** - * SAM interface that takes in an [IntRange] and returns a randomly chosen [Int] within its bounds. - * This is used as a replacement for [kotlin.random.Random] since there is no CSPRNG-backed - * implementation of it in the Kotlin stdlib. - */ -public fun interface RandomIntGenerator { - public fun get(range: IntRange): Int -} diff --git a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt deleted file mode 100644 index 5351aaa1..00000000 --- a/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParser.kt +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import java.io.InputStream - -internal object WordListParser { - fun parse(wordlistStream: InputStream) = - wordlistStream - .bufferedReader() - .lineSequence() - .map { line -> line.split(DELIMITER) } - .filter { items -> items.size == 2 && items[0].toIntOrNull() != null } - .map { items -> items[0].toInt() to items[1] } - .toMap() - - private const val DELIMITER = "\t" -} diff --git a/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGeneratorTest.kt b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGeneratorTest.kt new file mode 100644 index 00000000..69febaf1 --- /dev/null +++ b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DicewarePassphraseGeneratorTest.kt @@ -0,0 +1,29 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import kotlin.random.Random +import kotlin.test.assertEquals +import org.junit.Test + +class DicewarePassphraseGeneratorTest { + /** Pre-seeded [Random] instance to ensure tests are deterministic. */ + private val random = Random(1_00_000) + + private val intGenerator = RandomIntGenerator { it.random(random) } + @Test + fun generatePassphrase() { + val die = Die(6, intGenerator) + + val generator = + DicewarePassphraseGenerator( + die, + WordListParserTest.getDefaultWordList(), + ) + + assertEquals("salvation_cozily_croon_trustee_fidgety", generator.generatePassphrase(5, '_')) + } +} diff --git a/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DieTest.kt b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DieTest.kt new file mode 100644 index 00000000..725d80f7 --- /dev/null +++ b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/DieTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import kotlin.random.Random +import kotlin.test.Test +import kotlin.test.assertEquals + +class DieTest { + + /** Pre-seeded [Random] instance to ensure tests are deterministic. */ + private val random = Random(1_00_000) + + private val intGenerator = RandomIntGenerator { it.random(random) } + + @Test + fun oneRoll() { + val die = Die(6, intGenerator) + assertEquals(5, die.roll()) + } + + @Test + fun multipleRolls() { + val die = Die(6, intGenerator) + assertEquals(526242, die.rollMultiple(6)) + } + + @Test + fun consecutiveRolls() { + val die = Die(6, intGenerator) + assertEquals(5, die.roll()) + assertEquals(2, die.roll()) + assertEquals(6, die.roll()) + assertEquals(2, die.roll()) + assertEquals(4, die.roll()) + assertEquals(2, die.roll()) + } + + @Test + fun hundredSides() { + val die = Die(100, intGenerator) + assertEquals(67, die.roll()) + } +} diff --git a/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/WordListParserTest.kt b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/WordListParserTest.kt new file mode 100644 index 00000000..bcb85cb4 --- /dev/null +++ b/passgen/diceware/src/test/kotlin/app/passwordstore/passgen/diceware/WordListParserTest.kt @@ -0,0 +1,37 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.passgen.diceware + +import java.io.InputStream +import kotlin.test.Test +import kotlin.test.assertEquals + +class WordListParserTest { + @Test + fun parseWordList() { + val stream = "11111\tabcde\n22222\tfghij".byteInputStream() + val parsedMap = WordListParser.parse(stream) + assertEquals(2, parsedMap.size) + assertEquals("abcde", parsedMap[11111]) + assertEquals("fghij", parsedMap[22222]) + } + + @Test + fun parseDefaultWordList() { + val wordlist = getDefaultWordList() + val parsedMap = WordListParser.parse(wordlist) + assertEquals(7776, parsedMap.size) + assertEquals("zoom", parsedMap[66666]) + assertEquals("salute", parsedMap[52621]) + } + + companion object { + fun getDefaultWordList(): InputStream { + return requireNotNull(this::class.java.classLoader) + .getResourceAsStream("diceware_wordlist.txt") + } + } +} diff --git a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGeneratorTest.kt b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGeneratorTest.kt deleted file mode 100644 index 236be708..00000000 --- a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGeneratorTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import kotlin.random.Random -import kotlin.test.assertEquals -import org.junit.Test - -class DicewarePassphraseGeneratorTest { - /** Pre-seeded [Random] instance to ensure tests are deterministic. */ - private val random = Random(1_00_000) - - private val intGenerator = RandomIntGenerator { it.random(random) } - @Test - fun generatePassphrase() { - val die = Die(6, intGenerator) - - val generator = - DicewarePassphraseGenerator( - die, - WordListParserTest.getDefaultWordList(), - ) - - assertEquals("salvation_cozily_croon_trustee_fidgety", generator.generatePassphrase(5, '_')) - } -} diff --git a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt deleted file mode 100644 index 7f6398f6..00000000 --- a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import kotlin.random.Random -import kotlin.test.Test -import kotlin.test.assertEquals - -class DieTest { - - /** Pre-seeded [Random] instance to ensure tests are deterministic. */ - private val random = Random(1_00_000) - - private val intGenerator = RandomIntGenerator { it.random(random) } - - @Test - fun oneRoll() { - val die = Die(6, intGenerator) - assertEquals(5, die.roll()) - } - - @Test - fun multipleRolls() { - val die = Die(6, intGenerator) - assertEquals(526242, die.rollMultiple(6)) - } - - @Test - fun consecutiveRolls() { - val die = Die(6, intGenerator) - assertEquals(5, die.roll()) - assertEquals(2, die.roll()) - assertEquals(6, die.roll()) - assertEquals(2, die.roll()) - assertEquals(4, die.roll()) - assertEquals(2, die.roll()) - } - - @Test - fun hundredSides() { - val die = Die(100, intGenerator) - assertEquals(67, die.roll()) - } -} diff --git a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt deleted file mode 100644 index 9364085d..00000000 --- a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/WordListParserTest.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.passgen.diceware - -import java.io.InputStream -import kotlin.test.Test -import kotlin.test.assertEquals - -class WordListParserTest { - @Test - fun parseWordList() { - val stream = "11111\tabcde\n22222\tfghij".byteInputStream() - val parsedMap = WordListParser.parse(stream) - assertEquals(2, parsedMap.size) - assertEquals("abcde", parsedMap[11111]) - assertEquals("fghij", parsedMap[22222]) - } - - @Test - fun parseDefaultWordList() { - val wordlist = getDefaultWordList() - val parsedMap = WordListParser.parse(wordlist) - assertEquals(7776, parsedMap.size) - assertEquals("zoom", parsedMap[66666]) - assertEquals("salute", parsedMap[52621]) - } - - companion object { - fun getDefaultWordList(): InputStream { - return requireNotNull(this::class.java.classLoader) - .getResourceAsStream("diceware_wordlist.txt") - } - } -} -- cgit v1.2.3