Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
AspieSoft committed May 8, 2024
1 parent c9ff703 commit 49b3bd4
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cache/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cache/go.sum
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 3 additions & 3 deletions cputemp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions cputemp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
116 changes: 116 additions & 0 deletions crypt/crypt.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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{'-'}))
}
2 changes: 1 addition & 1 deletion crypt/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/AspieSoft/goutil/crypt
module github.com/AspieSoft/goutil/crypt/v2

go 1.20

Expand Down
2 changes: 1 addition & 1 deletion fs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions fs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion syncmap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions syncmap/go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 49b3bd4

Please sign in to comment.