Last active
December 13, 2015 11:51
-
-
Save talonx/b7f98f108e85b4f5731f to your computer and use it in GitHub Desktop.
rot13 using lookup table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
var lookup = map[byte]byte{ | |
'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', | |
} | |
func (r13 rot13Reader) Read(b []byte) (int, error) { | |
nrot := make([]byte, 1024) | |
read, err := r13.r.Read(nrot) | |
if err != nil { | |
return 0, err | |
} | |
for i := 0; i < read; i++ { | |
fmt.Println("Looking up " + string(b[i])) | |
b[i] = lookup[nrot[i]] | |
} | |
return read, 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