From b3f339c753b4b41dd58b61ce26ab38e43da3fea5 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Fri, 3 Mar 2023 14:51:33 +0100 Subject: [PATCH] improved code for token checksum calc --- management/server/personal_access_token.go | 17 +---------------- management/server/personal_access_token_test.go | 3 +-- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/management/server/personal_access_token.go b/management/server/personal_access_token.go index 07f621aa9..622ab01b7 100644 --- a/management/server/personal_access_token.go +++ b/management/server/personal_access_token.go @@ -22,20 +22,6 @@ type PersonalAccessToken struct { LastUsed time.Time } -const ( - // IEEE is by far and away the most common CRC-32 polynomial. - // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ... - IEEE = 0xedb88320 - // Castagnoli polynomial, used in iSCSI. - // Has better error detection characteristics than IEEE. - // https://dx.doi.org/10.1109/26.231911 - Castagnoli = 0x82f63b78 - // Koopman polynomial. - // Also has better error detection characteristics than IEEE. - // https://dx.doi.org/10.1109/DSN.2002.1028931 - Koopman = 0xeb31d82e -) - func CreateNewPAT(description string, expirationInDays int, createdBy User) (*PersonalAccessToken, string) { hashedToken, plainToken := generateNewToken() currentTime := time.Now().UTC() @@ -53,8 +39,7 @@ func CreateNewPAT(description string, expirationInDays int, createdBy User) (*Pe func generateNewToken() ([32]byte, string) { token := randStringRunes(30) - crc32q := crc32.MakeTable(IEEE) - checksum := crc32.Checksum([]byte(token), crc32q) + checksum := crc32.ChecksumIEEE([]byte(token)) encodedChecksum := base62.Encode(checksum) paddedChecksum := fmt.Sprintf("%06s", encodedChecksum) plainToken := "nbp_" + token + paddedChecksum diff --git a/management/server/personal_access_token_test.go b/management/server/personal_access_token_test.go index 375533068..623045c16 100644 --- a/management/server/personal_access_token_test.go +++ b/management/server/personal_access_token_test.go @@ -31,8 +31,7 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) { token := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6] tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:] - crc32q := crc32.MakeTable(IEEE) - expectedChecksum := crc32.Checksum([]byte(token), crc32q) + expectedChecksum := crc32.ChecksumIEEE([]byte(token)) actualChecksum, err := base62.Decode(tokenCheckSum) if err != nil { t.Fatal(err)