about summary refs log tree commit diff
path: root/structs/structs.go
blob: 9f12b345d889da1b1d49502dfa1e77f37cdfa6ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package structs

import (
	"fmt"
)

type Ciphertext struct {
	Text         string         // the CipherText
	FrequencyMap map[string]int // frequency map
	BiGrams      map[string]int // frequency map
	cipher       []string       // list of ciphers that were applied to get this payload
}

// RotAll rotates the given ciphertext for all possile rotations
func (CiphertextVar Ciphertext) DecodeRotN(mainChannel chan Ciphertext) {

	// Calculate the new ciphertext for all rotations and push the result into the channel
	for i := 0; i < 26; i++ {
		CiphertextVar.Rot(i, mainChannel)
	}
}

// Rot rotates a ciphertext n units in one direction
func (CiphertextVar Ciphertext) Rot(n int, mainChannel chan Ciphertext) {

	// Length of the given ciphertext
	var CiphertextLength int = len(CiphertextVar.Text)
	var RotatedText string

	// Iterate over all the chars in the ciphertext and shift them by i units circular
	for i := 0; i < CiphertextLength; i++ {

		// Handle uppercase characters
		if 65 < int(CiphertextVar.Text[i]) == true && (int(CiphertextVar.Text[i])) < 90 == true {
			var newChar = (((int(CiphertextVar.Text[i]) + n) - 65) % 26) + 65
			RotatedText += string(newChar)
		}

		// Handle lowercase characters
		if 97 < int(CiphertextVar.Text[i]) == true && (int(CiphertextVar.Text[i])) < 122 == true {
			var newChar = (((int(CiphertextVar.Text[i]) + n) - 97) % 26) + 97
			RotatedText += string(newChar)
		}
	}

	// Convert the RotatedText string into a Ciphertext struct
	RotatedCipherStruct := Ciphertext{
		Text:         RotatedText,
		FrequencyMap: nil,
		cipher:       nil,
	}

	// Push the RotatedCipherStruct back into the main channel
	mainChannel <- RotatedCipherStruct
}

func (PrintString Ciphertext) Println() {
	fmt.Println(PrintString)
}

func (CiphertextVar Ciphertext) GenFrequencyMap(mainChannel chan Ciphertext) {

	// Initialize a Map mapping runes (chars) to ins (the amount of occurrences)
	newFrequencyMap := make(map[string]int)

	// Iterate over all the characters in the CipherText generating a FrequencyMap
	CipherTextLength := len(CiphertextVar.Text)
	ciphertext := CiphertextVar.Text
	for i := 0; i < CipherTextLength; i++ {
		newFrequencyMap[string(rune(ciphertext[i]))] = newFrequencyMap[string(rune(ciphertext[i]))] + 1
	}

	// Generate an instance of the Ciphertext containing the new FrequencyMap
	newCipherText := Ciphertext{
		Text:         CiphertextVar.Text,
		FrequencyMap: newFrequencyMap,
		BiGrams:      nil,
		cipher:       nil,
	}

	// Push the newCipherText back into the mainChannel
	mainChannel <- newCipherText
}

func (CiphertextVar Ciphertext) GenBiGramMap(mainChannel chan Ciphertext) {
	newBiGramMap := make(map[string]int)

	// Iterate over all the characters in the CipherText generating a FrequencyMap
	CipherTextLength := len(CiphertextVar.Text)
	ciphertext := CiphertextVar.Text
	for i := 0; i < CipherTextLength-1; i++ {
		newBiGramMap[string(ciphertext[i:i+2])] = newBiGramMap[string(ciphertext[i:i+2])] + 1
	}

	// Generate an instance of the Ciphertext containing the new FrequencyMap
	newCipherText := Ciphertext{
		Text:         CiphertextVar.Text,
		FrequencyMap: CiphertextVar.FrequencyMap,
		BiGrams:      newBiGramMap,
		cipher:       nil,
	}

	// Push the newCipherText back into the mainChannel
	mainChannel <- newCipherText
}