|
3 | 3 | import android.content.Context; |
4 | 4 | import android.util.Base64; |
5 | 5 |
|
| 6 | +import com.microsoft.codepush.react.diffpatch.DiffManifest; |
| 7 | +import com.microsoft.codepush.react.diffpatch.Sha256; |
| 8 | + |
6 | 9 | import com.nimbusds.jose.JWSVerifier; |
7 | 10 | import com.nimbusds.jose.crypto.RSASSAVerifier; |
8 | 11 | import com.nimbusds.jwt.SignedJWT; |
9 | 12 |
|
10 | 13 | import java.security.interfaces.*; |
11 | 14 |
|
12 | 15 | import org.json.JSONArray; |
13 | | -import org.json.JSONException; |
14 | | -import org.json.JSONObject; |
15 | 16 |
|
16 | 17 | import java.io.ByteArrayInputStream; |
17 | 18 | import java.io.File; |
18 | 19 | import java.io.FileInputStream; |
19 | 20 | import java.io.FileNotFoundException; |
20 | 21 | import java.io.IOException; |
21 | 22 | import java.io.InputStream; |
22 | | -import java.security.DigestInputStream; |
23 | 23 | import java.security.KeyFactory; |
24 | | -import java.security.MessageDigest; |
25 | | -import java.security.NoSuchAlgorithmException; |
26 | 24 | import java.security.PublicKey; |
27 | 25 | import java.security.spec.X509EncodedKeySpec; |
28 | 26 | import java.util.ArrayList; |
@@ -73,51 +71,25 @@ private static void addContentsOfFolderToManifest(String folderPath, String path |
73 | 71 | } |
74 | 72 |
|
75 | 73 | private static String computeHash(InputStream dataStream) { |
76 | | - MessageDigest messageDigest = null; |
77 | | - DigestInputStream digestInputStream = null; |
78 | 74 | try { |
79 | | - messageDigest = MessageDigest.getInstance("SHA-256"); |
80 | | - digestInputStream = new DigestInputStream(dataStream, messageDigest); |
81 | | - byte[] byteBuffer = new byte[1024 * 8]; |
82 | | - while (digestInputStream.read(byteBuffer) != -1) ; |
83 | | - } catch (NoSuchAlgorithmException | IOException e) { |
| 75 | + return Sha256.sha256Hex(dataStream); |
| 76 | + } catch (Exception e) { |
84 | 77 | // Should not happen. |
85 | 78 | throw new CodePushUnknownException("Unable to compute hash of update contents.", e); |
86 | | - } finally { |
87 | | - try { |
88 | | - if (digestInputStream != null) { |
89 | | - digestInputStream.close(); |
90 | | - } |
91 | | - if (dataStream != null) { |
92 | | - dataStream.close(); |
93 | | - } |
94 | | - } catch (IOException e) { |
95 | | - e.printStackTrace(); |
96 | | - } |
97 | 79 | } |
98 | | - |
99 | | - byte[] hash = messageDigest.digest(); |
100 | | - return String.format("%064x", new java.math.BigInteger(1, hash)); |
101 | 80 | } |
102 | 81 |
|
103 | | - public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String newPackageFolderPath) throws IOException { |
| 82 | + public static void copyNecessaryFilesFromCurrentPackage(DiffManifest diffManifest, String currentPackageFolderPath, String newPackageFolderPath) throws IOException { |
104 | 83 | if (currentPackageFolderPath == null || !new File(currentPackageFolderPath).exists()) { |
105 | 84 | CodePushUtils.log("Unable to copy files from current package during diff update, because currentPackageFolderPath is invalid."); |
106 | 85 | return; |
107 | 86 | } |
108 | 87 | FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath); |
109 | | - JSONObject diffManifest = CodePushUtils.getJsonObjectFromFile(diffManifestFilePath); |
110 | | - try { |
111 | | - JSONArray deletedFiles = diffManifest.getJSONArray("deletedFiles"); |
112 | | - for (int i = 0; i < deletedFiles.length(); i++) { |
113 | | - String fileNameToDelete = deletedFiles.getString(i); |
114 | | - File fileToDelete = new File(newPackageFolderPath, fileNameToDelete); |
115 | | - if (fileToDelete.exists()) { |
116 | | - fileToDelete.delete(); |
117 | | - } |
| 88 | + for (String fileNameToDelete : diffManifest.getDeletedFiles()) { |
| 89 | + File fileToDelete = new File(newPackageFolderPath, fileNameToDelete); |
| 90 | + if (fileToDelete.exists()) { |
| 91 | + fileToDelete.delete(); |
118 | 92 | } |
119 | | - } catch (JSONException e) { |
120 | | - throw new CodePushUnknownException("Unable to copy files from current package during diff update", e); |
121 | 93 | } |
122 | 94 | } |
123 | 95 |
|
|
0 commit comments