@@ -489,6 +489,76 @@ + (void) readFile:(NSString *)path
489
489
}];
490
490
}
491
491
492
+ # pragma mark - hash
493
+
494
+ RCT_EXPORT_METHOD (hash:(NSString *)filepath
495
+ algorithm:(NSString *)algorithm
496
+ resolver:(RCTPromiseResolveBlock)resolve
497
+ rejecter:(RCTPromiseRejectBlock)reject)
498
+ {
499
+ BOOL fileExists = [[NSFileManager defaultManager ] fileExistsAtPath: filepath];
500
+
501
+ if (!fileExists) {
502
+ return reject (@" ENOENT" , [NSString stringWithFormat: @" ENOENT: no such file or directory, open '%@ '" , filepath], nil );
503
+ }
504
+
505
+ NSError *error = nil ;
506
+
507
+ NSDictionary *attributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: filepath error: &error];
508
+
509
+ if (error) {
510
+ return [self reject: reject withError: error];
511
+ }
512
+
513
+ if ([attributes objectForKey: NSFileType ] == NSFileTypeDirectory ) {
514
+ return reject (@" EISDIR" , @" EISDIR: illegal operation on a directory, read" , nil );
515
+ }
516
+
517
+ NSData *content = [[NSFileManager defaultManager ] contentsAtPath: filepath];
518
+
519
+ NSArray *keys = [NSArray arrayWithObjects: @" md5" , @" sha1" , @" sha224" , @" sha256" , @" sha384" , @" sha512" , nil ];
520
+
521
+ NSArray *digestLengths = [NSArray arrayWithObjects:
522
+ @CC_MD5_DIGEST_LENGTH,
523
+ @CC_SHA1_DIGEST_LENGTH,
524
+ @CC_SHA224_DIGEST_LENGTH,
525
+ @CC_SHA256_DIGEST_LENGTH,
526
+ @CC_SHA384_DIGEST_LENGTH,
527
+ @CC_SHA512_DIGEST_LENGTH,
528
+ nil ];
529
+
530
+ NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects: digestLengths forKeys: keys];
531
+
532
+ int digestLength = [[keysToDigestLengths objectForKey: algorithm] intValue ];
533
+
534
+ if (!digestLength) {
535
+ return reject (@" Error" , [NSString stringWithFormat: @" Invalid hash algorithm '%@ '" , algorithm], nil );
536
+ }
537
+
538
+ unsigned char buffer[digestLength];
539
+
540
+ if ([algorithm isEqualToString: @" md5" ]) {
541
+ CC_MD5 (content.bytes , (CC_LONG)content.length , buffer);
542
+ } else if ([algorithm isEqualToString: @" sha1" ]) {
543
+ CC_SHA1 (content.bytes , (CC_LONG)content.length , buffer);
544
+ } else if ([algorithm isEqualToString: @" sha224" ]) {
545
+ CC_SHA224 (content.bytes , (CC_LONG)content.length , buffer);
546
+ } else if ([algorithm isEqualToString: @" sha256" ]) {
547
+ CC_SHA256 (content.bytes , (CC_LONG)content.length , buffer);
548
+ } else if ([algorithm isEqualToString: @" sha384" ]) {
549
+ CC_SHA384 (content.bytes , (CC_LONG)content.length , buffer);
550
+ } else if ([algorithm isEqualToString: @" sha512" ]) {
551
+ CC_SHA512 (content.bytes , (CC_LONG)content.length , buffer);
552
+ } else {
553
+ return reject (@" Error" , [NSString stringWithFormat: @" Invalid hash algorithm '%@ '" , algorithm], nil );
554
+ }
555
+
556
+ NSMutableString *output = [NSMutableString stringWithCapacity: digestLength * 2 ];
557
+ for (int i = 0 ; i < digestLength; i++)
558
+ [output appendFormat: @" %02x " ,buffer[i]];
559
+
560
+ resolve (output);
561
+ }
492
562
493
563
# pragma mark - mkdir
494
564
0 commit comments