2016-02-21 23:35:42 -08:00
|
|
|
package psyfer
|
|
|
|
|
2016-03-11 20:11:27 -08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2016-03-11 23:31:26 -08:00
|
|
|
//KeyJSON is a letter to letter mapping key
|
2016-03-11 20:11:27 -08:00
|
|
|
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
|
2016-03-11 21:50:32 -08:00
|
|
|
func ReadConfig(file string) {
|
|
|
|
dat, err := ioutil.ReadFile(file)
|
2016-03-11 20:11:27 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
json.Unmarshal([]byte(dat), &k)
|
2016-03-11 21:50:32 -08:00
|
|
|
k.Key[" "] = " " // keep spaces alive
|
2016-03-11 20:11:27 -08:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:31:26 -08:00
|
|
|
//Substitution performs Substitution cipher based on key
|
2016-03-11 20:11:27 -08:00
|
|
|
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
|
|
|
}
|