psyfer/psyfer/substitution.go

38 lines
718 B
Go
Raw Normal View History

2016-02-21 23:35:42 -08:00
package psyfer
import (
"encoding/json"
"io/ioutil"
"log"
)
2016-03-11 23:31:26 -08:00
//KeyJSON is a letter to letter mapping key
type KeyJSON struct {
Key map[string]string `json:"key"`
}
var k = KeyJSON{}
2016-03-11 23:31:26 -08:00
//ReadConfig reads a json file containing key map
func ReadConfig(file string) {
dat, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
json.Unmarshal([]byte(dat), &k)
k.Key[" "] = " " // keep spaces alive
}
2016-03-11 23:31:26 -08:00
//Substitution performs Substitution cipher based on key
func Substitution(input string) string {
inputSlice := []string{}
output := ""
for _, char := range input {
inputSlice = append(inputSlice, string(char))
}
for i := range inputSlice {
output += k.Key[inputSlice[i]]
}
return output
2016-02-21 23:35:42 -08:00
}