iOS 之字段排序并且SHA1加密

#import <CommonCrypto/CommonDigest.h>


//SHA1加密方式


+(NSString*)sha1WithStr:(NSString *)str

{

    constchar *cstr = [strcStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSDatadataWithBytes:cstr length: strlen(cstr)];

    

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    

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

    

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

    

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

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

    

    return output;

}


+(NSMutableDictionary *)publicDict{

    NSDictionary *infoDictionary = [[NSBundlemainBundle] infoDictionary];

    CFShow(CFBridgingRetain(infoDictionary));//打印出infoDictionary的详细信息

    

    //bundelID

    NSString *bundelID= [[NSBundlemainBundle] bundleIdentifier];

//    NSLog(@"bundelID---%@",bundelID);

    

    // app名称displayName

    NSString *app_Name = [infoDictionaryobjectForKey:@"CFBundleDisplayName"];

//    NSLog(@"app名称displayname---%@",app_Name);

    

    // app名称name,displayname存在时,name显示为overridedisplayname不存在时,name正常显示

    NSString *name = [infoDictionaryobjectForKey:@"CFBundleName"];

//    NSLog(@"app名称name---%@",name);

    

    //手机序列号(唯一标识)

    NSString* identifierNumber =[[[UIDevicecurrentDevice] identifierForVendor]UUIDString];

//    NSLog(@"手机序列号: %@",identifierNumber);

    

    //设备名称

    NSString* deviceName = [[UIDevicecurrentDevice] systemName];

//    NSLog(@"设备名称: %@",deviceName );

    //手机系统版本

    NSString* phoneVersion = [[UIDevicecurrentDevice] systemVersion];

//    NSLog(@"手机系统版本: %@", phoneVersion);

    //手机型号

    NSString* phoneModel = [[UIDevicecurrentDevice] model];

//    NSLog(@"手机型号: %@",phoneModel );

    

    NSMutableString *model=[NSMutableStringstringWithString:[deviceNamestringByAppendingString:[NSStringstringWithFormat:@" %@",phoneVersion]]];

    [model appendString:[NSStringstringWithFormat:@"/%@",phoneModel]];

//    NSLog(@"model---%@",model);

    

    NSString *agentStr=[[WofuCommanManagersharedManager].agentstringByReplacingOccurrencesOfString:@"-"withString:@""];

//    NSLog(@"agentStr---%@",agentStr);

    int  randNum=19+arc4random()%10;

    NSString *noiseStr=[agentStrsubstringToIndex:randNum];

    NSMutableDictionary *params=[NSMutableDictionarydictionaryWithDictionary:@{

                                                        @"version":@"1.1",

                                                        @"agent":[WofuCommanManagersharedManager].agent,

                                                        @"device":identifierNumber,

                                                        @"model":model,

                                                        @"package":bundelID,

                                                        @"appname":app_Name?app_Name:name,

                                                        @"noise":noiseStr

                                                                                

                                                                                }];

    

    return params;

}



+(NSMutableDictionary *)publicSignParamsWithDict:(NSMutableDictionary *)params{


    NSArray *keyArr=[paramsallKeys];

    

    keyArr = [keyArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){

        NSComparisonResult result = [obj1compare:obj2];

        return result;

    }];

//    NSLog(@"key数组002%@",keyArr);

    NSMutableString *signStr=[[NSMutableStringalloc]initWithCapacity:10];

    for (int i=0; i<keyArr.count; i++) {

        NSString *key=keyArr[i];

        

        

        [signStr appendString:[NSStringstringWithFormat:@"%@=",key]];

        

        

        [signStr

         appendString:params[key]];

        

        [signStr appendString:@"&"];

        

        

    }

    

    [signStr appendString:[NSStringstringWithFormat:@"key=%@",[WofuCommanManagersharedManager].seckey]];

    

//      NSLog(@"拼接参数---%@",signStr);

    //SHA1计算上面拼接的字符串

    NSString *sha1str=[selfsha1WithStr:signStr];

    


    NSLog(@"sha1--%@",sha1str);

    [params setObject:sha1strforKey:@"sign"];

    

    return params;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值