费话少说,直接上代码

md5, sha1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
+ (NSString *)md5:(NSString *)str
{
  if ([str length] == 0)
    return @"";
  
  unsigned char result[CC_MD5_DIGEST_LENGTH];
  CC_MD5([str UTF8String], (CC_LONG)strlen([str UTF8String]) , result);
  NSMutableString *digest = [NSMutableString string];
  for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    [digest appendFormat:@"%02X", result[i]];
  return [digest lowercaseString];
}

+ (NSString *)sha1:(NSString *)input
{
  if ([input length] == 0)
    return @"";
  
  const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
  NSData *data = [NSData dataWithBytes:cstr length:input.length];
  
  uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  
  CC_SHA1(data.bytes, data.length, digest);
  
  NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  
  for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
    [output appendFormat:@"%02X", digest[i]];
  }
  
  return output;
}

10进制转16进制

1
NSString *num16Str = [NSString stringWithFormat:@"%x", numBase10];

16进制转10进制

1
NSString *num10Str = [NSString stringWithFormat:@"%d", strtoul([numBase16 UTF8String], 0, 16)];