Bootstrap

iOS端SHA256加密

1、创建类 SecurityUtility
2、.h文件代码:

#import <Foundation/Foundation.h>

@interface SecurityUtility : NSObject

//SHA256加密
+ (NSString*)sha256HashFor:(NSString *)input;

@end

3、.m文件代码:

#import "SecurityUtility.h"
#import <CommonCrypto/CommonDigest.h>

@implementation SecurityUtility

//SHA256加密
+ (NSString*)sha256HashFor:(NSString*)input{
    const char* str = [input UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(str, (CC_LONG)strlen(str), result);
    
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
    {
        [ret appendFormat:@"%02x",result[i]];
    }
    ret = (NSMutableString *)[ret uppercaseString];
    return ret;
}
@end

4、测试

NSString *str = [SecurityUtility sha256HashFor:@"dashidahsidias"];
NSLog(@"str:%@", str);

5、测试结果

str:03D8540484EEC7519B1493776B5D4159AC7631E9A52769488F796C55A3FD9136
;