MCPcopy Create free account
hub / github.com/itinance/react-native-fs / hash

Method hash

android/src/main/java/com/rnfs/RNFSManager.java:317–362  ·  view source on GitHub ↗
(String filepath, String algorithm, Promise promise)

Source from the content-addressed store, hash-verified

315 }
316
317 @ReactMethod
318 public void hash(String filepath, String algorithm, Promise promise) {
319 try {
320 Map<String, String> algorithms = new HashMap<>();
321
322 algorithms.put("md5", "MD5");
323 algorithms.put("sha1", "SHA-1");
324 algorithms.put("sha224", "SHA-224");
325 algorithms.put("sha256", "SHA-256");
326 algorithms.put("sha384", "SHA-384");
327 algorithms.put("sha512", "SHA-512");
328
329 if (!algorithms.containsKey(algorithm)) throw new Exception("Invalid hash algorithm");
330
331 File file = new File(filepath);
332
333 if (file.isDirectory()) {
334 rejectFileIsDirectory(promise);
335 return;
336 }
337
338 if (!file.exists()) {
339 rejectFileNotFound(promise, filepath);
340 return;
341 }
342
343 MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
344
345 FileInputStream inputStream = new FileInputStream(filepath);
346 byte[] buffer = new byte[1024 * 10]; // 10 KB Buffer
347
348 int read;
349 while ((read = inputStream.read(buffer)) != -1) {
350 md.update(buffer, 0, read);
351 }
352
353 StringBuilder hexString = new StringBuilder();
354 for (byte digestByte : md.digest())
355 hexString.append(String.format("%02x", digestByte));
356
357 promise.resolve(hexString.toString());
358 } catch (Exception ex) {
359 ex.printStackTrace();
360 reject(promise, filepath, ex);
361 }
362 }
363
364 @ReactMethod
365 public void moveFile(final String filepath, String destPath, ReadableMap options, final Promise promise) {

Callers

nothing calls this directly

Calls 5

rejectFileIsDirectoryMethod · 0.95
rejectFileNotFoundMethod · 0.95
rejectMethod · 0.95
existsMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected