summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-06-19 11:47:30 -0400
committersiddharth <s@ricketyspace.net>2021-06-19 11:47:30 -0400
commitd90053b1bb6620dc999f723d64aadccfbe6e0bbf (patch)
tree9d4d593727052cd471de513e2552ce251986e11e
parente285c8aa4a03b4f96b1ab36a0fd4b1a6e810ec4b (diff)
lib: add PrintableAscii, AsciiScores
Extracted from challenge 19
-rw-r--r--challenge/c19.go25
-rw-r--r--lib/str.go27
2 files changed, 29 insertions, 23 deletions
diff --git a/challenge/c19.go b/challenge/c19.go
index 544e30b..e785898 100644
--- a/challenge/c19.go
+++ b/challenge/c19.go
@@ -52,27 +52,6 @@ func C19() {
"VHJhbnNmb3JtZWQgdXR0ZXJseTo=",
"QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4=",
}
- // ASCII characters ordered by frequency
- // (https://mdickens.me/typing/letter_frequency.html)
- ascii := []byte{
- ' ', 'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd',
- 'c', 'u', 'm', 'f', 'g', 'p', 'y', 'w', 'b', ',', '.',
- 'v', 'k', '-', '"', '_', '\'', 'x', ')', '(', ';', '0', 'j',
- '1', 'q', '=', '2', ':', 'z', '/', '*', '!', '?', '$', '3',
- '5', '>', '{', '}', '4', '9', '[', ']', '8', '6', '7', '\\',
- '+', '|', '&', '<', '%', '@', '#', '^', '`', '~',
- }
- // Key: ASCII character; Value: score
- asciiScores := make(map[byte]int, 0)
- for pos, a := range ascii {
- asciiScores[a] = 255 - pos
- // Also add the uppercase version of ascii
- // character if it exists.
- au := lib.ByteToUpper(a)
- if a != au {
- asciiScores[au] = 255 - pos
- }
- }
// Utility functions.
cipherStreamByteGroups := func(ciphers [][]byte) map[int][]byte {
@@ -91,7 +70,7 @@ func C19() {
po := make([][]byte, len(g)) // Potential Output block bytes
for i, c := range g {
po[i] = make([]byte, 0)
- for _, a := range ascii {
+ for _, a := range lib.PrintableAscii {
o := c ^ a
po[i] = append(po[i], o)
@@ -117,7 +96,7 @@ func C19() {
scr := 0
for _, c := range g {
p := c ^ o
- if s, ok := asciiScores[p]; ok {
+ if s, ok := lib.AsciiScores[p]; ok {
scr += s
}
if pos == 0 && lib.ByteIsUpper(p) {
diff --git a/lib/str.go b/lib/str.go
index 8db415f..ee8667f 100644
--- a/lib/str.go
+++ b/lib/str.go
@@ -3,6 +3,33 @@
package lib
+// Key: ASCII character; Value: score
+var AsciiScores map[byte]int = make(map[byte]int, 0)
+
+// Printable ASCII characters orrdered by frequency.
+// (https://mdickens.me/typing/letter_frequency.html)
+var PrintableAscii []byte = []byte{
+ ' ', 'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd',
+ 'c', 'u', 'm', 'f', 'g', 'p', 'y', 'w', 'b', ',', '.',
+ 'v', 'k', '-', '"', '_', '\'', 'x', ')', '(', ';', '0', 'j',
+ '1', 'q', '=', '2', ':', 'z', '/', '*', '!', '?', '$', '3',
+ '5', '>', '{', '}', '4', '9', '[', ']', '8', '6', '7', '\\',
+ '+', '|', '&', '<', '%', '@', '#', '^', '`', '~',
+}
+
+func init() {
+ // Initialize AsciiScores
+ for pos, a := range PrintableAscii {
+ AsciiScores[a] = 255 - pos
+ // Also add the uppercase version of ascii
+ // character if it exists.
+ au := ByteToUpper(a)
+ if a != au {
+ AsciiScores[au] = 255 - pos
+ }
+ }
+}
+
func FillStr(a string, l int) string {
b := ""
if l < 1 {