Нет описания

DataMD5.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // DataMD5.m
  3. // WeChatDemo
  4. //
  5. // Created by 尹俊雄 on 15/7/2.
  6. // Copyright (c) 2015年 yjx. All rights reserved.
  7. //
  8. #import "DataMD5.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @interface DataMD5()
  11. @property (nonatomic,strong) NSString *appid;
  12. @property (nonatomic,strong) NSString *mch_id;
  13. @property (nonatomic,strong) NSString *nonce_str;
  14. @property (nonatomic,strong) NSString *partnerkey;
  15. @property (nonatomic,strong) NSString *body;
  16. @property (nonatomic,strong) NSString *out_trade_no;
  17. @property (nonatomic,strong) NSString *total_fee;
  18. @property (nonatomic,strong) NSString *spbill_create_ip;
  19. @property (nonatomic,strong) NSString *notify_url;
  20. @property (nonatomic,strong) NSString *trade_type;
  21. @end
  22. @implementation DataMD5
  23. -(instancetype)initWithAppid:(NSString *)appid_key
  24. mch_id:(NSString *)mch_id_key
  25. nonce_str:(NSString *)noce_str_key
  26. partner_id:(NSString *)partner_id
  27. body:(NSString *)body_key
  28. out_trade_no :(NSString *)out_trade_no_key
  29. total_fee:(NSString *)total_fee_key
  30. spbill_create_ip:(NSString *)spbill_create_ip_key
  31. notify_url:(NSString *)notify_url_key
  32. trade_type:(NSString *)trade_type_key{
  33. self = [super init];
  34. if (self) {
  35. _appid = appid_key;
  36. _mch_id = mch_id_key;
  37. _nonce_str = noce_str_key;
  38. _partnerkey = partner_id;
  39. _body = body_key;
  40. _out_trade_no = out_trade_no_key;
  41. _total_fee = total_fee_key;
  42. _spbill_create_ip = spbill_create_ip_key;
  43. _notify_url = notify_url_key;
  44. _trade_type = trade_type_key;
  45. }
  46. return self;
  47. }
  48. ///获取sign签名
  49. - (NSString *)getSignForMD5{
  50. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  51. [dic setValue:_appid forKey:@"appid"];
  52. [dic setValue:_mch_id forKey:@"mch_id"];
  53. [dic setValue:_nonce_str forKey:@"nonce_str"];
  54. [dic setValue:_body forKey:@"body"];
  55. [dic setValue:_out_trade_no forKey:@"out_trade_no"];
  56. [dic setValue:_total_fee forKey:@"total_fee"];
  57. [dic setValue:_spbill_create_ip forKey:@"spbill_create_ip"];
  58. [dic setValue:_notify_url forKey:@"notify_url"];
  59. [dic setValue:_trade_type forKey:@"trade_type"];
  60. return [self createMd5Sign:dic];
  61. }
  62. //创建签名
  63. -(NSString*) createMd5Sign:(NSMutableDictionary*)dict
  64. {
  65. NSMutableString *contentString =[NSMutableString string];
  66. NSArray *keys = [dict allKeys];
  67. //按字母顺序排序
  68. NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  69. return [obj1 compare:obj2 options:NSNumericSearch];
  70. }];
  71. //拼接字符串
  72. for (NSString *categoryId in sortedArray) {
  73. if ( ![[dict objectForKey:categoryId] isEqualToString:@""]
  74. && ![[dict objectForKey:categoryId] isEqualToString:@"sign"]
  75. && ![[dict objectForKey:categoryId] isEqualToString:@"key"]
  76. )
  77. {
  78. [contentString appendFormat:@"%@=%@&", categoryId, [dict objectForKey:categoryId]];
  79. }
  80. }
  81. //添加商户密钥key字段
  82. [contentString appendFormat:@"key=%@", _partnerkey];
  83. //得到MD5 sign签名
  84. NSString *md5Sign =[self md5:contentString];
  85. return md5Sign;
  86. }
  87. //创建发起支付时的sige签名
  88. -(NSString *)createMD5SingForPay:(NSString *)appid_key partnerid:(NSString *)partnerid_key prepayid:(NSString *)prepayid_key package:(NSString *)package_key noncestr:(NSString *)noncestr_key timestamp:(UInt32)timestamp_key{
  89. NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
  90. [signParams setObject:appid_key forKey:@"appid"];
  91. [signParams setObject:noncestr_key forKey:@"noncestr"];
  92. [signParams setObject:package_key forKey:@"package"];
  93. [signParams setObject:partnerid_key forKey:@"partnerid"];
  94. [signParams setObject:prepayid_key forKey:@"prepayid"];
  95. [signParams setObject:[NSString stringWithFormat:@"%u",timestamp_key] forKey:@"timestamp"];
  96. NSMutableString *contentString =[NSMutableString string];
  97. NSArray *keys = [signParams allKeys];
  98. //按字母顺序排序
  99. NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  100. return [obj1 compare:obj2 options:NSNumericSearch];
  101. }];
  102. //拼接字符串
  103. for (NSString *categoryId in sortedArray) {
  104. if ( ![[signParams objectForKey:categoryId] isEqualToString:@""]
  105. && ![[signParams objectForKey:categoryId] isEqualToString:@"sign"]
  106. && ![[signParams objectForKey:categoryId] isEqualToString:@"key"]
  107. )
  108. {
  109. [contentString appendFormat:@"%@=%@&", categoryId, [signParams objectForKey:categoryId]];
  110. }
  111. }
  112. //添加商户密钥key字段
  113. [contentString appendFormat:@"key=%@", @"yaBe20140901JJqqddSSkkBB20150717"];
  114. // NSString *signString =[self md5:contentString];
  115. NSString *result = [self md5:contentString];
  116. return result;
  117. }
  118. -(NSString *) md5:(NSString *)str
  119. {
  120. const char *cStr = [str UTF8String];
  121. //加密规则,因为逗比微信没有出微信支付demo,这里加密规则是参照安卓demo来得
  122. unsigned char result[16]= "0123456789abcdef";
  123. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  124. //这里的x是小写则产生的md5也是小写,x是大写则md5是大写,这里只能用大写,逗比微信的大小写验证很逗
  125. return [NSString stringWithFormat:
  126. @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  127. result[0], result[1], result[2], result[3],
  128. result[4], result[5], result[6], result[7],
  129. result[8], result[9], result[10], result[11],
  130. result[12], result[13], result[14], result[15]
  131. ];
  132. }
  133. @end