iOS SHA1 和 Base64 url_safe 加密

iOS SHA1 和 Base64 url_safe 加密

网上查找的资料SHA1加密后大部分获取的是字符串,如果还要进行下一步操作时,一定要慎用字符串,我最近在做SHA1 和 Base64 url_safe 加密,用SHA1加密后的获取字符串进行Base64编码,但是得到的数值对比安卓和后台接口得到的数值都不一样,安卓和后台SHA1可以直接获取二进制流,然后进行Base64编码,iOS  SHA1没法直接获取data数据,如果转换成字符串在进行Base64编码,得到的数据就不统一,这是一个很大的坑,一定要看好    Demo地址



#import "XWSSHA1Tool.h"

#import

#import "GTMBase64.h"

@implementation XWSSHA1Tool

//普通字符串加密,返回值字符串

+ (NSString *)xwsSHA1:(NSString *)input

{

    constchar*cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:input.length];


    uint8_t digest[CC_SHA1_DIGEST_LENGTH];


    CC_SHA1(data.bytes, (unsignedint)data.length, digest);


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


    for(inti =0; i < CC_SHA1_DIGEST_LENGTH; i++)

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


    NSString *shaStr = [NSString stringWithFormat:@"%@",output];

    NSData *dataBase = [shaStr dataUsingEncoding:NSUTF8StringEncoding];

    NSString *base64String = [dataBase base64EncodedStringWithOptions:0];


    returnbase64String;

}

//带有汉子类型加密,返回值字符串

+ (NSString *)xwsHanziSHA1:(NSString *)input

{

    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];


    uint8_t digest[CC_SHA1_DIGEST_LENGTH];


    CC_SHA1(data.bytes, (unsignedint)data.length, digest);


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


    for(inti=0; i

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

    }


    returnoutput;

}

//普通字符串加密,返回值字NSData

+ (NSData *)getDataSHA1:(NSString *)input

{

    constchar*cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:input.length];


    uint8_t digest[CC_SHA1_DIGEST_LENGTH];


    CC_SHA1(data.bytes, (unsignedint)data.length, digest);


    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];


    returnbase64;

}

//先SHA1后进行Base64(url_safe)

+ (NSString *)getSHA1WithBase64:(NSString *)input

{

    constchar*cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:input.length];


    uint8_t digest[CC_SHA1_DIGEST_LENGTH];


    CC_SHA1(data.bytes, (unsignedint)data.length, digest);


    //SHA1直接转data

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];


    //SHA1进行Base64编码

    //Base64编码中包含有"+,/,="不安全的URL字符串,我们要对这些字符进行转换

    NSString * base64Str = [GTMBase64 encodeBase64Data:base64];

    NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+"withString:@"-"];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/"withString:@"_"];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"="withString:@""];

    NSLog(@"safeBase64编码:%@", safeBase64Str);

    returnsafeBase64Str;

}

你可能感兴趣的:(iOS SHA1 和 Base64 url_safe 加密)