iOS encrypt Md5, Sha1,Base64

SHA1+Base64 & MD5:

#import "NSString+HmacSha1.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (HmacSha1)

+ (NSString *)hmacSha1:(NSString *)key text:(NSString *)text
{
    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
    uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
    NSString *strHash = [HMAC base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return strHash;
}

+ (NSString *)hmacMd5:(NSData *)data
{
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(data, (CC_LONG)data.length, result);
    NSData *HMAC = [[NSData alloc] initWithBytes:result length:CC_MD5_DIGEST_LENGTH];
    NSString *strMd5 = [HMAC base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return strMd5;
}



你可能感兴趣的:(iOS encrypt Md5, Sha1,Base64)