-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.go
More file actions
67 lines (56 loc) · 1.31 KB
/
password.go
File metadata and controls
67 lines (56 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package GoPassword
import (
"encoding/json"
"github.com/JustTimmm/GoColor"
"math/rand"
"os"
"strings"
"time"
)
type JsonFile struct {
Password string `json:"password"`
}
func GeneratePassword(width uint8, number, specialCharacter, uppercase, lowercase bool) string {
if width > 128 {
GoColor.ErrorLog("You can't exceed the 128 character limit !\n")
return ""
} else if width < 8 {
GoColor.ErrorLog("You must have at least 8 characters !\n")
return ""
}
var charset string
if number {
charset += "0123456789"
}
if specialCharacter {
charset += "&#-_*<>?!*"
}
if uppercase {
charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
if lowercase {
charset += "abcdefghijklmnopqrstuvwxyz"
}
rand.Seed(time.Now().UnixNano())
var sb strings.Builder
for i := 0; i < int(width); i++ {
randomIndex := rand.Intn(len(charset))
sb.WriteByte(charset[randomIndex])
}
return sb.String()
}
func SavePassword(filename string, password string) {
data := JsonFile{Password: password}
file, err := os.Create(filename + ".json")
if err != nil {
GoColor.ErrorLog("File error: %s\n", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
GoColor.ErrorLog("JSON encoding error: %s\n", err)
}
}