UnifyPayTool.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // UnifyPayTool.m
  3. // testDEMO
  4. //
  5. // Created by SunXP on 17/5/8.
  6. // Copyright © 2017年 L. All rights reserved.
  7. //
  8. #import "UnifyPayTool.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. #import "MBProgressHUD.h"
  11. @implementation UnifyPayTool
  12. + (NSString *)getCurrentDate {
  13. NSDate *currentDate = [NSDate date];
  14. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  15. [formatter setDateStyle:NSDateFormatterMediumStyle];
  16. [formatter setTimeStyle:NSDateFormatterShortStyle];
  17. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  18. NSString *currentDateStr = [formatter stringFromDate:currentDate];
  19. return currentDateStr;
  20. }
  21. + (NSString *)getOrderIDWithMsgSrcId:(NSString *)msgSrcId {
  22. // 生成28位随机数
  23. static int kNumber = 28;
  24. NSString *sourceStr = @"0123456789";
  25. NSMutableString *randomStr = [[NSMutableString alloc] init];
  26. #pragma clang diagnostic push
  27. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  28. srand(time(0)); // 此行代码有警告:
  29. #pragma clang diagnostic pop
  30. for (int i = 0; i < kNumber; i++) {
  31. NSInteger index = rand() % [sourceStr length];
  32. NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
  33. [randomStr appendString:oneStr];
  34. }
  35. // 拼接系统编号作为订单号的前四位
  36. NSString *resultStr = [msgSrcId stringByAppendingString:randomStr];
  37. return resultStr;
  38. }
  39. + (NSString *)getGoodsDescription {
  40. NSDictionary *descriptionDic = @{@"goodsId":@"666666",@"goodsName":@"unify测试商品",@"quantity":@"1",@"price":@"1",@"goodsCategory":@"测试商品分类",@"body":@"测试商品说明"};
  41. NSData *descriptionData = [NSJSONSerialization dataWithJSONObject:descriptionDic options:NSJSONWritingPrettyPrinted error:nil];
  42. NSString *tempStr = [[NSString alloc] initWithData:descriptionData encoding:NSUTF8StringEncoding];
  43. return [[tempStr stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  44. }
  45. + (NSString *)getSign:(NSDictionary *)dataDic MD5Key:(NSString *)MD5Key {
  46. NSMutableString *tempStr = [NSMutableString string];
  47. NSArray *originKeys = [dataDic allKeys];
  48. // 1、ASCII排序
  49. NSArray *sortedKeys = [originKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  50. return [obj1 compare:obj2 options:NSNumericSearch];
  51. }];
  52. // 2、拼接字符串
  53. for (NSString *item in sortedKeys) {
  54. [tempStr appendFormat:@"%@=%@&", item, dataDic[item]];
  55. }
  56. // 3、截掉最后的"&"
  57. [tempStr deleteCharactersInRange:NSMakeRange(tempStr.length - 1, 1)];
  58. // 4、最后拼接MD5秘钥
  59. [tempStr appendString:MD5Key];
  60. // 5、MD5加密
  61. return [[self class] md5:tempStr];
  62. }
  63. + (NSString *)md5:(NSString *)str {
  64. const char *cStr = [str UTF8String];
  65. unsigned char result[16]= "0123456789abcdef";
  66. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  67. return [NSString stringWithFormat:
  68. @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  69. result[0], result[1], result[2], result[3],
  70. result[4], result[5], result[6], result[7],
  71. result[8], result[9], result[10], result[11],
  72. result[12], result[13], result[14], result[15]
  73. ];
  74. }
  75. + (NSString *)getSHA256Sign:(NSDictionary *)dataDic MD5Key:(NSString *)MD5Key{
  76. NSMutableString *tempStr = [NSMutableString string];
  77. NSArray *originKeys = [dataDic allKeys];
  78. // 1、ASCII排序
  79. NSArray *sortedKeys = [originKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  80. return [obj1 compare:obj2 options:NSNumericSearch];
  81. }];
  82. // 2、拼接字符串
  83. for (NSString *item in sortedKeys) {
  84. [tempStr appendFormat:@"%@=%@&", item, dataDic[item]];
  85. }
  86. // 3、截掉最后的"&"
  87. [tempStr deleteCharactersInRange:NSMakeRange(tempStr.length - 1, 1)];
  88. // 4、最后拼接MD5秘钥
  89. [tempStr appendString:MD5Key];
  90. return [[self class] SHA256WithString:tempStr];
  91. }
  92. + (NSString *)SHA256WithString:(NSString *)inputString {
  93. const char* str = [inputString UTF8String];
  94. unsigned char result[CC_SHA256_DIGEST_LENGTH];
  95. CC_SHA256(str, (CC_LONG)strlen(str), result);
  96. NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
  97. for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
  98. {
  99. [ret appendFormat:@"%02x",result[i]];
  100. }
  101. ret = (NSMutableString *)[ret uppercaseString];
  102. return ret;
  103. }
  104. + (void)showHUD:(UIView *)targetView animated:(BOOL)animated {
  105. if (targetView) {
  106. [MBProgressHUD showHUDAddedTo:targetView animated:YES];
  107. }
  108. }
  109. + (void)hideHUD:(UIView *)targetView animated:(BOOL)animated {
  110. [MBProgressHUD hideHUDForView:targetView animated:YES];
  111. }
  112. + (NSString *)getUUID {
  113. return [UIDevice currentDevice].identifierForVendor.UUIDString;
  114. }
  115. @end