diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2021-12-10 13:59:32 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2021-12-21 13:01:40 +0530 |
commit | c5436b543df466657a1d0dc3af40d4707fca9b9d (patch) | |
tree | a5b04250886153f0e9d4e1bba0867fbb8332fe04 /passgen/diceware/src/main/kotlin/dev | |
parent | ab8f6a43eefab012900edd1ad9dbefd9a49b7429 (diff) |
diceware: add passphrase generator
Diffstat (limited to 'passgen/diceware/src/main/kotlin/dev')
-rw-r--r-- | passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt | 42 |
1 files changed, 42 insertions, 0 deletions
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 new file mode 100644 index 00000000..ee18352e --- /dev/null +++ b/passgen/diceware/src/main/kotlin/dev/msfjarvis/aps/passgen/diceware/DicewarePassphraseGenerator.kt @@ -0,0 +1,42 @@ +/* + * 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 StringBuilder() + .apply { + repeat(wordCount) { idx -> + append(wordMap[die.rollMultiple(DIGITS)]) + if (idx < wordCount - 1) append(separator) + } + } + .toString() + .trimEnd() + } + + private companion object { + + /** Number of digits used by indices in the default wordlist. */ + const val DIGITS: Int = 5 + } +} |