public void SignFile(string inputFileName, X509Certificate2 certificate, string timestampServer, SignFileRequest signFileRequest, SignFileResponse signFileResponse) { SignFileResponseResult successResult = SignFileResponseResult.FileSigned; if (IsFileSigned(inputFileName)) { if (signFileRequest.OverwriteSignature) { successResult = SignFileResponseResult.FileResigned; } else { signFileResponse.Result = SignFileResponseResult.FileAlreadySigned; return; } } var outputFileName = inputFileName + ".signed"; try { if (string.IsNullOrEmpty(signFileRequest.HashAlgorithm) || !ApkSupportedHashAlgorithms.TryGetValue(signFileRequest.HashAlgorithm, out var digestAlgorithm)) { digestAlgorithm = null; } var isV2SigningEnabled = ".apk".Equals(Path.GetExtension(inputFileName), StringComparison.InvariantCultureIgnoreCase) && // v2 only for APKs not for JARs (digestAlgorithm == null || !digestAlgorithm.Equals(DigestAlgorithm.SHA1)) // v2 signing requires SHA256 or SHA512 ; var apkSigner = new ApkSigner(certificate, inputFileName, outputFileName) { V1SigningEnabled = true, V2SigningEnabled = isV2SigningEnabled, DigestAlgorithm = digestAlgorithm }; apkSigner.Sign(); File.Delete(inputFileName); File.Move(outputFileName, inputFileName); signFileResponse.Result = successResult; signFileResponse.FileContent = new FileStream(inputFileName, FileMode.Open, FileAccess.Read); signFileResponse.FileSize = signFileResponse.FileContent.Length; } catch { if (File.Exists(outputFileName)) { File.Delete(outputFileName); } throw; } }
private void BtnSave_Click(object sender, EventArgs e) { _manager.WriteAllOpenAssets(); _fileProvider.Save(); if (_fileProvider is ApkAssetsFileProvider) { ApkSigner s = new ApkSigner(BSConst.DebugCertificatePEM); s.Sign(_fileProvider); } CloseStuff(); }
private void SignApk(string apkFilename) { try { using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory())) { ApkSigner signer = new ApkSigner(QuestomAssets.BeatSaber.BSConst.DebugCertificatePEM); signer.Sign(apk); } UpdateStatus("APK signed!"); } catch (Exception ex) { Log.LogErr($"Exception signing the APK {apkFilename}!", ex); UpdateStatus("Error re-signing the APK!"); throw new ModException($"Exception signing the APK {apkFilename}!", ex); } }