Skip to content

Added support for hashing with secret key. Android only #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,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`
Expand Down
42 changes: 41 additions & 1 deletion android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -918,6 +920,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.
Expand Down
8 changes: 8 additions & 0 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ function hash(path: string, algorithm: string): Promise<string> {
return RNFetchBlob.hash(path, algorithm)
}

function hashWithKey(path: string, algorithm: string, key: string): Promise<string> {
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<boolean> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string' || typeof dest !== 'string') {
Expand Down Expand Up @@ -418,6 +425,7 @@ export default {
syncPathAppGroup,
readFile,
hash,
hashWithKey,
exists,
createFile,
isDir,
Expand Down