Skip to content

Commit

Permalink
Correct types in caesar.go (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Siriak <[email protected]>
  • Loading branch information
soypat and siriak authored Sep 7, 2021
1 parent e498a69 commit 5ac0bd5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ciphers/caesar/caesar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package caesar
func Encrypt(input string, key int) string {
// if key is negative value,
// updates "key" the number which congruents to "key" modulo 26
key = (key%26 + 26) % 26
key8 := byte(key%26+26) % 26

outputBuffer := []byte{}
var outputBuffer []byte
// r is a rune, which is the equivalent of uint32.
for _, r := range input {
newByte := byte(r)
if 'A' <= newByte && newByte <= 'Z' {
outputBuffer = append(outputBuffer, byte(int('A')+int(int(newByte-'A')+key)%26))
} else if 'a' <= newByte && newByte <= 'z' {
outputBuffer = append(outputBuffer, byte(int('a')+int(int(newByte-'a')+key)%26))
if 'A' <= r && r <= 'Z' {
outputBuffer = append(outputBuffer, 'A'+(newByte-'A'+key8)%26)
} else if 'a' <= r && r <= 'z' {
outputBuffer = append(outputBuffer, 'a'+(newByte-'a'+key8)%26)
} else {
outputBuffer = append(outputBuffer, newByte)
}
Expand Down

0 comments on commit 5ac0bd5

Please sign in to comment.