Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
flexible bruteforcer skeleton in Golang
package main
import "fmt"
type state struct {
digit [2]int
negative bool
}
const MAX_NUMBER = 16
// returns a function that manipulates the digit at given pos
func digitManipulator(pos int) func(*state) bool {
return func(s *state) bool {
s.digit[pos]++
if s.digit[pos] == MAX_NUMBER {
s.digit[pos] = 0
return true
}
return false
}
}
func manipulateNegative(s *state) bool {
s.negative = !s.negative
// stop when negative once again becomes false
return !s.negative
}
// controls the order in which state is manipulated
var stateManipulators = []func(*state) bool{
digitManipulator(1), // 2nd digit
digitManipulator(0), // 1st digit
manipulateNegative, // sign
}
// manipulates the state
func nextState(s *state) bool {
i := 0
for ; i < len(stateManipulators); i++ {
if !stateManipulators[i](s) {
break
}
}
return i == len(stateManipulators)
}
func stateFormatter(s *state) string {
const c = "0123456789abcdef"
sign := ""
if s.negative {
sign = "-"
}
return sign + string(c[s.digit[0]]) + string(c[s.digit[1]])
}
func main() {
s := &state{} // initial state
for {
//fmt.Printf("%v\n", s)
fmt.Println(stateFormatter(s))
if nextState(s) {
break
}
}
fmt.Println("finished.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment