diff --git a/cache/go.mod b/cache/go.mod index 08fddf6..fb54a46 100644 --- a/cache/go.mod +++ b/cache/go.mod @@ -2,6 +2,6 @@ module github.com/AspieSoft/goutil/cache go 1.20 -require github.com/AspieSoft/goutil/v7 v7.7.1 +require github.com/AspieSoft/goutil/v7 v7.7.2 require github.com/AspieSoft/go-regex-re2/v2 v2.2.0 // indirect diff --git a/cache/go.sum b/cache/go.sum index 342af20..e4abf1d 100644 --- a/cache/go.sum +++ b/cache/go.sum @@ -1,6 +1,6 @@ github.com/AspieSoft/go-regex-re2/v2 v2.2.0 h1:CK9+SYs7BYy+lV/JrmRbyF+SuTF+e+BIyjKGjKJQzLg= github.com/AspieSoft/go-regex-re2/v2 v2.2.0/go.mod h1:w+vA1zICvB4OQZGY8KdpyMwjwbFXdnZt9iQ7jRR+ycQ= -github.com/AspieSoft/goutil/v7 v7.7.0 h1:4eI/zkfOLBloKdIm6+1tk1LkZOC8FdaRWcgVj5nElq8= -github.com/AspieSoft/goutil/v7 v7.7.0/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I= github.com/AspieSoft/goutil/v7 v7.7.1 h1:fxokEHYF8QsTIsnJ16wivfCzBshGGA62UwRiQ0MOYlw= github.com/AspieSoft/goutil/v7 v7.7.1/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I= +github.com/AspieSoft/goutil/v7 v7.7.2 h1:Iuk6F9I/3wpNUQKwFqsRSjcXkJSaSQMpnXsblx/MLU4= +github.com/AspieSoft/goutil/v7 v7.7.2/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I= diff --git a/cputemp/go.mod b/cputemp/go.mod index 8d75c59..57b10d7 100644 --- a/cputemp/go.mod +++ b/cputemp/go.mod @@ -7,8 +7,8 @@ require github.com/shirou/gopsutil v3.21.11+incompatible require ( github.com/go-ole/go-ole v1.3.0 // indirect github.com/stretchr/testify v1.8.4 // indirect - github.com/tklauser/go-sysconf v0.3.13 // indirect - github.com/tklauser/numcpus v0.7.0 // indirect + github.com/tklauser/go-sysconf v0.3.14 // indirect + github.com/tklauser/numcpus v0.8.0 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect - golang.org/x/sys v0.19.0 // indirect + golang.org/x/sys v0.20.0 // indirect ) diff --git a/cputemp/go.sum b/cputemp/go.sum index 7f2df26..f3c9e6e 100644 --- a/cputemp/go.sum +++ b/cputemp/go.sum @@ -9,12 +9,18 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tklauser/go-sysconf v0.3.13 h1:GBUpcahXSpR2xN01jhkNAbTLRk2Yzgggk8IM08lq3r4= github.com/tklauser/go-sysconf v0.3.13/go.mod h1:zwleP4Q4OehZHGn4CYZDipCgg9usW5IJePewFCGVEa0= +github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU= +github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= github.com/tklauser/numcpus v0.7.0 h1:yjuerZP127QG9m5Zh/mSO4wqurYil27tHrqwRoRjpr4= github.com/tklauser/numcpus v0.7.0/go.mod h1:bb6dMVcj8A42tSE7i32fsIUCbQNllK5iDguyOZRUzAY= +github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY= +github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/crypt/crypt.go b/crypt/crypt.go index 7c510ff..8b96e11 100644 --- a/crypt/crypt.go +++ b/crypt/crypt.go @@ -1,14 +1,20 @@ package crypt import ( + "bytes" "crypto/aes" "crypto/cipher" "crypto/hmac" "crypto/rand" + "crypto/sha1" "crypto/sha256" + "crypto/sha512" "encoding/base64" "errors" "io" + "math" + "strconv" + "time" "github.com/AspieSoft/go-regex-re2/v2" ) @@ -159,3 +165,113 @@ func RandBytes(size int, exclude ...[]byte) []byte { return b[:size] } + +var uuidGenLastTime int64 + +// GenUUID generates a Unique Identifier using a custom build method +// +// Notice: This feature is currently in beta +// +// @size: (minimum: 8) the bit size for the last part of the uuid +// (note: other parts may vary) +// +// @timezone: optionally add a timezone string to the uuid +// (note: you could also pass random info into here for a more complex algorithm) +// +// This method uses the following data: +// - A hash of the current year and day of year +// - A hash of the current timezone +// - A hash of the current unix time (in seconds) +// - A hash of the current unix time in nanoseconds and a random number +// +// The returned value is url encoded and will look something like this: xxxx-xxxx-xxxx-xxxxxxxx +func GenUUID(size int, timezone ...string) string { + for time.Now().UnixNano() <= uuidGenLastTime { + time.Sleep(1 * time.Millisecond) + } + uuidGenLastTime = time.Now().UnixNano() + + if size < 8 { + size = 8 + } + + uuid := [][]byte{{}, {}, {}, {}} + + // year + { + s := int(math.Min(float64(size/4), 8)) + if s < 4 { + s = 4 + } + + sm := s/2 + if s % 2 != 0 { + sm++ + } + + b := sha1.Sum([]byte(strconv.Itoa(time.Now().Year()))) + uuid[0] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm] + b = sha1.Sum([]byte(strconv.Itoa(time.Now().YearDay()))) + uuid[0] = append(uuid[0], []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm]...) + uuid[0] = uuid[0][:s] + } + + // time zone + { + s := int(math.Min(float64(size/8), 8)) + if s < 4 { + s = 4 + } + + if len(timezone) != 0 { + sm := s/len(timezone) + if s % 2 != 0 { + sm++ + } + + for _, zone := range timezone { + b := sha1.Sum([]byte(zone)) + uuid[1] = append(uuid[1], []byte(base64.URLEncoding.EncodeToString(b[:]))[:sm]...) + } + uuid[1] = uuid[1][:s] + }else{ + z, _ := time.Now().Zone() + b := sha1.Sum([]byte(z)) + uuid[1] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s] + } + } + + // unix time + { + s := int(math.Min(float64(size/2), 16)) + if s < 4 { + s = 4 + } + + b := sha1.Sum([]byte(strconv.Itoa(int(time.Now().Unix())))) + uuid[2] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s] + } + + // random + { + s := int(math.Min(float64(size/4), 64)) + if s < 4 { + s = 4 + } + + b := sha512.Sum512([]byte(strconv.Itoa(int(time.Now().UnixNano())))) + uuid[3] = []byte(base64.URLEncoding.EncodeToString(b[:]))[:s] + uuid[3] = append(uuid[3], []byte(base64.URLEncoding.EncodeToString(RandBytes(size)))[:size-s]...) + } + + if len(uuid[1]) == 0 { + uuid = append(uuid[:1], uuid[2:]...) + } + + for i := range uuid { + uuid[i] = bytes.ReplaceAll(uuid[i], []byte{'-'}, []byte{'0'}) + uuid[i] = bytes.ReplaceAll(uuid[i], []byte{'_'}, []byte{'1'}) + } + + return string(bytes.Join(uuid, []byte{'-'})) +} diff --git a/crypt/go.mod b/crypt/go.mod index 678a924..f911261 100644 --- a/crypt/go.mod +++ b/crypt/go.mod @@ -1,4 +1,4 @@ -module github.com/AspieSoft/goutil/crypt +module github.com/AspieSoft/goutil/crypt/v2 go 1.20 diff --git a/fs/go.mod b/fs/go.mod index c77044f..f6e3aed 100644 --- a/fs/go.mod +++ b/fs/go.mod @@ -8,4 +8,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 ) -require golang.org/x/sys v0.19.0 // indirect +require golang.org/x/sys v0.20.0 // indirect diff --git a/fs/go.sum b/fs/go.sum index c75b560..5897dd7 100644 --- a/fs/go.sum +++ b/fs/go.sum @@ -4,6 +4,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/syncmap/go.mod b/syncmap/go.mod index 4a88c47..91c6266 100644 --- a/syncmap/go.mod +++ b/syncmap/go.mod @@ -2,6 +2,6 @@ module github.com/AspieSoft/goutil/syncmap go 1.21.5 -require github.com/AspieSoft/goutil/v7 v7.7.1 +require github.com/AspieSoft/goutil/v7 v7.7.2 require github.com/AspieSoft/go-regex-re2/v2 v2.2.0 // indirect diff --git a/syncmap/go.sum b/syncmap/go.sum index 342af20..e4abf1d 100644 --- a/syncmap/go.sum +++ b/syncmap/go.sum @@ -1,6 +1,6 @@ github.com/AspieSoft/go-regex-re2/v2 v2.2.0 h1:CK9+SYs7BYy+lV/JrmRbyF+SuTF+e+BIyjKGjKJQzLg= github.com/AspieSoft/go-regex-re2/v2 v2.2.0/go.mod h1:w+vA1zICvB4OQZGY8KdpyMwjwbFXdnZt9iQ7jRR+ycQ= -github.com/AspieSoft/goutil/v7 v7.7.0 h1:4eI/zkfOLBloKdIm6+1tk1LkZOC8FdaRWcgVj5nElq8= -github.com/AspieSoft/goutil/v7 v7.7.0/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I= github.com/AspieSoft/goutil/v7 v7.7.1 h1:fxokEHYF8QsTIsnJ16wivfCzBshGGA62UwRiQ0MOYlw= github.com/AspieSoft/goutil/v7 v7.7.1/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I= +github.com/AspieSoft/goutil/v7 v7.7.2 h1:Iuk6F9I/3wpNUQKwFqsRSjcXkJSaSQMpnXsblx/MLU4= +github.com/AspieSoft/goutil/v7 v7.7.2/go.mod h1:JGAt912jBwFrTXiazla1FTVqSI3zDetUKR5HJb9ND5I=