Skip to content

Instantly share code, notes, and snippets.

@zainfathoni
Created April 11, 2017 02:40
Show Gist options
  • Save zainfathoni/161ec3364b6ebe28f85e67b57db22286 to your computer and use it in GitHub Desktop.
Save zainfathoni/161ec3364b6ebe28f85e67b57db22286 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func Rot13(b byte) byte {
if (b >= 'A' && b <= 'M') || (b >= 'a' && b <= 'm') {
b += 13
} else if (b >= 'N' && b <= 'Z') || (b >= 'n' && b <= 'z') {
b -= 13
}
return b
}
func (rot rot13Reader) Read(b []byte) (n int, err error) {
n, err = rot.r.Read(b)
for i, val := range b {
b[i] = Rot13(val)
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment