Skip to content

Commit febd30a

Browse files
committed
init commit ✨
0 parents  commit febd30a

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/main

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Francois-Timéo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/JustTimmm/GoPassword
2+
3+
go 1.23
4+
5+
require github.com/JustTimmm/GoColor v1.3.6

password.go

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

0 commit comments

Comments
 (0)