Skip to content

Instantly share code, notes, and snippets.

@itsmikeq
Created May 7, 2015 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsmikeq/0c1c58fc86099956b731 to your computer and use it in GitHub Desktop.
Save itsmikeq/0c1c58fc86099956b731 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
rot io.Reader
}
func rot13(b byte) byte {
var a, z byte
if 'a' <= b && b <= 'z' {
a, z = 'a', 'z'
} else if 'A' <= b && b <= 'Z' {
a, z = 'A', 'Z'
}
return (b-a+13)%(z-a+1) + a
}
func (rot *rot13Reader) Read(bytes []byte) (num int, err error) {
num, err = rot.rot.Read(bytes)
for i := 0; i < num; i++ {
bytes[i] = rot13(bytes[i])
}
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