Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
87fa6021e4
|
|||
297d6c52a8
|
|||
b9c46947d2
|
|||
412277b3e0
|
|||
e46f8019ec
|
|||
ae952b2166
|
|||
b24dba9a45
|
|||
cfbc20367d
|
|||
e25912758e
|
|||
e1ae77a9db
|
@@ -74,7 +74,7 @@ func processEnvOverrides(rval reflect.Value, delim string, prefix string) error
|
||||
|
||||
rvfield.Set(reflect.ValueOf(envval))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' () with '%s'\n", fullEnvKey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(int(0)) {
|
||||
|
||||
|
68
cryptext/aes.go
Normal file
68
cryptext/aes.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package cryptext
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// https://stackoverflow.com/a/18819040/1761622
|
||||
|
||||
func EncryptAESSimple(password, text []byte) ([]byte, error) {
|
||||
|
||||
key, err := scrypt.Key(password, nil, 32768, 8, 1, 32) // this is not 100% correct, rounds too low and salt is missing
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b := base64.StdEncoding.EncodeToString(text)
|
||||
ciphertext := make([]byte, aes.BlockSize+len(b))
|
||||
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfb := cipher.NewCFBEncrypter(block, iv)
|
||||
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
|
||||
|
||||
return ciphertext, nil
|
||||
}
|
||||
|
||||
func DecryptAESSimple(password, text []byte) ([]byte, error) {
|
||||
|
||||
key, err := scrypt.Key(password, nil, 32768, 8, 1, 32) // this is not 100% correct, rounds too low and salt is missing
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(text) < aes.BlockSize {
|
||||
return nil, errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
iv := text[:aes.BlockSize]
|
||||
text = text[aes.BlockSize:]
|
||||
cfb := cipher.NewCFBDecrypter(block, iv)
|
||||
cfb.XORKeyStream(text, text)
|
||||
|
||||
data, err := base64.StdEncoding.DecodeString(string(text))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
22
cryptext/aes_test.go
Normal file
22
cryptext/aes_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package cryptext
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncryptAESSimple(t *testing.T) {
|
||||
|
||||
pw := []byte("hunter12")
|
||||
|
||||
str1 := []byte("Hello World")
|
||||
|
||||
str2, err := EncryptAESSimple(pw, str1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
str3, err := DecryptAESSimple(pw, str2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
assertEqual(t, string(str1), string(str3))
|
||||
}
|
116
dataext/stack.go
Normal file
116
dataext/stack.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package dataext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var ErrEmptyStack = errors.New("stack is empty")
|
||||
|
||||
type Stack[T any] struct {
|
||||
lock *sync.Mutex
|
||||
data []T
|
||||
}
|
||||
|
||||
func NewStack[T any](threadsafe bool, initialCapacity int) *Stack[T] {
|
||||
var lck *sync.Mutex = nil
|
||||
if threadsafe {
|
||||
lck = &sync.Mutex{}
|
||||
}
|
||||
return &Stack[T]{
|
||||
lock: lck,
|
||||
data: make([]T, 0, initialCapacity),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Push(v T) {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
s.data = append(s.data, v)
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Pop() (T, error) {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
l := len(s.data)
|
||||
if l == 0 {
|
||||
return *new(T), ErrEmptyStack
|
||||
}
|
||||
|
||||
result := s.data[l-1]
|
||||
s.data = s.data[:l-1]
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Stack[T]) OptPop() *T {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
l := len(s.data)
|
||||
if l == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := s.data[l-1]
|
||||
s.data = s.data[:l-1]
|
||||
|
||||
return langext.Ptr(result)
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Peek() (T, error) {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
l := len(s.data)
|
||||
|
||||
if l == 0 {
|
||||
return *new(T), ErrEmptyStack
|
||||
}
|
||||
|
||||
return s.data[l-1], nil
|
||||
}
|
||||
|
||||
func (s *Stack[T]) OptPeek() *T {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
l := len(s.data)
|
||||
|
||||
if l == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return langext.Ptr(s.data[l-1])
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Length() int {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
return len(s.data)
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Empty() bool {
|
||||
if s.lock != nil {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
}
|
||||
|
||||
return len(s.data) == 0
|
||||
}
|
@@ -34,3 +34,7 @@ func IsNil(i interface{}) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func PtrEquals[T comparable](v1 *T, v2 *T) bool {
|
||||
return (v1 == nil && v2 == nil) || (v1 != nil && v2 != nil && *v1 == *v2)
|
||||
}
|
||||
|
@@ -37,3 +37,21 @@ func IsSliceSorted[T any](arr []T, less func(v1, v2 T) bool) bool {
|
||||
return less(arr[i1], arr[i2])
|
||||
})
|
||||
}
|
||||
|
||||
func SortBy[TElem any, TSel OrderedConstraint](arr []TElem, selector func(v TElem) TSel) {
|
||||
sort.Slice(arr, func(i1, i2 int) bool {
|
||||
return selector(arr[i1]) < selector(arr[i2])
|
||||
})
|
||||
}
|
||||
|
||||
func SortByStable[TElem any, TSel OrderedConstraint](arr []TElem, selector func(v TElem) TSel) {
|
||||
sort.SliceStable(arr, func(i1, i2 int) bool {
|
||||
return selector(arr[i1]) < selector(arr[i2])
|
||||
})
|
||||
}
|
||||
|
||||
func IsSortedBy[TElem any, TSel OrderedConstraint](arr []TElem, selector func(v TElem) TSel) {
|
||||
sort.SliceStable(arr, func(i1, i2 int) bool {
|
||||
return selector(arr[i1]) < selector(arr[i2])
|
||||
})
|
||||
}
|
||||
|
130
rext/wrapper.go
Normal file
130
rext/wrapper.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package rext
|
||||
|
||||
import "regexp"
|
||||
|
||||
type Regex interface {
|
||||
IsMatch(haystack string) bool
|
||||
MatchFirst(haystack string) (RegexMatch, bool)
|
||||
MatchAll(haystack string) []RegexMatch
|
||||
ReplaceAll(haystack string, repl string, literal bool) string
|
||||
ReplaceAllFunc(haystack string, repl func(string) string) string
|
||||
RemoveAll(haystack string) string
|
||||
GroupCount() int
|
||||
}
|
||||
|
||||
type regexWrapper struct {
|
||||
rex *regexp.Regexp
|
||||
subnames []string
|
||||
}
|
||||
|
||||
type RegexMatch struct {
|
||||
haystack string
|
||||
submatchesIndex []int
|
||||
subnames []string
|
||||
}
|
||||
|
||||
type RegexMatchGroup struct {
|
||||
haystack string
|
||||
start int
|
||||
end int
|
||||
}
|
||||
|
||||
func W(rex *regexp.Regexp) Regex {
|
||||
return ®exWrapper{rex: rex, subnames: rex.SubexpNames()}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
func (w *regexWrapper) IsMatch(haystack string) bool {
|
||||
return w.rex.MatchString(haystack)
|
||||
}
|
||||
|
||||
func (w *regexWrapper) MatchFirst(haystack string) (RegexMatch, bool) {
|
||||
res := w.rex.FindStringSubmatchIndex(haystack)
|
||||
if res == nil {
|
||||
return RegexMatch{}, false
|
||||
}
|
||||
|
||||
return RegexMatch{haystack: haystack, submatchesIndex: res, subnames: w.subnames}, true
|
||||
}
|
||||
|
||||
func (w *regexWrapper) MatchAll(haystack string) []RegexMatch {
|
||||
resarr := w.rex.FindAllStringSubmatchIndex(haystack, -1)
|
||||
|
||||
matches := make([]RegexMatch, 0, len(resarr))
|
||||
for _, res := range resarr {
|
||||
matches = append(matches, RegexMatch{haystack: haystack, submatchesIndex: res, subnames: w.subnames})
|
||||
}
|
||||
|
||||
return matches
|
||||
}
|
||||
|
||||
func (w *regexWrapper) ReplaceAll(haystack string, repl string, literal bool) string {
|
||||
if literal {
|
||||
// do not expand placeholder aka $1, $2, ...
|
||||
return w.rex.ReplaceAllLiteralString(haystack, repl)
|
||||
} else {
|
||||
return w.rex.ReplaceAllString(haystack, repl)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *regexWrapper) ReplaceAllFunc(haystack string, repl func(string) string) string {
|
||||
return w.rex.ReplaceAllStringFunc(haystack, repl)
|
||||
}
|
||||
|
||||
func (w *regexWrapper) RemoveAll(haystack string) string {
|
||||
return w.rex.ReplaceAllLiteralString(haystack, "")
|
||||
}
|
||||
|
||||
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
|
||||
func (w *regexWrapper) GroupCount() int {
|
||||
return len(w.subnames) - 1
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
func (m RegexMatch) FullMatch() RegexMatchGroup {
|
||||
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[0], end: m.submatchesIndex[1]}
|
||||
}
|
||||
|
||||
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
|
||||
func (m RegexMatch) GroupCount() int {
|
||||
return len(m.subnames) - 1
|
||||
}
|
||||
|
||||
// GroupByIndex returns the value of a matched group (group 0 == whole match)
|
||||
func (m RegexMatch) GroupByIndex(idx int) RegexMatchGroup {
|
||||
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
|
||||
}
|
||||
|
||||
// GroupByName returns the value of a matched group (group 0 == whole match)
|
||||
func (m RegexMatch) GroupByName(name string) RegexMatchGroup {
|
||||
for idx, subname := range m.subnames {
|
||||
if subname == name {
|
||||
return RegexMatchGroup{haystack: m.haystack, start: m.submatchesIndex[idx*2], end: m.submatchesIndex[idx*2+1]}
|
||||
}
|
||||
}
|
||||
panic("failed to find regex-group by name")
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
func (g RegexMatchGroup) Value() string {
|
||||
return g.haystack[g.start:g.end]
|
||||
}
|
||||
|
||||
func (g RegexMatchGroup) Start() int {
|
||||
return g.start
|
||||
}
|
||||
|
||||
func (g RegexMatchGroup) End() int {
|
||||
return g.end
|
||||
}
|
||||
|
||||
func (g RegexMatchGroup) Range() (int, int) {
|
||||
return g.start, g.end
|
||||
}
|
||||
|
||||
func (g RegexMatchGroup) Length() int {
|
||||
return g.end - g.start
|
||||
}
|
Reference in New Issue
Block a user