Skip to content

Commit

Permalink
Remove encode, encrypt and random from typeutil (see new packages).
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni-vonage committed May 3, 2024
1 parent 16a394f commit f374405
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 1,687 deletions.
4 changes: 2 additions & 2 deletions pkg/kafka/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/segmentio/kafka-go"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Consumer) HealthCheck(ctx context.Context) error {
// DefaultMessageDecodeFunc is the default function to decode a message for ReceiveData().
// The value underlying data must be a pointer to the correct type for the next data item received.
func DefaultMessageDecodeFunc(_ context.Context, msg []byte, data any) error {
return typeutil.ByteDecode(msg, data) //nolint:wrapcheck
return encode.ByteDecode(msg, data) //nolint:wrapcheck
}

// ReceiveData retrieves a message from the queue and extract its content in the data.
Expand Down
4 changes: 2 additions & 2 deletions pkg/kafka/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/segmentio/kafka-go"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func (p *Producer) Send(ctx context.Context, msg []byte) error {

// DefaultMessageEncodeFunc is the default function to encode the input data for SendData().
func DefaultMessageEncodeFunc(_ context.Context, data any) ([]byte, error) {
return typeutil.ByteEncode(data) //nolint:wrapcheck
return encode.ByteEncode(data) //nolint:wrapcheck
}

// SendData delivers the specified data as encoded message to the queue.
Expand Down
4 changes: 2 additions & 2 deletions pkg/kafkacgo/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/confluentinc/confluent-kafka-go/kafka"
)

Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *Consumer) Receive() ([]byte, error) {
// DefaultMessageDecodeFunc is the default function to decode a message for ReceiveData().
// The value underlying data must be a pointer to the correct type for the next data item received.
func DefaultMessageDecodeFunc(_ context.Context, msg []byte, data any) error {
return typeutil.ByteDecode(msg, data) //nolint:wrapcheck
return encode.ByteDecode(msg, data) //nolint:wrapcheck
}

// ReceiveData retrieves a message from the queue and extract its content in the data.
Expand Down
4 changes: 2 additions & 2 deletions pkg/kafkacgo/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/confluentinc/confluent-kafka-go/kafka"
)

Expand Down Expand Up @@ -72,7 +72,7 @@ func (p *Producer) Send(topic string, msg []byte) error {

// DefaultMessageEncodeFunc is the default function to encode the input data for SendData().
func DefaultMessageEncodeFunc(_ context.Context, _ string, data any) ([]byte, error) {
return typeutil.ByteEncode(data) //nolint:wrapcheck
return encode.ByteEncode(data) //nolint:wrapcheck
}

// SendData delivers the specified data as encoded message to the queue.
Expand Down
18 changes: 12 additions & 6 deletions pkg/passwordhash/passwordhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"fmt"
"runtime"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/Vonage/gosrvlib/pkg/encrypt"
"github.com/Vonage/gosrvlib/pkg/random"
"golang.org/x/crypto/argon2"
)

Expand Down Expand Up @@ -98,6 +100,9 @@ type Params struct {
// maxPLen is the maximum length of the input password (Message string P).
// It must have a length not greater than 2^(32)-1 bytes.
maxPLen uint32

// rnd is the random generator.
rnd *random.Rnd
}

// Hashed contains the hashed password key and hashing parameters.
Expand Down Expand Up @@ -125,6 +130,7 @@ func defaultParams() *Params {
Threads: uint8(max(minThreads, min(runtime.NumCPU(), maxThreads))),
minPLen: DefaultMinPasswordLength,
maxPLen: DefaultMaxPasswordLength,
rnd: random.New(nil),
}
}

Expand Down Expand Up @@ -154,7 +160,7 @@ func (ph *Params) passwordHashData(password string) (*Hashed, error) {
return nil, fmt.Errorf("the password is too long: %d > %d", len(password), ph.maxPLen)
}

salt, err := typeutil.RandomBytes(typeutil.RandReader, int(ph.SaltLen))
salt, err := ph.rnd.RandomBytes(int(ph.SaltLen))
if err != nil {
return nil, err //nolint:wrapcheck
}
Expand Down Expand Up @@ -200,15 +206,15 @@ func (ph *Params) PasswordHash(password string) (string, error) {
return "", err
}

return typeutil.Serialize(data) //nolint:wrapcheck
return encode.Serialize(data) //nolint:wrapcheck
}

// PasswordVerify verifies if a given password matches a hashed password generated with the PasswordHash method.
// It returns true if the password matches the hashed password, otherwise false.
func (ph *Params) PasswordVerify(password, hash string) (bool, error) {
data := &Hashed{}

err := typeutil.Deserialize(hash, data)
err := encode.Deserialize(hash, data)
if err != nil {
return false, fmt.Errorf("unable to decode the hash string: %w", err)
}
Expand All @@ -224,14 +230,14 @@ func (ph *Params) EncryptPasswordHash(key []byte, password string) (string, erro
return "", err
}

return typeutil.EncryptSerializeAny(key, data) //nolint:wrapcheck
return encrypt.EncryptSerializeAny(key, data) //nolint:wrapcheck
}

// EncryptPasswordVerify extends the PasswordVerify method by decrypting the password hash using the provided key (pepper).
func (ph *Params) EncryptPasswordVerify(key []byte, password, hash string) (bool, error) {
data := &Hashed{}

err := typeutil.DecryptSerializeAny(key, hash, data)
err := encrypt.DecryptSerializeAny(key, hash, data)
if err != nil {
return false, fmt.Errorf("unable to decode the hash string: %w", err)
}
Expand Down
26 changes: 10 additions & 16 deletions pkg/passwordhash/passwordhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"testing/iotest"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/random"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/argon2"
)
Expand Down Expand Up @@ -42,8 +42,9 @@ func TestNew(t *testing.T) {
require.Equal(t, uint32(128), p.maxPLen)
}

//nolint:paralleltest
func Test_passwordHashData(t *testing.T) {
t.Parallel()

p := New()

hash, err := p.passwordHashData("test-password")
Expand All @@ -65,10 +66,7 @@ func Test_passwordHashData(t *testing.T) {
require.Error(t, err)
require.Empty(t, hash)

rr := typeutil.RandReader
defer func() { typeutil.RandReader = rr }()

typeutil.RandReader = iotest.ErrReader(errors.New("test-rand-reader-error"))
p.rnd = random.New(iotest.ErrReader(errors.New("test-rand-reader-error")))

hash, err = p.passwordHashData("test")

Expand Down Expand Up @@ -113,19 +111,17 @@ func Test_passwordHashData_passwordVerifyData(t *testing.T) {
require.False(t, ok)
}

//nolint:paralleltest
func TestPasswordHash(t *testing.T) {
t.Parallel()

p := New()

hash, err := p.PasswordHash("TestPasswordString")

require.NoError(t, err)
require.NotEmpty(t, hash)

rr := typeutil.RandReader
defer func() { typeutil.RandReader = rr }()

typeutil.RandReader = iotest.ErrReader(errors.New("test-rand-reader-error"))
p.rnd = random.New(iotest.ErrReader(errors.New("test-rand-reader-error")))

_, err = p.PasswordHash("test")

Expand Down Expand Up @@ -168,8 +164,9 @@ func Test_PasswordHash_PasswordVerify(t *testing.T) {
require.True(t, ok)
}

//nolint:paralleltest
func Test_EncryptPasswordHash(t *testing.T) {
t.Parallel()

p := New()

key := []byte("0123456789012345")
Expand All @@ -180,10 +177,7 @@ func Test_EncryptPasswordHash(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, hash)

rr := typeutil.RandReader
defer func() { typeutil.RandReader = rr }()

typeutil.RandReader = iotest.ErrReader(errors.New("test-rand-reader-error"))
p.rnd = random.New(iotest.ErrReader(errors.New("test-rand-reader-error")))

hash, err = p.EncryptPasswordHash(key, secret)

Expand Down
4 changes: 2 additions & 2 deletions pkg/randkey/randkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"strings"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/random"
)

// RandKey stores the random key.
Expand All @@ -17,7 +17,7 @@ type RandKey struct {

// New generates a new uint64 random key.
func New() *RandKey {
return &RandKey{key: typeutil.RandUint64()}
return &RandKey{key: random.New(nil).RandUint64()}
}

// Key returns a uint64 key.
Expand Down
6 changes: 3 additions & 3 deletions pkg/sqs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"
"strings"

"github.com/Vonage/gosrvlib/pkg/typeutil"
"github.com/Vonage/gosrvlib/pkg/encode"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
Expand Down Expand Up @@ -173,13 +173,13 @@ func (c *Client) Delete(ctx context.Context, receiptHandle string) error {

// MessageEncode encodes and serialize the input data to a string compatible with SQS.
func MessageEncode(data any) (string, error) {
return typeutil.Encode(data) //nolint:wrapcheck
return encode.Encode(data) //nolint:wrapcheck
}

// MessageDecode decodes a message encoded with MessageEncode to the provided data object.
// The value underlying data must be a pointer to the correct type for the next data item received.
func MessageDecode(msg string, data any) error {
return typeutil.Decode(msg, data) //nolint:wrapcheck
return encode.Decode(msg, data) //nolint:wrapcheck
}

// DefaultMessageEncodeFunc is the default function to encode and serialize the input data for SendData().
Expand Down
131 changes: 0 additions & 131 deletions pkg/typeutil/encode.go

This file was deleted.

Loading

0 comments on commit f374405

Please sign in to comment.