Jeffrey Liu

Little by little

  • 主页
所有文章 友链

Jeffrey Liu

Little by little

  • 主页

使用iOS自带CommonCrypto库计算MD5

2016-03-04

开发中常常会用到MD5计算,实际上iOS 5.0就已经内置了加密和哈希计算的库,这里就简单讲下用其中的CommonCrypto进行MD5计算。

Objective-C代码:

1
2
3
4
5
6
7
8
9
10
11
12
#import <CommonCrypto/CommonCrypto.h>
+(NSString *)MD5:(NSString *)str
{
const char* cstr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cstr, (CC_LONG)strlen(cstr), result);

return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]];
}

此外,CommonCrypto还支持SHA,Hmac等。

  • iOS
  • CommonCrypto
  • MD5
  • iOS开发

扫一扫,分享到微信

微信分享二维码
iOS开发中macro的使用
Mac SVN版本太旧解决办法
© 2018 Jeffrey Liu
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • xxd
  • file
  • bundle
  • Xcode
  • font
  • wget
  • download
  • mac
  • TensorFlow
  • macOS
  • pip
  • Python
  • iOS
  • Objective-C
  • Macros
  • objc
  • type-encoding
  • runtime
  • 吐槽
  • CommonCrypto
  • MD5

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Charlene
  • Meeple Life