| 232 | } |
| 233 | |
| 234 | private static JSONObject getMetadata(DocumentFile dfile, int dLimit, int timeLimit, byte[] secret) |
| 235 | throws JSONException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
| 236 | String fileName = dfile.getName(); |
| 237 | long fileSize = dfile.length(); |
| 238 | String mimeType = dfile.getType(); |
| 239 | |
| 240 | if (TextUtils.isEmpty(mimeType)) |
| 241 | mimeType = Helper.guessMimeType(fileName); |
| 242 | |
| 243 | JSONObject jfile = new JSONObject(); |
| 244 | jfile.put("name", fileName); |
| 245 | jfile.put("size", fileSize); |
| 246 | jfile.put("type", mimeType); |
| 247 | |
| 248 | JSONArray jfiles = new JSONArray(); |
| 249 | jfiles.put(jfile); |
| 250 | |
| 251 | JSONObject jmanifest = new JSONObject(); |
| 252 | jmanifest.put("files", jfiles); |
| 253 | |
| 254 | JSONObject jmeta = new JSONObject(); |
| 255 | jmeta.put("name", fileName); // Shown on website |
| 256 | jmeta.put("size", fileSize); |
| 257 | jmeta.put("type", mimeType); |
| 258 | jmeta.put("manifest", jmanifest); |
| 259 | |
| 260 | Log.i("Send meta=" + jmeta); |
| 261 | |
| 262 | byte[] auth_key = new byte[64]; |
| 263 | HKDFBytesGenerator hkdf = new HKDFBytesGenerator(new SHA256Digest()); |
| 264 | hkdf.init(new HKDFParameters(secret /* ikm */, new byte[0] /* salt */, "authentication".getBytes())); |
| 265 | hkdf.generateBytes(auth_key /* okm */, 0, auth_key.length); |
| 266 | |
| 267 | byte[] meta_key = new byte[16]; |
| 268 | hkdf = new HKDFBytesGenerator(new SHA256Digest()); |
| 269 | hkdf.init(new HKDFParameters(secret /* ikm */, new byte[0] /* salt */, "metadata".getBytes())); |
| 270 | hkdf.generateBytes(meta_key /* okm */, 0, meta_key.length); |
| 271 | |
| 272 | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 273 | cipher.init(Cipher.ENCRYPT_MODE, |
| 274 | new SecretKeySpec(meta_key, "AES"), |
| 275 | new GCMParameterSpec(16 * 8, new byte[12])); |
| 276 | |
| 277 | byte[] metadata = cipher.doFinal(jmeta.toString().getBytes()); |
| 278 | |
| 279 | JSONObject jupload = new JSONObject(); |
| 280 | jupload.put("fileMetadata", Base64.encodeToString(metadata, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP)); |
| 281 | jupload.put("authorization", "send-v1 " + Base64.encodeToString(auth_key, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP)); |
| 282 | jupload.put("dlimit", dLimit); |
| 283 | jupload.put("timeLimit", timeLimit); // seconds |
| 284 | |
| 285 | return jupload; |
| 286 | } |
| 287 | |
| 288 | public interface IProgress { |
| 289 | void onProgress(int percentage); |