Skip to content

Instantly share code, notes, and snippets.

@retrospectacus
Last active February 2, 2019 00:52
Show Gist options
  • Save retrospectacus/1d45709998e7b34217e4292c71755e66 to your computer and use it in GitHub Desktop.
Save retrospectacus/1d45709998e7b34217e4292c71755e66 to your computer and use it in GitHub Desktop.
import java.time.{Duration, ZonedDateTime}
object BruteFast extends App {
val startTime = ZonedDateTime.now
val theCorrectPassword = "ordaindrooppodlovelyemberlogic"
// val theCorrectPassword = "horsebutt"
// val dictionary = scala.io.Source.fromFile("/usr/share/dict/words").getLines.toVector
val dictionary = Vector("ordain","droop","pod","lovely","ember","logic")
var guesses = 0L
// single word
def oneWordPassphrase = dictionary.iterator
def twoWordPassphrase = for {
w1 <- dictionary.iterator
w2 <- dictionary.iterator
} yield w1+w2
def threeWordPassphrase = for {
w1 <- dictionary.iterator
w2 <- dictionary.iterator
w3 <- dictionary.iterator
} yield w1+w2+w3
def fourWordPassphrase = for {
w1 <- dictionary.iterator
w2 <- dictionary.iterator
w3 <- dictionary.iterator
w4 <- dictionary.iterator
} yield w1+w2+w3+w4
def fiveWordPassphrase = for {
w1 <- dictionary.iterator
w2 <- dictionary.iterator
w3 <- dictionary.iterator
w4 <- dictionary.iterator
w5 <- dictionary.iterator
} yield w1+w2+w3+w4+w5
def sixWordPassphrase = for {
w1 <- dictionary.iterator
w2 <- dictionary.iterator
w3 <- dictionary.iterator
w4 <- dictionary.iterator
w5 <- dictionary.iterator
w6 <- dictionary.iterator
} yield w1+w2+w3+w4+w5+w6
(oneWordPassphrase ++ twoWordPassphrase ++ threeWordPassphrase ++ fourWordPassphrase ++ fiveWordPassphrase ++ sixWordPassphrase)
.find { w =>
guesses = guesses + 1
theCorrectPassword == w
}
.foreach { pp =>
val s = Duration.between(startTime, ZonedDateTime.now).getSeconds
println(s"Took $s seconds to guess $pp correctly ($guesses guesses)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment