Browse Source

primo commit libreria generatrice

Mauro 1 year ago
commit
8cd767b794
3 changed files with 140 additions and 0 deletions
  1. 137 0
      Generator.go
  2. 0 0
      README.md
  3. 3 0
      go.mod

+ 137 - 0
Generator.go

@@ -0,0 +1,137 @@
+package mod
+
+import (
+  "fmt"
+  "os"
+  "bufio"
+  "container/list"
+  "math/rand"
+  "time"
+  "unicode"
+  "strings"
+)
+
+
+func caricaFile(file_da_leggere string) *os.File {
+  /*
+  lettura file.
+  nel caso di errore, ritorna nil
+  */ 
+  f,err := os.Open(file_da_leggere)
+
+  if err != nil {
+    fmt.Println("Errore apertura files",err)
+    return nil
+  }
+
+  // fmt.Println("Valore tornato:",f)
+  return f
+}
+
+func scansioneFile(result *os.File) *list.List {
+  /*
+  effettua la lettura del file attraverso
+  un apposito buffer.
+  */
+  l := list.New()
+  fs := bufio.NewScanner(result)
+
+  for  fs.Scan() {
+  /* 
+  l è una lista che viene 
+  alimentata leggendo il file di testo bufferizzato
+  */
+    l.PushBack(fs.Text())
+  }
+
+  if err := fs.Err(); err != nil {
+    fmt.Println("Errore finale:",err)
+    return nil 
+  }
+
+  return l
+}
+
+func returnRandom(l int) int {
+  /*
+  ritorna un numero random da 0 a l
+  ogni volta resetta il contatore
+  */
+  r1 := rand.NewSource(time.Now().UnixNano())
+  s1 := rand.New(r1)
+  return s1.Intn(l)+1
+}
+
+func returnStringCharUp(stringa string) string {
+  /*
+  prende una stringa e cambia lo stato di un carattere (da lower a upper e viceversa)
+  */
+  lunghezza := len(stringa)
+  random := returnRandom(lunghezza) -1
+  s := []byte(stringa)
+  
+  
+  if unicode.IsUpper(rune(s[random])) {
+    s[random]=byte(unicode.ToLower(rune(s[random])))
+  } else {
+    s[random]=byte(unicode.ToUpper(rune(s[random])))
+  }
+  return string(s)
+  
+}
+
+func returnStringToLower(stringa string) string {
+  // ritorna la stringa passata in lower.mode
+  return strings.ToLower(stringa)
+}
+
+func Run(filedaleggere string) string {
+
+  result := caricaFile(filedaleggere)
+  if result == nil {
+    fmt.Printf("Errore di lettura file '%s', esco.\n",filedaleggere)
+    os.Exit(98)
+  }
+  
+  // effettua la scnasione del file e ne legge il contenuto
+  l := scansioneFile(result)
+  if l == nil {
+    fmt.Println("Errore di lettura, esco.")
+    return ""
+  }
+
+  defer result.Close()
+
+  // estrae un valore a caso da 0 alla lunghezza del file.
+  position := returnRandom(l.Len())
+
+  counter := 0
+  risultato1 := "               "
+
+  /*
+  estrae una riga dal file in base a 'position'
+  */
+  for e := l.Front(); e != nil; e = e.Next() {
+    if position == counter {
+      risultato1 = fmt.Sprintf("%s",e.Value)
+    }
+    counter++
+  }
+  risultato1 = returnStringToLower(risultato1)
+  risultato1 = returnStringCharUp(risultato1)
+   
+  /* estrae un simbolo */
+  var simboli string = "\"'!?,.;:#%"
+
+  simbolo1 := simboli[returnRandom(len(simboli))-1]
+  simbolo2 := simboli[returnRandom(len(simboli))-1]
+  
+  elementi := []int{returnRandom(9),returnRandom(9),returnRandom(9),returnRandom(9)}
+
+  stringa := fmt.Sprintf("%d%d%c%s%c%d%d",elementi[0],elementi[1],simbolo1,risultato1,simbolo2,elementi[2],elementi[3])
+  fmt.Printf("Password: %s\n",stringa)
+  return stringa
+   
+}
+
+

+ 0 - 0
README.md


+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module Generator
+
+go 1.19