Created
April 4, 2017 15:32
-
-
Save geekman/dfad1bd979ae1e6279310b05f547a4f8 to your computer and use it in GitHub Desktop.
flexible bruteforcer skeleton in Golang
This file contains 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 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