Skip to content

Commit

Permalink
feat: add stats for decrypt issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Apr 16, 2023
1 parent f348e53 commit c1bf966
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void finishDownload(
) {
try {
final File downloaded = new File(this.documentsDir, dest);
this.decryptFile(downloaded, sessionKey);
this.decryptFile(downloaded, sessionKey, version);
final String checksum;
checksum = this.getChecksum(downloaded);
this.notifyDownload(id, 71);
Expand Down Expand Up @@ -478,7 +478,7 @@ private String getChecksum(File file) throws IOException {
return enc.toLowerCase();
}

private void decryptFile(final File file, final String ivSessionKey)
private void decryptFile(final File file, final String ivSessionKey, final String version)
throws IOException {
// (str != null && !str.isEmpty())
if (
Expand Down Expand Up @@ -514,6 +514,7 @@ private void decryptFile(final File file, final String ivSessionKey)
fos.close();
} catch (GeneralSecurityException e) {
Log.i(TAG, "decryptFile fail");
this.sendStats("decrypt_fail", version);
e.printStackTrace();
throw new IOException("GeneralSecurityException");
}
Expand Down
9 changes: 5 additions & 4 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,21 @@ extension CustomError: LocalizedError {
}
}

private func decryptFile(filePath: URL, sessionKey: String) throws {
private func decryptFile(filePath: URL, sessionKey: String, version: String) throws {
if self.privateKey.isEmpty || sessionKey.isEmpty {
print("\(self.TAG) Cannot found privateKey or sessionKey")
return
}
do {
guard let rsaPrivateKey: RSAPrivateKey = .load(rsaPrivateKey: self.privateKey) else {
print("cannot decode privateKey", self.privateKey)
return
throw CustomError.cannotDecode
}

let sessionKeyArray: [String] = sessionKey.components(separatedBy: ":")
guard let ivData: Data = Data(base64Encoded: sessionKeyArray[0]) else {
print("cannot decode sessionKey", sessionKey)
return
throw CustomError.cannotDecode
}
let sessionKeyDataEncrypted: Data = Data(base64Encoded: sessionKeyArray[1])!
let sessionKeyDataDecrypted: Data = rsaPrivateKey.decrypt(data: sessionKeyDataEncrypted)!
Expand All @@ -364,6 +364,7 @@ extension CustomError: LocalizedError {
try decryptedData.write(to: filePath)
} catch {
print("\(self.TAG) Cannot decode: \(filePath.path)", error)
self.sendStats(action: "decrypt_fail", versionName: version)
throw CustomError.cannotDecode
}
}
Expand Down Expand Up @@ -470,7 +471,7 @@ extension CustomError: LocalizedError {
case .success:
self.notifyDownload(id, 71)
do {
try self.decryptFile(filePath: fileURL, sessionKey: sessionKey)
try self.decryptFile(filePath: fileURL, sessionKey: sessionKey, version: version)
checksum = self.getChecksum(filePath: fileURL)
try self.saveDownloaded(sourceZip: fileURL, id: id, base: self.documentsDir.appendingPathComponent(self.bundleDirectoryHot))
self.notifyDownload(id, 85)
Expand Down

0 comments on commit c1bf966

Please sign in to comment.