Last active
May 25, 2024 08:39
-
-
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/cc90b36b24aa72797fdc239b151190a5c91b6602
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
// 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.4.2" | |
//> 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