From 1e797f5b5ce8c629ab179d24ae0efae9b3d895cf Mon Sep 17 00:00:00 2001 From: Arzumify Date: Sat, 4 May 2024 18:36:39 +0100 Subject: [PATCH] Correctly display large integers instead of rounding off to base64 in the base64 encoding of the modulus --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 4904bc3..22df2fc 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ var ( exponent int ) -func Int64ToBase64(num int64) (string, error) { +func Int64ToBase64URL(num int64) (string, error) { numBytes := make([]byte, 8) binary.BigEndian.PutUint64(numBytes, uint64(num)) startIndex := 0 @@ -42,7 +42,18 @@ func Int64ToBase64(num int64) (string, error) { startIndex++ } trimmedBytes := numBytes[startIndex:] - encoded := base64.StdEncoding.EncodeToString(trimmedBytes) + encoded := base64.URLEncoding.EncodeToString(trimmedBytes) + return encoded, nil +} + +func BigIntToBase64URL(num *big.Int) (string, error) { + numBytes := num.Bytes() + startIndex := 0 + for startIndex < len(numBytes) && numBytes[startIndex] == 0 { + startIndex++ + } + trimmedBytes := numBytes[startIndex:] + encoded := base64.URLEncoding.EncodeToString(trimmedBytes) return encoded, nil } @@ -1271,14 +1282,14 @@ func main() { }) router.GET("/.well-known/jwks.json", func(c *gin.Context) { - mod, err := Int64ToBase64(modulus.Int64()) + mod, err := BigIntToBase64URL(modulus) if err != nil { log.Println("[ERROR] Unknown in /well-known/jwks.json modulus at", strconv.FormatInt(time.Now().Unix(), 10)+":", err) c.JSON(500, gin.H{"error": "Unknown error occured"}) return } - exp, err := Int64ToBase64(int64(exponent)) + exp, err := Int64ToBase64URL(int64(exponent)) if err != nil { log.Println("[ERROR] Unknown in /well-known/jwks.json exponent at", strconv.FormatInt(time.Now().Unix(), 10)+":", err) c.JSON(500, gin.H{"error": "Unknown error occured"})