Skip to content

Commit

Permalink
Add manual verify function to project
Browse files Browse the repository at this point in the history
  • Loading branch information
h.kheirollahpour committed Dec 15, 2023
1 parent d91f3d5 commit 4699bcf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/hkh/jwtgenerationapp/jwt/JwtUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.hkh.jwtgenerationapp.jwt

import android.os.Build
import androidx.annotation.RequiresApi
import com.hkh.jwtgenerationapp.jwt.Base64Util.base64UrlDecode
import com.hkh.jwtgenerationapp.jwt.Base64Util.base64UrlEncode
import com.hkh.jwtgenerationapp.jwt.Utils.toStandardByteArray
import java.security.Signature
import java.security.SignatureException
import java.security.interfaces.ECPrivateKey
import java.security.interfaces.ECPublicKey
import kotlin.math.max
import kotlin.math.min

Expand Down Expand Up @@ -87,4 +89,14 @@ object JwtUtil {
return joseSignature
}

@RequiresApi(Build.VERSION_CODES.O)
fun manualVerifyJWT(publicKey: ECPublicKey, jwt: String): Boolean {
val parts = jwt.split(".")
if (parts.size != 3) return false
val signature = Signature.getInstance("SHA256withECDSA")
signature.initVerify(publicKey)
signature.update("${parts[0]}.${parts[1]}".toByteArray())
return signature.verify(base64UrlDecode(parts[2]))
}

}

0 comments on commit 4699bcf

Please sign in to comment.