Skip to content

Instantly share code, notes, and snippets.

@nnkken
Last active February 11, 2018 05:26
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 nnkken/8bf1da5407d9eb3eba4af02c4e767f4a to your computer and use it in GitHub Desktop.
Save nnkken/8bf1da5407d9eb3eba4af02c4e767f4a to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "encoding/hex"
import "crypto/sha1"
func enumerateInternal(n int, prefix []byte, set []byte, targets map[[20]byte]bool) map[[20]byte][]byte {
result := make(map[[20]byte][]byte)
size := len(set)
prefixLen := len(prefix)
data := make([]byte, n + prefixLen)
copy(data, prefix)
input := data[prefixLen:]
for i := 0; i < n; i += 1 {
input[i] = set[0]
}
inputIndex := make([]int, n)
for {
b := sha1.Sum(data)
b2 := sha1.Sum(b[:])
_, match := targets[b2]
if match {
r := make([]byte, len(data))
copy(r, data)
result[b2] = r
}
inputIndex[0] += 1
if inputIndex[0] == size {
inputIndex[0] = 0
carryCount := 1
for ; carryCount < n; carryCount += 1 {
if inputIndex[carryCount] == size - 1 {
inputIndex[carryCount] = 0
input[carryCount] = set[inputIndex[carryCount]]
} else {
inputIndex[carryCount] += 1
input[carryCount] = set[inputIndex[carryCount]]
break
}
}
if carryCount == n {
break
}
} else {
input[0] = set[inputIndex[0]]
}
}
return result
}
func enumerate(n int, set []byte, targets map[[20]byte]bool) map[[20]byte][]byte {
prefix := make([]byte, 0)
return enumerateInternal(n, prefix, set, targets)
}
func enumerateParallel(n int, set []byte, targets map[[20]byte]bool) map[[20]byte][]byte {
if n < 2 {
return enumerate(n, set, targets)
}
resultChan := make(chan(map[[20]byte][]byte), len(set))
for _, prefixByte := range(set) {
prefix := []byte { prefixByte }
go func() {
result := enumerateInternal(n - 1, prefix, set, targets)
resultChan <- result
}()
}
result := make(map[[20]byte][]byte)
for range(set) {
subResult := <-resultChan
for k, v := range(subResult) {
result[k] = v
}
}
return result
}
func main() {
set := []byte("abcdefghijklmnopqrstuvwxyz")
hashHexList := []string {
"aa1420f182e88b9e5f874f6fbe7459291e8f4601",
"422694e6f7d2b39e8ef7b36b5f448c04f6493802",
}
targets := make(map[[20]byte]bool)
for _, hashHex := range(hashHexList) {
var r [20]byte
hex.Decode(r[:], []byte(hashHex))
targets[r] = true
}
result := enumerateParallel(6, set, targets)
fmt.Println("Done.")
for hash, input := range(result) {
hashHex := hex.EncodeToString(hash[:])
fmt.Printf("%s => %s\n", hashHex, string(input))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment