Skip to content

A sample repository for Qr code generation and scanning jjwt token

License

Notifications You must be signed in to change notification settings

niharika2810/ZxingPlusJjwtSample

Repository files navigation

ZxingPlusJjwtSample

A demo application for generating QR code and scanning JJWT token.

using zxing-android-embedded and zxing for generating and scanning QR code.

Some Features of Zxing library:

  1. Can be used via Intents (little code required).
  2. Can be embedded in an Activity, for advanced customization of UI and logic.
  3. Scanning can be performed in landscape or portrait mode.
  4. Camera is managed in a background thread, for fast startup time.

JJWT aims to be the easiest to use and understand library for creating and verifying JSON Web Tokens (JWTs) on the JVM and Android.

JJWT is a pure Java implementation based exclusively on the JWT, JWS, JWE, JWK and JWA RFC specifications and open source under the terms of the Apache 2.0 License.

Usage

QR code generation

        val qrText = "Your qr code hash here"
        val multiFormatWriter = MultiFormatWriter()
        try {
            bitMatrix?.clear()
            val size = convertDpToPixel()
            bitMatrix = multiFormatWriter.encode(
                    qrText,
                    BarcodeFormat.QR_CODE,
                    size,
                    size
            )
            val barcodeEncoder = BarcodeEncoder()
            val bitmap = barcodeEncoder.createBitmap(bitMatrix)
            imageView.setImageBitmap(bitmap)
        } catch (e: WriterException) { //do nothing
        }

Decrypting JJWT token

        val publicKeyBytes = Base64.decode("your public key", Base64.DEFAULT)
        // create a key object from the bytes
        val keySpec =
            X509EncodedKeySpec(publicKeyBytes)
        val keyFactory = KeyFactory.getInstance("RSA")
        val publicKey = keyFactory.generatePublic(keySpec)
        return Jwts.parserBuilder().setSigningKey(publicKey).build().parseClaimsJws(jwtToken)

Scanning and Extracting result from QR code

        capture = CustomCaptureManager(this, barcodeScanner)
        capture?.setShowMissingCameraPermissionDialog(true)
        barcodeScanner.barcodeView.cameraSettings = camSettings
        capture?.setViewCaptureListener(this)
        capture?.initializeFromIntent(intent, savedInstanceState)
        capture?.decode()

In other lifecycle methods,add this-
 override fun onResume() {
        super.onResume()
        capture?.onResume()
    }

    override fun onPause() {
        super.onPause()
        capture?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        capture?.onDestroy()
    }

On getting the result,you can extract keys like this-

  //show your result here
        val claimsJws: Jws<Claims?>?
        try {
            claimsJws = decryptFile(json)
            //extract your keys from claimJws
            val stringKey = claimsJws?.body?.get("your key name", String::class.java)
            val intKey = claimsJws?.body?.get("your key name", Integer::class.java)
        } catch (e: Exception) {
            //handle exception
        }

Happy Coding!!

About

A sample repository for Qr code generation and scanning jjwt token

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published