From 175129831d16c893f601d9b79f1fb73a8513caca Mon Sep 17 00:00:00 2001 From: Christian Tamayo Date: Thu, 22 Nov 2018 11:15:57 +0800 Subject: [PATCH 1/3] Update fs.js --- fs.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs.js b/fs.js index e61f44312..7c5a0da8a 100644 --- a/fs.js +++ b/fs.js @@ -245,6 +245,13 @@ function hash(path: string, algorithm: string): Promise { return RNFetchBlob.hash(path, algorithm) } +function hashWithKey(path: string, algorithm: string, key: string): Promise { + if (typeof path !== 'string' || typeof algorithm !== 'string' || typeof key !== 'string') { + return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "algorithm" and/or "key"'))) + } + return RNFetchBlob.hashWithKey(path, algorithm, key) +} + function cp(path: string, dest: string): Promise { return new Promise((resolve, reject) => { if (typeof path !== 'string' || typeof dest !== 'string') { @@ -404,6 +411,7 @@ export default { pathForAppGroup, readFile, hash, + hashWithKey, exists, createFile, isDir, From 34f4290798a8f10d68cdb465f5afaa98b2024242 Mon Sep 17 00:00:00 2001 From: Christian Tamayo Date: Thu, 22 Nov 2018 11:18:06 +0800 Subject: [PATCH 2/3] Update RNFetchBlobFS.java --- .../java/com/RNFetchBlob/RNFetchBlobFS.java | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java index 101e8033a..d2db0e58a 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java @@ -9,7 +9,7 @@ import android.os.StatFs; import android.os.SystemClock; import android.util.Base64; - +import android.util.Log; import com.RNFetchBlob.Utils.PathResolver; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; @@ -29,6 +29,8 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; class RNFetchBlobFS { @@ -894,6 +896,44 @@ static void hash(String path, String algorithm, Promise promise) { } } + static void hashWithKey(String path, String algorithm, String key, Promise promise) { + try { + File file = new File(path); + + if (file.isDirectory()) { + promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory"); + return; + } + + if (!file.exists()) { + promise.reject("ENOENT", "No such file '" + path + "'"); + return; + } + + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(secretKeySpec); + + FileInputStream inputStream = new FileInputStream(path); + byte[] buffer = new byte[(int)file.length()]; + + int read; + while ((read = inputStream.read(buffer)) != -1) { + mac.update(buffer, 0, read); + } + + StringBuilder hexString = new StringBuilder(); + for (byte digestByte : mac.doFinal()){ + hexString.append(String.format("%02x", digestByte)); + } + + promise.resolve(hexString.toString()); + } catch (Exception e) { + e.printStackTrace(); + promise.reject("EUNSPECIFIED", e.getLocalizedMessage()); + } + } + /** * Create new file at path * @param path The destination path of the new file. From 5d4b81f256dcd6b0f42c0ea3c5705da1943bb76f Mon Sep 17 00:00:00 2001 From: Christian Tamayo Date: Thu, 22 Nov 2018 11:18:46 +0800 Subject: [PATCH 3/3] Update RNFetchBlob.java --- .../src/main/java/com/RNFetchBlob/RNFetchBlob.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index ac9ce5669..a791bdd53 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -292,6 +292,16 @@ public void run() { }); } + @ReactMethod + public void hashWithKey(final String path, final String algorithm, final String key, final Promise promise) { + threadPool.execute(new Runnable() { + @Override + public void run() { + RNFetchBlobFS.hashWithKey(path, algorithm, key, promise); + } + }); + } + /** * @param path Stream file path * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8` @@ -410,4 +420,4 @@ public void getSDCardDir(Promise promise) { public void getSDCardApplicationDir(Promise promise) { RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise); } -} \ No newline at end of file +}