move into separate package

This commit is contained in:
Pascal Fischer 2023-05-16 12:57:56 +02:00
parent 2fef52b856
commit 873abc43bf
5 changed files with 14 additions and 14 deletions

View File

@ -1,4 +1,4 @@
package util
package base62
import (
"fmt"
@ -11,8 +11,8 @@ const (
base = uint32(len(alphabet))
)
// EncodeBase62 encodes a uint32 value to a base62 string.
func EncodeBase62(num uint32) string {
// Encode encodes a uint32 value to a base62 string.
func Encode(num uint32) string {
if num == 0 {
return string(alphabet[0])
}
@ -32,8 +32,8 @@ func EncodeBase62(num uint32) string {
return reversed
}
// DecodeBase62 decodes a base62 string to a uint32 value.
func DecodeBase62(encoded string) (uint32, error) {
// Decode decodes a base62 string to a uint32 value.
func Decode(encoded string) (uint32, error) {
var decoded uint32
strLen := len(encoded)

View File

@ -1,4 +1,4 @@
package util
package base62
import (
"testing"
@ -17,8 +17,8 @@ func TestEncodeDecode(t *testing.T) {
}
for _, tt := range tests {
encoded := EncodeBase62(tt.num)
decoded, err := DecodeBase62(encoded)
encoded := Encode(tt.num)
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Decode error: %v", err)

View File

@ -21,13 +21,13 @@ import (
"github.com/rs/xid"
log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/base62"
nbdns "github.com/netbirdio/netbird/dns"
"github.com/netbirdio/netbird/management/server/activity"
"github.com/netbirdio/netbird/management/server/idp"
"github.com/netbirdio/netbird/management/server/jwtclaims"
"github.com/netbirdio/netbird/management/server/status"
"github.com/netbirdio/netbird/route"
"github.com/netbirdio/netbird/util"
)
const (
@ -1174,7 +1174,7 @@ func (am *DefaultAccountManager) GetAccountFromPAT(token string) (*Account, *Use
secret := token[len(PATPrefix) : len(PATPrefix)+PATSecretLength]
encodedChecksum := token[len(PATPrefix)+PATSecretLength : len(PATPrefix)+PATSecretLength+PATChecksumLength]
verificationChecksum, err := util.DecodeBase62(encodedChecksum)
verificationChecksum, err := base62.Decode(encodedChecksum)
if err != nil {
return nil, nil, nil, fmt.Errorf("token checksum decoding failed: %w", err)
}

View File

@ -10,7 +10,7 @@ import (
b "github.com/hashicorp/go-secure-stdlib/base62"
"github.com/rs/xid"
"github.com/netbirdio/netbird/util"
"github.com/netbirdio/netbird/base62"
)
const (
@ -72,7 +72,7 @@ func generateNewToken() (string, string, error) {
}
checksum := crc32.ChecksumIEEE([]byte(secret))
encodedChecksum := util.EncodeBase62(checksum)
encodedChecksum := base62.Encode(checksum)
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
plainToken := PATPrefix + secret + paddedChecksum
hashedToken := sha256.Sum256([]byte(plainToken))

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/netbirdio/netbird/util"
"github.com/netbirdio/netbird/base62"
)
func TestPAT_GenerateToken_Hashing(t *testing.T) {
@ -38,7 +38,7 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) {
var i big.Int
i.SetString(secret, 62)
expectedChecksum := crc32.ChecksumIEEE([]byte(secret))
actualChecksum, err := util.DecodeBase62(tokenCheckSum)
actualChecksum, err := base62.Decode(tokenCheckSum)
if err != nil {
t.Fatal(err)
}