Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:29
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 dacr/8c2e7ca89f8a37ae09c85ddea162aaf8 to your computer and use it in GitHub Desktop.
Save dacr/8c2e7ca89f8a37ae09c85ddea162aaf8 to your computer and use it in GitHub Desktop.
all possible solutions generator, can be used to brute force passwords, hashes, ... / published by https://github.com/dacr/code-examples-manager #768484fa-7034-4e6d-9c7e-598d2bc8673a/4f22ce93d4a3f9529ae3b4cbdea67f6dc7fae243
// summary : all possible solutions generator, can be used to brute force passwords, hashes, ...
// keywords : scala, algorithm, bruteforce, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 768484fa-7034-4e6d-9c7e-598d2bc8673a
// created-on : 2020-04-07T20:29:03Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
import org.scalatest._, flatspec._, matchers._
type Fragment = Byte
type Solution = Array[Fragment]
case class Alphabet(fragments: Array[Fragment]) {
val size = fragments.size
def head:Fragment = fragments.head
def apply(index: Int):Fragment = fragments(index)
}
case class SolutionContext(
solutionLength: Int,
alphabet: Alphabet) {
def possibleSolutionsCount():BigInt = BigInt(alphabet.size).pow(solutionLength)
}
case class SolutionGenerator(context: SolutionContext) {
def generator(): Iterator[Solution] = {
val currentSolutionAlphabetIndices = Array.fill(context.solutionLength)(0)
def inc(): Boolean = {
@annotation.tailrec
def incworker(position: Int): Boolean = {
if (position == context.solutionLength) false else {
if (currentSolutionAlphabetIndices(position) < context.alphabet.size - 1) {
currentSolutionAlphabetIndices(position) += 1
true
} else {
currentSolutionAlphabetIndices(position) = 0
incworker(position + 1)
}
}
}
incworker(0)
}
new Iterator[Solution] {
private var status: Boolean = true
def hasNext: Boolean = status
def next(): Solution = {
val newone = Array.ofDim[Byte](context.solutionLength)
for(i <- 0 until context.solutionLength) newone(i)=context.alphabet(currentSolutionAlphabetIndices(i))
status = inc()
newone
}
}
}
}
class GeneratorTest extends AnyFlatSpec with should.Matchers {
override def suiteName: String = "GeneratorTest"
"SolutionGenerator" should "generates the right possible solutions" in {
val context = SolutionContext(2, Alphabet(Array('a', 'b')))
val generator = SolutionGenerator(context).generator()
context.possibleSolutionsCount() shouldBe 4
generator.toList.map(_.map(_.toChar).mkString) shouldBe List("aa", "ba", "ab", "bb")
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[GeneratorTest].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment