iOS开发-微信签到机制Sign签名制作

微信功能的模块,简化一下完成了微信后台签到机制。剩下的就是网络请求了,这代码简单易懂,已经用最粗俗的码山来完成的了~~哈哈

原始签名Sign

产生随机数

- (NSString *)acrRandow
{
    NSString * randow = [[NSStringalloc]init];
    int xx =0;
    for (int x =0 ; x <16; x++) {
        xx = arc4random() %10;
        NSString * aa = [NSStringstringWithFormat:@"%d",xx];
        randow = [NSStringstringWithFormat:@"%@%@",randow,aa];
    }
    return randow;
}

②Sign生成过程

- (NSString *)Sign
{
    //对比字段参数的大小然后排序
    NSArray * array = [[NSArrayalloc]initWithObjects:@"参数1",@"参数2",@"参数3",@"参数4",nil];
    NSStringCompareOptions comparisonOptions =NSCaseInsensitiveSearch|NSNumericSearch|
    NSWidthInsensitiveSearch|NSForcedOrderingSearch;
    //根据ASCII排序
    NSComparator sort = ^(NSString *obj1,NSString *obj2){
        NSRange range =NSMakeRange(0,obj1.length);
        return [obj1compare:obj2options:comparisonOptionsrange:range];
    };
    NSArray *resultArray2 = [arraysortedArrayUsingComparator:sort];
    NSLog(@"字符串数组排序结果%@",resultArray2);
    //原始数据
    NSMutableDictionary * dic = [[NSMutableDictionaryalloc]init];
    //idvf
    NSString *idfv = [[[UIDevicecurrentDevice]identifierForVendor]UUIDString];
    [dic setObject:[NSStringstringWithFormat:@"%@",idfv]forKey:@"DeviceId"];
    //授权号
    [dic setObject:@"123"forKey:@"参数1"];
    //POS终端版本号
    [dic setObject:@"1.0.0"forKey:@"AppVersion"];
    NSString * aa = [[NSStringalloc]init];
    //随机字符串
    aa = [selfacrRandow];
    [dic setObject:aaforKey:@"Nonce"];
    //拼接串   key1=value1
    NSMutableArray * arraya = [[NSMutableArrayalloc]init];
    NSString * Sign = [[NSStringalloc]init];
    for (int i =0; i < resultArray2.count; i++) {
        NSString * Signa = [NSStringstringWithFormat:@"%@=%@",[resultArray2objectAtIndex:i],[dicobjectForKey:[resultArray2objectAtIndex:i]]];
        [arraya addObject:Signa];
    }
    // 原始 格式 sign = key1=value1&key2=value2&....
    for (int i =0; i < arraya.count; i++) {
        Sign = [NSStringstringWithFormat:@"%@&%@",Sign,[arrayaobjectAtIndex:i]];
    }
    Sign =  [Sign substringFromIndex:1];
    NSLog(@"%@",Sign);
    return Sign;
    }
    //把之前得到的Sign用  md5加密
    - (NSString *)md5:(NSString *)str
    {
    constchar *concat_str = [strUTF8String];
    unsignedchar result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(concat_str, (unsignedint)strlen(concat_str), result);
    NSMutableString *hash = [NSMutableStringstring];
    for (int i =0; i <16; i++){
        [hash appendFormat:@"%02X", result[i]];
    }
    return [hashuppercaseString];
}

通过以上步骤就获得了一个加密之后的Sign签名



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值