欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS SHA1 和 Base64 url_safe加密解析

程序员文章站 2022-05-16 20:33:10
ios sha1 和 base64 url_safe 。 网上查找的资料sha1加密后大部分获取的是字符串,如果还要进行下一步操作时,一定要慎用字符串,我最近在做sha1 和 base64 url_...

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

{

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, (unsigned int)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]];

 

nsstring *shastr = [nsstring stringwithformat:@"%@",output];

nsdata *database = [shastr datausingencoding:nsutf8stringencoding];

nsstring *base64string = [database base64encodedstringwithoptions:0];

 

return base64string;

}

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

+ (nsstring *)xwshanzisha1:(nsstring *)input

{

nsdata *data = [input datausingencoding:nsutf8stringencoding];

 

uint8_t digest[cc_sha1_digest_length];

 

cc_sha1(data.bytes, (unsigned int)data.length, digest);

 

nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2];

 

for(int i=0; i

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

}

 

return output;

}

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

+ (nsdata *)getdatasha1:(nsstring *)input

{

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, (unsigned int)data.length, digest);

 

nsdata * base64 = [[nsdata alloc]initwithbytes:digest length:cc_sha1_digest_length];

 

return base64;

}

//先sha1后进行base64(url_safe)

+ (nsstring *)getsha1withbase64:(nsstring *)input

{

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, (unsigned int)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);

return safebase64str;

}

@end