-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
167 lines (136 loc) · 4.06 KB
/
utils.go
File metadata and controls
167 lines (136 loc) · 4.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package utils
import (
"crypto/rand"
"io/fs"
"math/big"
"os"
"path/filepath"
"strings"
"unsafe"
)
//nolint:gosec
/*#nosec G103*/
func UnsafeBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
//nolint:gosec
/*#nosec G103*/
func UnsafeString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
// GetAbsolutePath Returns absolute path string for a given directory and error if directory doesent exist
func GetAbsolutePath(path string) (string, error) {
var err error
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return path, nil
}
return path, err
}
// CreateDirectoryFromFile Provides a way of creating a directory from a path
// Returns created directory path and error if fails
func CreateDirectoryFromFile(path string, perm fs.FileMode) (string, error) {
p, err := GetAbsolutePath(path)
if err != nil {
return "", err
}
directory := filepath.Dir(p)
if err = os.MkdirAll(directory, perm); err != nil {
return "", err
}
return p, nil
}
// CreateFile Creates file for given directory with flags and permissions for directory and file
// Returns file instance and error if it fails
func CreateFile(path string, flags int, dirMode, mode fs.FileMode) (*os.File, error) {
path, err := CreateDirectoryFromFile(path, dirMode|fs.ModeDir)
if err != nil {
return nil, err
}
if _, err = os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
var file *os.File
//#nosec G304
file, err = os.Create(path)
if err != nil {
return nil, err
}
if err = file.Chmod(mode); err != nil {
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
}
//#nosec G304
return os.OpenFile(path, flags, mode)
}
// CreateLogFile Creates write only appendable file with permission 0o744 for directory and file for given path
func CreateLogFile(path string) (*os.File, error) {
return CreateFile(path, os.O_WRONLY|os.O_APPEND, 0o744, fs.FileMode(0o744)|os.ModeAppend)
}
// FileExists Provides a way of checking if file exists. Returns bool
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// CreateDirectory Provides a way of creating directory with permissions for a given path
// Returns string path of created directory and error if fails
func CreateDirectory(path string, perm fs.FileMode) (string, error) {
p, err := GetAbsolutePath(path)
if err != nil {
return "", err
}
if err = os.MkdirAll(p, perm); err != nil {
return "", err
}
return p, nil
}
const (
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
special = "!@#$%^&*()-_=+[]{}|;:,.<>?"
allChars = lowercase + uppercase + numbers + special
)
var charSets = []string{lowercase, uppercase, numbers, special}
// GenerateRandomPassword generates a cryptographically secure random password
// with specified length containing uppercase letters, lowercase letters, numbers, and special characters
func GenerateRandomPassword(length int) (string, error) {
if length < 8 {
length = 12 // Default to 12 characters minimum for security
}
var password strings.Builder
password.Grow(length)
// Ensure at least one character from each set
for _, charset := range charSets {
randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
_ = password.WriteByte(charset[randomIndex.Int64()])
}
// Fill the rest with random characters from all sets
for password.Len() < length {
randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(allChars))))
if err != nil {
return "", err
}
_ = password.WriteByte(allChars[randomIndex.Int64()])
}
// Shuffle the password to avoid predictable patterns
passwordBytes := UnsafeBytes(password.String())
for i := len(passwordBytes) - 1; i > 0; i-- {
j, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
if err != nil {
return "", err
}
passwordBytes[i], passwordBytes[j.Int64()] = passwordBytes[j.Int64()], passwordBytes[i]
}
return UnsafeString(passwordBytes), nil
}