我们经常有下载文件上的需求

为了安全我们经常需要对文件进行md5校验

那我就来给大家分享一个很方便的获取文件md5值得方法。

首先需要引用系统库文件

#include <CommonCrypto/CommonDigest.h>

 

/** 获取文件的md5值*/

+ (NSString *)getFileMD5StrFromPath:(NSString *)path

{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:path isDirectory:nil])

    {

        NSData *data = [NSData dataWithContentsOfFile:path];

        unsigned char digest[CC_MD5_DIGEST_LENGTH];

        CC_MD5( data.bytes, (CC_LONG)data.length, digest );

        NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

        for( int i = 0; i < CC_MD5_DIGEST_LENGTH; i++ )

        {

            [output appendFormat:@"%02x", digest[i]];

        }

        return output;

    }

    else

    {

        return @"";

    }

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2022-02-03
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2021-05-30
  • 2021-12-01
相关资源
相似解决方案