Last active
September 22, 2021 14:14
-
-
Save avrebarra/3abf91c855018f36461127c1556118bd to your computer and use it in GitHub Desktop.
golang sequence obfuscation
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" | |
"math" | |
"math/rand" | |
"strings" | |
"time" | |
) | |
// reference https://cs.stackexchange.com/questions/21294/sequential-numbers-to-unique-looking-numbers | |
func main() { | |
ceil := 100 | |
digits := 2 | |
factor := 0 | |
obfuscator := 0 | |
// rebuild randomizer | |
s1 := rand.NewSource(time.Now().UnixNano()) | |
rand := rand.New(s1) | |
// find suitable factor | |
potfactors := []int{} | |
for pot := int(math.Pow(10, float64(digits-1))); pot < int(math.Pow(10, float64(digits))); pot++ { | |
if GCD(pot, int(math.Pow(10, float64(digits)))) == 1 { | |
potfactors = append(potfactors, pot) | |
} | |
} | |
factor = potfactors[rand.Intn(len(potfactors))] | |
obfuscator = potfactors[rand.Intn(len(potfactors))] | |
fmt.Println("potential factors:", potfactors) | |
fmt.Println("using factor:", factor) | |
fmt.Println("using obfuscator:", obfuscator) | |
// generate sequence | |
sequences := []string{} | |
outmap := map[int]bool{} | |
for i := 0; i < ceil; i++ { | |
seq := (i+1)*factor%int(math.Pow(10, float64(digits))) + obfuscator | |
outmap[seq] = true | |
sequences = append(sequences, fmt.Sprint(seq)) | |
} | |
fmt.Println("collision count:", ceil-len(outmap)) | |
fmt.Println("resulting sequences:", strings.Join(sequences, " ")) | |
} | |
func GCD(a, b int) int { | |
for b != 0 { | |
t := b | |
b = a % b | |
a = t | |
} | |
return a | |
} |
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" | |
"math" | |
"math/rand" | |
"strings" | |
"time" | |
) | |
// reference https://cs.stackexchange.com/questions/21294/sequential-numbers-to-unique-looking-numbers | |
func main() { | |
ceil := 100 | |
digits := 2 | |
// rebuild randomizer | |
s1 := rand.NewSource(time.Now().UnixNano()) | |
rand := rand.New(s1) | |
// find suitable factor | |
potfactors := []int{} | |
for pot := int(math.Pow(10, float64(digits-1))); pot < int(math.Pow(10, float64(digits))); pot++ { | |
if GCD(pot, int(math.Pow(10, float64(digits)))) == 1 { | |
potfactors = append(potfactors, pot) | |
} | |
} | |
factor1 := potfactors[rand.Intn(len(potfactors))] | |
factor2 := potfactors[rand.Intn(len(potfactors))] | |
// generate leveled obfuscators | |
o1 := obfuscator(digits, factor1) | |
o2 := obfuscator(digits, factor2) | |
// generate sequence | |
sequences := []string{} | |
outmap := map[int]bool{} | |
for i := 0; i < ceil; i++ { | |
seq := o2(o1(i)) | |
outmap[seq] = true | |
sequences = append(sequences, fmt.Sprint(seq)) | |
} | |
fmt.Println("collision count:", ceil-len(outmap)) | |
fmt.Println("resulting sequences:", strings.Join(sequences, " ")) | |
} | |
func obfuscator(digits, factor int) (fx func(int) int) { | |
return func(i int) int { | |
seq := (i + 1) * factor % int(math.Pow(10, float64(digits))) | |
return seq | |
} | |
} | |
func GCD(a, b int) int { | |
for b != 0 { | |
t := b | |
b = a % b | |
a = t | |
} | |
return a | |
} |
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" | |
"math" | |
"strings" | |
) | |
// reference https://cs.stackexchange.com/questions/21294/sequential-numbers-to-unique-looking-numbers | |
func main() { | |
ceil := 100 | |
digits := 2 | |
// generate leveled obfuscators | |
o1 := obfuscator(digits, 69) | |
o2 := obfuscator(digits, 67) | |
// generate sequence | |
sequences := []string{} | |
outmap := map[int]bool{} | |
for i := 0; i < ceil; i++ { | |
seq := o2(o1(i)) | |
outmap[seq] = true | |
sequences = append(sequences, fmt.Sprint(seq)) | |
} | |
fmt.Println("collision count:", ceil-len(outmap)) | |
fmt.Println("resulting sequences:", strings.Join(sequences, " ")) | |
} | |
func obfuscator(digits, factor int) (fx func(int) int) { | |
if GCD(factor, int(math.Pow(10, float64(digits)))) != 1 { | |
panic("bad factor") | |
} | |
return func(i int) int { | |
seq := (i + 1) * factor % int(math.Pow(10, float64(digits))) | |
return seq | |
} | |
} | |
func GCD(a, b int) int { | |
for b != 0 { | |
t := b | |
b = a % b | |
a = t | |
} | |
return a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment