2012-11-06 12:35:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-11-10 18:41:33 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2012-11-06 12:35:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var lookupTable = map[byte]byte{
|
2012-11-10 18:41:33 +00:00
|
|
|
'A': 'N',
|
|
|
|
'B': 'O',
|
|
|
|
'C': 'P',
|
|
|
|
'D': 'Q',
|
|
|
|
'E': 'R',
|
|
|
|
'F': 'S',
|
|
|
|
'G': 'T',
|
|
|
|
'H': 'U',
|
|
|
|
'I': 'V',
|
|
|
|
'J': 'W',
|
|
|
|
'K': 'X',
|
|
|
|
'L': 'Y',
|
|
|
|
'M': 'Z',
|
|
|
|
'N': 'A',
|
|
|
|
'O': 'B',
|
|
|
|
'P': 'C',
|
|
|
|
'Q': 'D',
|
|
|
|
'R': 'E',
|
|
|
|
'S': 'F',
|
|
|
|
'T': 'G',
|
|
|
|
'U': 'H',
|
|
|
|
'V': 'I',
|
|
|
|
'W': 'J',
|
|
|
|
'X': 'K',
|
|
|
|
'Y': 'L',
|
|
|
|
'Z': 'M',
|
|
|
|
'a': 'n',
|
|
|
|
'b': 'o',
|
|
|
|
'c': 'p',
|
|
|
|
'd': 'q',
|
|
|
|
'e': 'r',
|
|
|
|
'f': 's',
|
|
|
|
'g': 't',
|
|
|
|
'h': 'u',
|
|
|
|
'i': 'v',
|
|
|
|
'j': 'w',
|
|
|
|
'k': 'x',
|
|
|
|
'l': 'y',
|
|
|
|
'm': 'z',
|
|
|
|
'n': 'a',
|
|
|
|
'o': 'b',
|
|
|
|
'p': 'c',
|
|
|
|
'q': 'd',
|
|
|
|
'r': 'e',
|
|
|
|
's': 'f',
|
|
|
|
't': 'g',
|
|
|
|
'u': 'h',
|
|
|
|
'v': 'i',
|
|
|
|
'w': 'j',
|
|
|
|
'x': 'k',
|
|
|
|
'y': 'l',
|
|
|
|
'z': 'm',
|
2012-11-06 12:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rot13Reader struct {
|
2012-11-10 18:41:33 +00:00
|
|
|
reader io.Reader
|
2012-11-06 12:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rot *rot13Reader) Read(p []byte) (i int, err error) {
|
2012-11-10 18:41:33 +00:00
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(p))
|
|
|
|
i, err = rot.reader.Read(buf)
|
2012-11-06 12:35:11 +00:00
|
|
|
|
2012-11-10 18:41:33 +00:00
|
|
|
var (
|
|
|
|
b, c byte
|
|
|
|
found bool
|
|
|
|
)
|
|
|
|
for i := range buf {
|
|
|
|
b = buf[i]
|
|
|
|
c, found = lookupTable[b]
|
|
|
|
if found {
|
|
|
|
p[i] = c
|
|
|
|
} else {
|
|
|
|
p[i] = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2012-11-06 12:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-11-10 18:41:33 +00:00
|
|
|
s := strings.NewReader("Lbh penpxrq gur pbqr!")
|
|
|
|
r := rot13Reader{s}
|
|
|
|
io.Copy(os.Stdout, &r)
|
|
|
|
fmt.Println("")
|
2012-11-06 12:35:11 +00:00
|
|
|
}
|