Correctly display large integers instead of rounding off to base64 in the base64 encoding of the modulus

This commit is contained in:
Tracker-Friendly 2024-05-04 18:36:39 +01:00
parent 36123f3eee
commit 1e797f5b5c
1 changed files with 15 additions and 4 deletions

19
main.go
View File

@ -34,7 +34,7 @@ var (
exponent int exponent int
) )
func Int64ToBase64(num int64) (string, error) { func Int64ToBase64URL(num int64) (string, error) {
numBytes := make([]byte, 8) numBytes := make([]byte, 8)
binary.BigEndian.PutUint64(numBytes, uint64(num)) binary.BigEndian.PutUint64(numBytes, uint64(num))
startIndex := 0 startIndex := 0
@ -42,7 +42,18 @@ func Int64ToBase64(num int64) (string, error) {
startIndex++ startIndex++
} }
trimmedBytes := numBytes[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 return encoded, nil
} }
@ -1271,14 +1282,14 @@ func main() {
}) })
router.GET("/.well-known/jwks.json", func(c *gin.Context) { router.GET("/.well-known/jwks.json", func(c *gin.Context) {
mod, err := Int64ToBase64(modulus.Int64()) mod, err := BigIntToBase64URL(modulus)
if err != nil { if err != nil {
log.Println("[ERROR] Unknown in /well-known/jwks.json modulus at", strconv.FormatInt(time.Now().Unix(), 10)+":", err) 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"}) c.JSON(500, gin.H{"error": "Unknown error occured"})
return return
} }
exp, err := Int64ToBase64(int64(exponent)) exp, err := Int64ToBase64URL(int64(exponent))
if err != nil { if err != nil {
log.Println("[ERROR] Unknown in /well-known/jwks.json exponent at", strconv.FormatInt(time.Now().Unix(), 10)+":", err) 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"}) c.JSON(500, gin.H{"error": "Unknown error occured"})