Skip to content

Commit

Permalink
Make some useful changes with pointers to reduce confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
emedvedev committed Dec 20, 2016
1 parent 997d087 commit 8d340ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
6 changes: 3 additions & 3 deletions enigma.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type RotorConfig struct {
func NewEnigma(rotorConfiguration []RotorConfig, refID string, plugs []string) *Enigma {
rotors := make(Rotors, len(rotorConfiguration))
for i, configuration := range rotorConfiguration {
rotors[i] = HistoricRotors.GetByID(configuration.ID)
rotors[i] = *HistoricRotors.GetByID(configuration.ID)
rotors[i].Offset = CharToIndex(configuration.Start)
rotors[i].Ring = configuration.Ring - 1
}
Expand Down Expand Up @@ -118,13 +118,13 @@ func (e *Enigma) EncodeChar(letter byte) byte {
letterIndex = e.Plugboard[letterIndex]

for i := len(e.Rotors) - 1; i >= 0; i-- {
e.Rotors[i].Step(&letterIndex, false)
letterIndex = e.Rotors[i].Step(letterIndex, false)
}

letterIndex = e.Reflector.Sequence[letterIndex]

for i := 0; i < len(e.Rotors); i++ {
e.Rotors[i].Step(&letterIndex, true)
letterIndex = e.Rotors[i].Step(letterIndex, true)
}

letterIndex = e.Plugboard[letterIndex]
Expand Down
30 changes: 15 additions & 15 deletions presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ package enigma
// notches. "Beta" and "Gamma" are additional rotors used in M4
// at the leftmost position.
var HistoricRotors = Rotors{
NewRotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", "I", "Q"),
NewRotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", "II", "E"),
NewRotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", "III", "V"),
NewRotor("ESOVPZJAYQUIRHXLNFTGKDCMWB", "IV", "J"),
NewRotor("VZBRGITYUPSDNHLXAWMJQOFECK", "V", "Z"),
NewRotor("JPGVOUMFYQBENHZRDKASXLICTW", "VI", "ZM"),
NewRotor("NZJHGRCXMYSWBOUFAIVLPEKQDT", "VII", "ZM"),
NewRotor("FKQHTLXOCBJSPDZRAMEWNIUYGV", "VIII", "ZM"),
NewRotor("LEYJVCNIXWPBQMDRTAKZGFUHOS", "Beta", ""),
NewRotor("FSOKANUERHMBTIYCWLQPZXVGJD", "Gamma", ""),
*NewRotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", "I", "Q"),
*NewRotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", "II", "E"),
*NewRotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", "III", "V"),
*NewRotor("ESOVPZJAYQUIRHXLNFTGKDCMWB", "IV", "J"),
*NewRotor("VZBRGITYUPSDNHLXAWMJQOFECK", "V", "Z"),
*NewRotor("JPGVOUMFYQBENHZRDKASXLICTW", "VI", "ZM"),
*NewRotor("NZJHGRCXMYSWBOUFAIVLPEKQDT", "VII", "ZM"),
*NewRotor("FKQHTLXOCBJSPDZRAMEWNIUYGV", "VIII", "ZM"),
*NewRotor("LEYJVCNIXWPBQMDRTAKZGFUHOS", "Beta", ""),
*NewRotor("FSOKANUERHMBTIYCWLQPZXVGJD", "Gamma", ""),
}

// HistoricReflectors in the list are pre-loaded with historically accurate data
// from Enigma machines. Use "B-Thin" and "C-Thin" with M4 (4 rotors).
var HistoricReflectors = Reflectors{
NewReflector("EJMZALYXVBWFCRQUONTSPIKHGD", "A"),
NewReflector("YRUHQSLDPXNGOKMIEBFZCWVJAT", "B"),
NewReflector("FVPJIAOYEDRZXWGCTKUQSBNMHL", "C"),
NewReflector("ENKQAUYWJICOPBLMDXZVFTHRGS", "B-thin"),
NewReflector("RDOBJNTKVEHMLFCWZAXGYIPSUQ", "C-thin"),
*NewReflector("EJMZALYXVBWFCRQUONTSPIKHGD", "A"),
*NewReflector("YRUHQSLDPXNGOKMIEBFZCWVJAT", "B"),
*NewReflector("FVPJIAOYEDRZXWGCTKUQSBNMHL", "C"),
*NewReflector("ENKQAUYWJICOPBLMDXZVFTHRGS", "B-thin"),
*NewReflector("RDOBJNTKVEHMLFCWZAXGYIPSUQ", "C-thin"),
}
4 changes: 2 additions & 2 deletions reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func NewReflector(mapping string, id string) *Reflector {
}

// Reflectors is a simple list of reflector pointers.
type Reflectors []*Reflector
type Reflectors []Reflector

// GetByID takes a "name" of the reflector (e.g. "B") and returns the
// Reflector pointer.
func (refs *Reflectors) GetByID(id string) *Reflector {
for _, ref := range *refs {
if ref.ID == id {
return ref
return &ref
}
}
return nil
Expand Down
17 changes: 8 additions & 9 deletions rotor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,26 @@ func (r *Rotor) ShouldTurnOver() bool {

// Step through the rotor, performing the letter substitution depending
// on the offset and direction.
func (r *Rotor) Step(letter *int, invert bool) {
l := *letter
l = (l - r.Ring + r.Offset + 26) % 26
func (r *Rotor) Step(letter int, invert bool) int {
letter = (letter - r.Ring + r.Offset + 26) % 26
if invert {
l = r.ReverseSeq[l]
letter = r.ReverseSeq[letter]
} else {
l = r.StraightSeq[l]
letter = r.StraightSeq[letter]
}
l = (l + r.Ring - r.Offset + 26) % 26
*letter = l
letter = (letter + r.Ring - r.Offset + 26) % 26
return letter
}

// Rotors is a simple list of rotor pointers.
type Rotors []*Rotor
type Rotors []Rotor

// GetByID takes a "name" of the rotor (e.g. "III") and returns the
// Rotor pointer.
func (rs *Rotors) GetByID(id string) *Rotor {
for _, rotor := range *rs {
if rotor.ID == id {
return rotor
return &rotor
}
}
return nil
Expand Down

0 comments on commit 8d340ea

Please sign in to comment.