Created
March 9, 2016 19:10
-
-
Save dnorton/449081fc3680e6d06364 to your computer and use it in GitHub Desktop.
A small Kotlin file to generate a passphrase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
Author
dnorton
commented
Mar 9, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment