Skip to content

Instantly share code, notes, and snippets.

@dnorton
Created March 9, 2016 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnorton/449081fc3680e6d06364 to your computer and use it in GitHub Desktop.
Save dnorton/449081fc3680e6d06364 to your computer and use it in GitHub Desktop.
A small Kotlin file to generate a passphrase
package com.velogica.passwords
import org.apache.http.client.fluent.Request
import java.util.*
/**
* Take a list of words, extract three at random, do some switcheraoos to the last word
*/
class PasswordGenerator {
val wordsFileRaw = "https://enter_your_url_to_words/funny_words.txt"
fun getWords(): List<String> {
val response = Request.Get(wordsFileRaw).execute().returnContent()
return response.asString().split('\n')
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
val passwordGenerator = PasswordGenerator()
val passwordList = passwordGenerator.getWords().pick(3)
val mutableList = passwordList.toMutableList()
mutableList[passwordList.lastIndex] = passwordize(passwordList.last())
println(mutableList.joinToString(" "))
}
}
}
fun <T> List<T>.pick(num: Int): List<T> {
Collections.shuffle(this)
return this.subList(0, num)
}
fun passwordize(source : String) : String {
val map = mapOf("o" to "0", "i" to "1", "e" to "3", "a" to "8")
var resultString = source.capitalize()
for ((a,b) in map) {
resultString = resultString.replaceFirst(a, b)
}
return resultString
}
@dnorton
Copy link
Author

dnorton commented Mar 9, 2016

<dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.5.2</version>
        </dependency>
    </dependencies>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment