diff --git a/collections/node.go b/collections/node.go new file mode 100644 index 0000000..0224625 --- /dev/null +++ b/collections/node.go @@ -0,0 +1 @@ +package collections diff --git a/crypto/rsa.go b/crypto/rsa.go index b39c97c..895f3ed 100644 --- a/crypto/rsa.go +++ b/crypto/rsa.go @@ -54,6 +54,24 @@ func NewRsa(h Hash, opts ...rsaOption) (*rsaInstance, error) { return o, nil } +func ParsePKCS8PrivateKey(p []byte) rsaOption { + return func(o *rsaInstance) error { + block, _ := pem.Decode(p) + if block == nil { + return errors.New("failed to decode private key") + } + + prk, err := x509.ParsePKCS8PrivateKey(block.Bytes) + if err != nil { + return err + } + + o.prk = prk.(*rsa.PrivateKey) + + return nil + } +} + func ParsePKCS1PrivateKey(p []byte) rsaOption { return func(o *rsaInstance) error { block, _ := pem.Decode(p) diff --git a/crypto/sm2.go b/crypto/sm2.go index 53068f5..3027fa2 100644 --- a/crypto/sm2.go +++ b/crypto/sm2.go @@ -3,7 +3,6 @@ package crypto import ( "crypto/rand" "errors" - "fmt" s "github.com/tjfoc/gmsm/sm2" x "github.com/tjfoc/gmsm/x509" @@ -54,8 +53,6 @@ func NewSm2(opts ...option) (*sm2Instance, error) { func ParseSm2PrivateKey(p []byte, pwd []byte) option { return func(so *sm2Instance) error { - fmt.Println(string(p)) - priv, err := x.ReadPrivateKeyFromPem(p, pwd) if err != nil { return err diff --git a/hash/hash_test.go b/hash/hash_test.go index d1d6944..0dc6f3e 100644 --- a/hash/hash_test.go +++ b/hash/hash_test.go @@ -7,8 +7,19 @@ import ( "testing" "github.com/charlienet/go-mixed/hash" + "github.com/charlienet/go-mixed/rand" + "github.com/stretchr/testify/assert" ) +func TestHashComplie(t *testing.T) { + abc, err := hash.New("MD5") + if err != nil { + + } + b, _ := hex.DecodeString(rand.Hex.Generate(16)) + assert.False(t, abc.Verify([]byte("source"), b)) +} + func TestEncode(t *testing.T) { t.Log(hash.Sha1([]byte{0x31}).Base64()) t.Log(hash.Sha1([]byte{0x31}).Hex()) diff --git a/maps/concurrent_map.go b/maps/concurrent_map.go index 7af9b02..d0e03d6 100644 --- a/maps/concurrent_map.go +++ b/maps/concurrent_map.go @@ -49,6 +49,17 @@ func (m *ConcurrnetMap[K, V]) Exist(key K) bool { return mm.Exist(key) } +func (m *ConcurrnetMap[K, V]) Iter() <-chan *Entry[K, V] { + num := int(m.numOfBuckets) + ch := make(chan *Entry[K, V], m.Count()) + for i := 0; i < num; i++ { + c := m.buckets[i].Iter() + ch <- <-c + } + + return ch +} + func (m *ConcurrnetMap[K, V]) ForEach(f func(K, V)) { var wg sync.WaitGroup diff --git a/maps/hash_map.go b/maps/hash_map.go index 8b14fb1..d3a04b4 100644 --- a/maps/hash_map.go +++ b/maps/hash_map.go @@ -34,6 +34,22 @@ func (m *hashMap[K, V]) Exist(key K) bool { return ok } +func (m *hashMap[K, V]) Iter() <-chan *Entry[K, V] { + ch := make(chan *Entry[K, V], m.Count()) + go func() { + for k, v := range m.m { + ch <- &Entry[K, V]{ + Key: k, + Value: v, + } + } + + close(ch) + }() + + return ch +} + func (m *hashMap[K, V]) ForEach(f func(K, V)) { for k, v := range m.m { f(k, v) diff --git a/maps/map.go b/maps/map.go index 340eefa..41bde33 100644 --- a/maps/map.go +++ b/maps/map.go @@ -10,5 +10,11 @@ type Map[K constraints.Ordered, V any] interface { Clone() Map[K, V] Clear() Count() int + Iter() <-chan *Entry[K, V] ForEach(f func(K, V)) } + +type Entry[K constraints.Ordered, V any] struct { + Key K + Value V +} diff --git a/maps/rwlock_map.go b/maps/rwlock_map.go index 7bdffd7..cb2d0cb 100644 --- a/maps/rwlock_map.go +++ b/maps/rwlock_map.go @@ -49,6 +49,13 @@ func (m *rw_map[K, V]) Count() int { return m.m.Count() } +func (m *rw_map[K, V]) Iter() <-chan *Entry[K, V] { + m.mu.RLock() + defer m.mu.RUnlock() + + return m.m.Iter() +} + func (m *rw_map[K, V]) ForEach(f func(K, V)) { m.mu.RLock() defer m.mu.RUnlock() diff --git a/maps/sort_map.go b/maps/sort_map.go index 69f6c41..d1388db 100644 --- a/maps/sort_map.go +++ b/maps/sort_map.go @@ -64,6 +64,23 @@ func (m *sorted_map[K, V]) Clone() Map[K, V] { return &sorted_map[K, V]{maps: m.maps.Clone(), keys: getKeys(m.maps)} } +func (m *sorted_map[K, V]) Iter() <-chan *Entry[K, V] { + c := make(chan *Entry[K, V], m.Count()) + go func() { + for _, k := range m.keys { + v, _ := m.maps.Get(k) + + c <- &Entry[K, V]{ + Key: k, + Value: v, + } + } + close(c) + }() + + return c +} + func (m *sorted_map[K, V]) ForEach(f func(K, V)) { m.maps.ForEach(f) } diff --git a/sets/hash_set.go b/sets/hash_set.go index 66e6452..e3a761b 100644 --- a/sets/hash_set.go +++ b/sets/hash_set.go @@ -25,7 +25,7 @@ func (s hash_set[T]) Contain(value T) bool { func (s hash_set[T]) Clone() hash_set[T] { set := NewHashSet[T]() - set.Add(s.Values()...) + set.Add(s.ToSlice()...) return set } @@ -38,7 +38,7 @@ func (s hash_set[T]) Iterate(fn func(value T)) { // Union creates a new set contain all element of set s and other func (s hash_set[T]) Union(other hash_set[T]) hash_set[T] { set := s.Clone() - set.Add(other.Values()...) + set.Add(other.ToSlice()...) return set } @@ -54,7 +54,7 @@ func (s hash_set[T]) Intersection(other hash_set[T]) hash_set[T] { return set } -func (s hash_set[T]) Values() []T { +func (s hash_set[T]) ToSlice() []T { values := make([]T, 0, s.Size()) s.Iterate(func(value T) { values = append(values, value) diff --git a/sets/set.go b/sets/set.go index fba7e98..a2d8d34 100644 --- a/sets/set.go +++ b/sets/set.go @@ -1,4 +1,8 @@ package sets type Set[T comparable] interface { + Add(values ...T) + Remove(v T) + Contain(value T) bool + IsEmpty() bool } diff --git a/sets/sorted_set.go b/sets/sorted_set.go index 7c9d6e4..a366676 100644 --- a/sets/sorted_set.go +++ b/sets/sorted_set.go @@ -5,4 +5,12 @@ type sorted_set[T comparable] struct { set Set[T] } +func NewSortedSet[T comparable]() *sorted_set[T] { + return &sorted_set[T]{ + set: NewHashSet[T](), + } +} +func (s *sorted_set[T]) ToSlice() []T { + return s.sorted +}