Skip to content

Instantly share code, notes, and snippets.

@nexus166
Created June 3, 2020 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nexus166/c27b0f3df9bd541bfd8fbe6e221cb013 to your computer and use it in GitHub Desktop.
Save nexus166/c27b0f3df9bd541bfd8fbe6e221cb013 to your computer and use it in GitHub Desktop.
func rotateString(s string, n int, encdec bool) (string, error) {
l := len(s)
if l < 1 {
return "", fmt.Errorf("empty input")
}
if n < 1 || n > 6 {
return "", fmt.Errorf("invalid shift n %d", n)
}
var out string
switch encdec {
case true: // obfuscate
for i := 0; i < l; i++ {
out += string(int(s[i]) + n)
}
case false: // deobfuscate
for i := 0; i < l; i++ {
out += string(int(s[i]) - n)
}
}
return out, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment