Skip to content

Instantly share code, notes, and snippets.

@gbarrancos
Created April 17, 2014 17:13
Show Gist options
  • Save gbarrancos/10998867 to your computer and use it in GitHub Desktop.
Save gbarrancos/10998867 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
var rot13Map = []string {
"a", "n",
"n", "a",
"b", "o",
"o", "b",
"c", "p",
"p", "c",
"d", "q",
"q", "d",
"e", "r",
"r", "e",
"f", "s",
"s", "f",
"g", "t",
"t", "g",
"u", "h",
"h", "u",
"i", "v",
"v", "i",
"j", "w",
"w", "j",
"k", "x",
"x", "k",
"l", "y",
"y", "l",
"z", "m",
"m", "z",
}
type rot13Reader struct {
reader io.Reader
}
func (r *rot13Reader) Read(p []byte) (n int, err error) {
readbytes, err := r.reader.Read(p)
replacer := strings.NewReplacer(rot13Map...)
decodedString := replacer.Replace(string(p[:readbytes]))
copy(p, []byte(decodedString))
return readbytes, nil
}
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