github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/brutecheck/hashes.go (about) 1 package main 2 3 import "fmt" 4 5 type sortByHash struct { 6 keywords []string 7 hashes []byte 8 } 9 10 func (s *sortByHash) Len() int { return len(s.keywords) } 11 func (s *sortByHash) Less(i, k int) bool { return s.hashes[i] < s.hashes[k] } 12 func (s *sortByHash) Swap(i, k int) { 13 s.hashes[i], s.hashes[k] = s.hashes[k], s.hashes[i] 14 s.keywords[i], s.keywords[k] = s.keywords[k], s.keywords[i] 15 } 16 17 type Combiner struct { 18 Name string 19 Op string 20 Fn func(a, b byte) byte 21 } 22 23 var combiners = []*Combiner{ 24 {Name: "Xor", Op: "^", Fn: func(a, b byte) byte { return a ^ b }}, 25 {Name: "Add", Op: "+", Fn: func(a, b byte) byte { return a + b }}, 26 {Name: "Or", Op: "|", Fn: func(a, b byte) byte { return a | b }}, 27 {Name: "And", Op: "&", Fn: func(a, b byte) byte { return a & b }}, 28 } 29 30 func twoHashCalc(name string, a, b *Combiner, shiftn, shift0, shift1 byte) byte { 31 return b.Fn(a.Fn(byte(len(name))<<shiftn, name[0]<<shift0), name[1]<<shift1) 32 } 33 func twoHashFormat(a, b *Combiner, shiftn, shift0, shift1 byte) string { 34 return fmt.Sprintf("((byte(len(name)) << %d) %s (name[0] << %d)) %s (name[1] << %d)", shiftn, a.Op, shift0, b.Op, shift1) 35 } 36 37 func twoHashCalcAlt(name string, a, b *Combiner, shiftn, shift0, shift1 byte) byte { 38 return b.Fn(byte(len(name))<<shiftn, a.Fn(name[0]<<shift0, name[1]<<shift1)) 39 } 40 func twoHashFormatAlt(a, b *Combiner, shiftn, shift0, shift1 byte) string { 41 return fmt.Sprintf("(byte(len(name)) << %d) %s ((name[0] << %d) %s (name[1] << %d))", shiftn, a.Op, shift0, b.Op, shift1) 42 }