Created
September 10, 2024 11:14
-
-
Save Mojtaba-Shafaei/6e0d3d5ed4e60f97a1b793060e038c94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.fl3xx.sales.utils | |
| import android.content.Context | |
| import android.content.pm.* | |
| import android.content.pm.PackageManager.NameNotFoundException | |
| import android.util.Base64 | |
| import java.security.MessageDigest | |
| object SignatureUtil { | |
| fun printAppSignature(context: Context) { | |
| try { | |
| val packageInfo: PackageInfo = | |
| context.packageManager.getPackageInfo( | |
| context.packageName, | |
| PackageManager.GET_SIGNATURES, | |
| ) | |
| for (signature in packageInfo.signatures) { | |
| // For SHA-1 | |
| val signatureBytes = signature.toByteArray() | |
| val base64Signature: String = Base64.encodeToString(signatureBytes, Base64.NO_WRAP) | |
| println("AppSignature:Base64 Signature: $base64Signature") | |
| // Optionally, you can convert to SHA-1 hex format | |
| try { | |
| val md = MessageDigest.getInstance("SHA") | |
| val digest = md.digest(signatureBytes) | |
| val hexString = StringBuilder() | |
| for (b in digest) { | |
| val hex = Integer.toHexString(0xFF and b.toInt()) | |
| if (hex.length == 1) { | |
| hexString.append('0') | |
| } | |
| hexString.append(hex) | |
| } | |
| println("AppSignature:SHA-1 Signature: $hexString") | |
| } catch (e: Exception) { | |
| println("AppSignature:Error generating SHA-1:" + e.message) | |
| } | |
| } | |
| } catch (e: NameNotFoundException) { | |
| println("AppSignature:Package not found: ${e.message}") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment