Grown

lkey package | Encrypt function

Dec 14th, 2020
631
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Encrypt returns a unique encrypted key
  2. func Encrypt(text string) string {
  3.     out := ""
  4.  
  5.     words := strings.Split(text, "")
  6.  
  7.     data, err := ioutil.ReadFile("keys.txt")
  8.  
  9.     if err != nil {
  10.         log.Fatal("lkey -> no keys.txt found")
  11.     }
  12.  
  13.     lines := []string{}
  14.  
  15.     if len(string(data)) > 0 {
  16.         lines = strings.Split(strings.TrimSpace(string(data)), "\n")
  17.     }
  18.  
  19.     letters := []map[string]string{}
  20.  
  21.     if len(lines) > 0 {
  22.         for _, line := range lines {
  23.             line = strings.Replace(line, "\r", "", 1)
  24.  
  25.             lineSplit := strings.Split(line, "=")
  26.  
  27.             l, e := lineSplit[0], lineSplit[1]
  28.  
  29.             letters = append(letters, map[string]string{"letter": l, "encrypted": e})
  30.         }
  31.     }
  32.  
  33.     for _, l := range words {
  34.         found := false
  35.  
  36.         e := ""
  37.  
  38.         for _, m := range letters {
  39.             if m["letter"] == l {
  40.                 found = true
  41.                 e = m["encrypted"]
  42.             }
  43.         }
  44.  
  45.         if found == true {
  46.             out += e
  47.         } else {
  48.             r := uniuri.NewLen(9)
  49.  
  50.             out += r
  51.  
  52.             f, err := os.OpenFile("keys.txt", os.O_APPEND|os.O_WRONLY, 0644)
  53.  
  54.             if err != nil {
  55.                 log.Fatal("lkey -> something went wrong whilst opening keys.txt")
  56.             }
  57.  
  58.             f.WriteString(fmt.Sprintf("%s=%s\n", l, r))
  59.  
  60.             f.Close()
  61.  
  62.             letters = append(letters, map[string]string{"letter": l, "encrypted": r})
  63.         }
  64.     }
  65.  
  66.     return out
  67. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×