forked from netbirdio/netbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove dependency to external base62 package and create own methods i…
…n utils
- Loading branch information
1 parent
a3ee45b
commit 2fef52b
Showing
7 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
) | ||
|
||
const ( | ||
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | ||
base = uint32(len(alphabet)) | ||
) | ||
|
||
// EncodeBase62 encodes a uint32 value to a base62 string. | ||
func EncodeBase62(num uint32) string { | ||
if num == 0 { | ||
return string(alphabet[0]) | ||
} | ||
|
||
var encoded strings.Builder | ||
remainder := uint32(0) | ||
|
||
for num > 0 { | ||
remainder = num % base | ||
encoded.WriteByte(alphabet[remainder]) | ||
num /= base | ||
} | ||
|
||
// Reverse the encoded string | ||
encodedString := encoded.String() | ||
reversed := reverse(encodedString) | ||
return reversed | ||
} | ||
|
||
// DecodeBase62 decodes a base62 string to a uint32 value. | ||
func DecodeBase62(encoded string) (uint32, error) { | ||
var decoded uint32 | ||
strLen := len(encoded) | ||
|
||
for i, char := range encoded { | ||
index := strings.IndexRune(alphabet, char) | ||
if index < 0 { | ||
return 0, fmt.Errorf("invalid character: %c", char) | ||
} | ||
|
||
decoded += uint32(index) * uint32(math.Pow(float64(base), float64(strLen-i-1))) | ||
} | ||
|
||
return decoded, nil | ||
} | ||
|
||
// Reverse a string. | ||
func reverse(s string) string { | ||
runes := []rune(s) | ||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { | ||
runes[i], runes[j] = runes[j], runes[i] | ||
} | ||
return string(runes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestEncodeDecode(t *testing.T) { | ||
tests := []struct { | ||
num uint32 | ||
}{ | ||
{0}, | ||
{1}, | ||
{42}, | ||
{12345}, | ||
{99999}, | ||
{123456789}, | ||
} | ||
|
||
for _, tt := range tests { | ||
encoded := EncodeBase62(tt.num) | ||
decoded, err := DecodeBase62(encoded) | ||
|
||
if err != nil { | ||
t.Errorf("Decode error: %v", err) | ||
} | ||
|
||
if decoded != tt.num { | ||
t.Errorf("Decode(%v) = %v, want %v", encoded, decoded, tt.num) | ||
} | ||
} | ||
} |