Created
September 12, 2023 09:08
-
-
Save erdnaxeli/a050d600a4bcbac6e3434bbf9128c0fd to your computer and use it in GitHub Desktop.
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 ( | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"strings" | |
) | |
const msg = "The SHA256 for this sentence begins with: " | |
var counter = 0 | |
func toString(arr []string, s2i map[string]string) string { | |
b := strings.Builder{} | |
for _, v := range arr { | |
fmt.Fprint(&b, s2i[v]) | |
} | |
return b.String() | |
} | |
func testShasum(arr []string, s2i map[string]string) string { | |
counter++ | |
fullMsg := msg + strings.Join(arr[:6], ", ") + " and " + arr[6] + "." | |
sum := sha256.Sum256([]byte(fullMsg)) | |
hexSum := hex.EncodeToString(sum[:]) | |
//fmt.Println(counter, fullMsg) | |
if hexSum[:7] == toString(arr, s2i) { | |
return fullMsg | |
} | |
return "" | |
} | |
func test(choices []string, s2i map[string]string, toTest []string, idx int) string { | |
if idx == 7 { | |
return testShasum(toTest, s2i) | |
} | |
for _, v := range choices { | |
toTest[idx] = v | |
result := test(choices, s2i, toTest, idx+1) | |
if len(result) > 0 { | |
return result | |
} | |
} | |
return "" | |
} | |
func main() { | |
k := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "a", "b", "c", "d", "e", "f"} | |
m := map[string]string{ | |
"one": "1", | |
"two": "2", | |
"three": "3", | |
"four": "4", | |
"five": "5", | |
"six": "6", | |
"seven": "7", | |
"eight": "8", | |
"nine": "9", | |
"a": "a", | |
"b": "b", | |
"c": "c", | |
"d": "d", | |
"e": "e", | |
"f": "f", | |
} | |
fmt.Println(test(k, m, make([]string, 7), 0)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment