mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-20 09:47:49 +02:00
improved code for token checksum calc
This commit is contained in:
parent
e0fc779f58
commit
b3f339c753
@ -22,20 +22,6 @@ type PersonalAccessToken struct {
|
|||||||
LastUsed time.Time
|
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) {
|
func CreateNewPAT(description string, expirationInDays int, createdBy User) (*PersonalAccessToken, string) {
|
||||||
hashedToken, plainToken := generateNewToken()
|
hashedToken, plainToken := generateNewToken()
|
||||||
currentTime := time.Now().UTC()
|
currentTime := time.Now().UTC()
|
||||||
@ -53,8 +39,7 @@ func CreateNewPAT(description string, expirationInDays int, createdBy User) (*Pe
|
|||||||
func generateNewToken() ([32]byte, string) {
|
func generateNewToken() ([32]byte, string) {
|
||||||
token := randStringRunes(30)
|
token := randStringRunes(30)
|
||||||
|
|
||||||
crc32q := crc32.MakeTable(IEEE)
|
checksum := crc32.ChecksumIEEE([]byte(token))
|
||||||
checksum := crc32.Checksum([]byte(token), crc32q)
|
|
||||||
encodedChecksum := base62.Encode(checksum)
|
encodedChecksum := base62.Encode(checksum)
|
||||||
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
|
paddedChecksum := fmt.Sprintf("%06s", encodedChecksum)
|
||||||
plainToken := "nbp_" + token + paddedChecksum
|
plainToken := "nbp_" + token + paddedChecksum
|
||||||
|
@ -31,8 +31,7 @@ func TestPAT_GenerateToken_Checksum(t *testing.T) {
|
|||||||
token := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
|
token := tokenWithoutPrefix[:len(tokenWithoutPrefix)-6]
|
||||||
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
|
tokenCheckSum := tokenWithoutPrefix[len(tokenWithoutPrefix)-6:]
|
||||||
|
|
||||||
crc32q := crc32.MakeTable(IEEE)
|
expectedChecksum := crc32.ChecksumIEEE([]byte(token))
|
||||||
expectedChecksum := crc32.Checksum([]byte(token), crc32q)
|
|
||||||
actualChecksum, err := base62.Decode(tokenCheckSum)
|
actualChecksum, err := base62.Decode(tokenCheckSum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user