Skip to content

Commit

Permalink
1、验证签名方法适配低版本
Browse files Browse the repository at this point in the history
  • Loading branch information
venson1992 committed Feb 15, 2022
1 parent 914ea12 commit 4ef1dc2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/venson/apk/signer/SignActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class SignActivity : AppCompatActivity() {
// val srcFilePath = "/storage/emulated/0/25game/apps/-1702942688.apk"
// val srcFilePath = "/storage/emulated/0/25game/apps/-2034501281.apk"
val srcFilePath = "/storage/sdcard/25game/apps/-823954455.apk"
// val srcFilePath = "/storage/emulated/0/25game/apps/1180683424.apk"
val srcFile = File(srcFilePath)
printLog("srcFile=$srcFile")
return srcFile
Expand Down Expand Up @@ -124,7 +125,7 @@ class SignActivity : AppCompatActivity() {
private fun printLog(msg: String, isShowToast: Boolean = false) {
val stringBuilder = StringBuilder(mLogView.text)
if (stringBuilder.isNotEmpty()) {
stringBuilder.append("\n")
stringBuilder.append("\n\n")
}
stringBuilder.append(msg)
mLogView.text = stringBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static com.android.apksig.internal.x509.Certificate.findCertificate;
import static com.android.apksig.internal.x509.Certificate.parseCertificates;

import android.util.Base64;

import com.android.apksig.ApkVerifier.Issue;
import com.android.apksig.ApkVerifier.IssueWithParams;
import com.android.apksig.apk.ApkFormatException;
Expand Down Expand Up @@ -63,8 +65,6 @@
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -940,8 +940,8 @@ private boolean verifyManifestDigest(
V1SchemeSigner.MANIFEST_ENTRY_NAME,
jcaDigestAlgorithm,
mSignatureFileEntry.getName(),
Base64.getEncoder().encodeToString(actual),
Base64.getEncoder().encodeToString(expected));
Base64.encodeToString(actual, Base64.DEFAULT),
Base64.encodeToString(expected, Base64.DEFAULT));
verified = false;
}
}
Expand Down Expand Up @@ -982,8 +982,8 @@ private void verifyManifestMainSectionDigest(
Issue.JAR_SIG_MANIFEST_MAIN_SECTION_DIGEST_DID_NOT_VERIFY,
jcaDigestAlgorithm,
mSignatureFileEntry.getName(),
Base64.getEncoder().encodeToString(actual),
Base64.getEncoder().encodeToString(expected));
Base64.encodeToString(actual, Base64.DEFAULT),
Base64.encodeToString(expected, Base64.DEFAULT));
}
}
}
Expand Down Expand Up @@ -1035,8 +1035,8 @@ private void verifyManifestIndividualSectionDigest(
entryName,
jcaDigestAlgorithm,
mSignatureFileEntry.getName(),
Base64.getEncoder().encodeToString(actual),
Base64.getEncoder().encodeToString(expected));
Base64.encodeToString(actual, Base64.DEFAULT),
Base64.encodeToString(expected, Base64.DEFAULT));
}
}
}
Expand Down Expand Up @@ -1111,7 +1111,6 @@ public static Collection<NamedDigest> getDigestsToVerify(
String digestAttrSuffix,
int minSdkVersion,
int maxSdkVersion) {
Decoder base64Decoder = Base64.getDecoder();
List<NamedDigest> result = new ArrayList<>(1);
if (minSdkVersion < AndroidSdkVersion.JELLY_BEAN_MR2) {
// Prior to JB MR2, Android platform's logic for picking a digest algorithm to verify is
Expand Down Expand Up @@ -1140,7 +1139,7 @@ public static Collection<NamedDigest> getDigestsToVerify(
continue;
}
// Supported digest algorithm
result.add(new NamedDigest(alg, base64Decoder.decode(digestBase64)));
result.add(new NamedDigest(alg, Base64.decode(digestBase64, Base64.DEFAULT)));
break;
}
// No supported digests found -- this will fail to verify on pre-JB MR2 Androids.
Expand All @@ -1159,7 +1158,7 @@ public static Collection<NamedDigest> getDigestsToVerify(
// Attribute not found
continue;
}
byte[] digest = base64Decoder.decode(digestBase64);
byte[] digest = Base64.decode(digestBase64, Base64.DEFAULT);
byte[] digestInResult = getDigest(result, alg);
if ((digestInResult == null) || (!Arrays.equals(digestInResult, digest))) {
result.add(new NamedDigest(alg, digest));
Expand Down Expand Up @@ -1377,8 +1376,8 @@ private static Set<Signer> verifyJarEntriesAgainstManifestAndSigners(
entryName,
expectedDigest.jcaDigestAlgorithm,
V1SchemeSigner.MANIFEST_ENTRY_NAME,
Base64.getEncoder().encodeToString(actualDigest),
Base64.getEncoder().encodeToString(expectedDigest.digest));
Base64.encodeToString(actualDigest, Base64.DEFAULT),
Base64.encodeToString(expectedDigest.digest, Base64.DEFAULT));
}
}
}
Expand Down

0 comments on commit 4ef1dc2

Please sign in to comment.