@@ -581,6 +581,15 @@ + (void) readFile:(NSString *)path
581581
582582# pragma mark - hash
583583
584+ typedef enum {
585+ HashAlgorithmMD5,
586+ HashAlgorithmSHA1,
587+ HashAlgorithmSHA224,
588+ HashAlgorithmSHA256,
589+ HashAlgorithmSHA384,
590+ HashAlgorithmSHA512,
591+ } HashAlgorithm;
592+
584593+ (void ) hash : (NSString *)path
585594 algorithm : (NSString *)algorithm
586595 resolver : (RCTPromiseResolveBlock)resolve
@@ -611,8 +620,8 @@ + (void) hash:(NSString *)path
611620 return ;
612621 }
613622
614- NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath: filePath error: &error ];
615- if (error ) {
623+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath: path ];
624+ if (fileHandle == nil ) {
616625 reject (@" EUNKNOWN" , [NSString stringWithFormat: @" Error opening '%@ ' for reading" , path], error);
617626 return ;
618627 }
@@ -643,61 +652,80 @@ + (void) hash:(NSString *)path
643652
644653 CC_MD5_CTX md5Context;
645654 CC_SHA1_CTX sha1Context;
646- CC_SHA224_CTX sha224Context;
647655 CC_SHA256_CTX sha256Context;
648- CC_SHA384_CTX sha384Context;
649656 CC_SHA512_CTX sha512Context;
657+ HashAlgorithm hashAlgorithm;
650658
651659 if ([algorithm isEqualToString: @" md5" ]) {
652660 CC_MD5_Init (&md5Context);
661+ hashAlgorithm = HashAlgorithmMD5;
653662 } else if ([algorithm isEqualToString: @" sha1" ]) {
654- CC_MD5_Init (&sha1Context);
663+ CC_SHA1_Init (&sha1Context);
664+ hashAlgorithm = HashAlgorithmSHA1;
655665 } else if ([algorithm isEqualToString: @" sha224" ]) {
656- CC_MD5_Init (&sha224Context);
666+ CC_SHA224_Init (&sha256Context);
667+ hashAlgorithm = HashAlgorithmSHA224;
657668 } else if ([algorithm isEqualToString: @" sha256" ]) {
658- CC_MD5_Init (&sha256Context);
669+ CC_SHA256_Init (&sha256Context);
670+ hashAlgorithm = HashAlgorithmSHA256;
659671 } else if ([algorithm isEqualToString: @" sha384" ]) {
660- CC_MD5_Init (&sha384Context);
672+ CC_SHA384_Init (&sha512Context);
673+ hashAlgorithm = HashAlgorithmSHA384;
661674 } else if ([algorithm isEqualToString: @" sha512" ]) {
662- CC_MD5_Init (&sha512Context);
675+ CC_SHA512_Init (&sha512Context);
676+ hashAlgorithm = HashAlgorithmSHA512;
663677 } else {
664678 reject (@" EINVAL" , [NSString stringWithFormat: @" Invalid algorithm '%@ ', must be one of md5, sha1, sha224, sha256, sha384, sha512" , algorithm], nil );
665679 return ;
666680 }
667681
668- while ((dataChunk = [fileHandle readDataOfLength : chunkSize error: &error])) {
682+ while ((dataChunk = [fileHandle readDataUpToLength : chunkSize error: &error])) {
669683 if (error) {
670684 return reject (@" EREAD" , [NSString stringWithFormat: @" Error reading file '%@ '" , path], error);
671685 break ;
672686 }
673-
674- if ([algorithm isEqualToString: @" md5" ]) {
675- CC_MD5_Update (&md5Context);
676- } else if ([algorithm isEqualToString: @" sha1" ]) {
677- CC_SHA1_Update (&sha1Context);
678- } else if ([algorithm isEqualToString: @" sha224" ]) {
679- CC_SHA224_Update (&sha224Context);
680- } else if ([algorithm isEqualToString: @" sha256" ]) {
681- CC_SHA256_Update (&sha256Context);
682- } else if ([algorithm isEqualToString: @" sha384" ]) {
683- CC_SHA384_Update (&sha384Context);
684- } else if ([algorithm isEqualToString: @" sha512" ]) {
685- CC_SHA512_Update (&sha512Context);
687+
688+ switch (hashAlgorithm) {
689+ case HashAlgorithmMD5:
690+ CC_MD5_Update (&md5Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
691+ break ;
692+ case HashAlgorithmSHA1:
693+ CC_SHA1_Update (&sha1Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
694+ break ;
695+ case HashAlgorithmSHA224:
696+ CC_SHA224_Update (&sha256Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
697+ break ;
698+ case HashAlgorithmSHA256:
699+ CC_SHA256_Update (&sha256Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
700+ break ;
701+ case HashAlgorithmSHA384:
702+ CC_SHA384_Update (&sha512Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
703+ break ;
704+ case HashAlgorithmSHA512:
705+ CC_SHA512_Update (&sha512Context, [dataChunk bytes ], CC_LONG ([dataChunk length ]));
706+ break ;
686707 }
687708 }
688709
689- if ([algorithm isEqualToString: @" md5" ]) {
690- CC_MD5_Final (buffer, &md5Context));
691- } else if ([algorithm isEqualToString: @" sha1" ]) {
692- CC_SHA1_Final (buffer, &sha1Context));
693- } else if ([algorithm isEqualToString: @" sha224" ]) {
694- CC_SHA224_Final (buffer, &sha224Context));
695- } else if ([algorithm isEqualToString: @" sha256" ]) {
696- CC_SHA256_Final (buffer, &sha256Context));
697- } else if ([algorithm isEqualToString: @" sha384" ]) {
698- CC_SHA384_Final (buffer, &sha384Context));
699- } else if ([algorithm isEqualToString: @" sha512" ]) {
700- CC_SHA512_Final (buffer, &sha512Context));
710+ switch (hashAlgorithm) {
711+ case HashAlgorithmMD5:
712+ CC_MD5_Final (buffer, &md5Context);
713+ break ;
714+ case HashAlgorithmSHA1:
715+ CC_SHA1_Final (buffer, &sha1Context);
716+ break ;
717+ case HashAlgorithmSHA224:
718+ CC_SHA224_Final (buffer, &sha256Context);
719+ break ;
720+ case HashAlgorithmSHA256:
721+ CC_SHA256_Final (buffer, &sha256Context);
722+ break ;
723+ case HashAlgorithmSHA384:
724+ CC_SHA384_Final (buffer, &sha512Context);
725+ break ;
726+ case HashAlgorithmSHA512:
727+ CC_SHA512_Final (buffer, &sha512Context);
728+ break ;
701729 }
702730
703731 NSMutableString *output = [NSMutableString stringWithCapacity: digestLength * 2 ];
0 commit comments