XSUtils.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // XSUtils.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/28.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XSUtils.h"
  9. #import <Security/Security.h>
  10. #import <CommonCrypto/CommonCrypto.h>
  11. #import <SystemConfiguration/SystemConfiguration.h>
  12. NSString* sdk_md5(NSString* value) {
  13. unsigned char buff[CC_MD5_DIGEST_LENGTH] = {};
  14. const char* ptr = value.UTF8String;
  15. CC_MD5(ptr, (CC_LONG)strlen(ptr), buff);
  16. NSMutableString* result = [[NSMutableString alloc] init];
  17. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) {
  18. [result appendFormat:@"%02x", buff[i]];
  19. }
  20. return result;
  21. }
  22. NSString* sdk_signature(NSDictionary* value, NSString* key) {
  23. NSMutableString* str = [NSMutableString new];
  24. NSArray* keys = [value.allKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  25. return [obj1 compare:obj2];
  26. }];
  27. [keys enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  28. [str appendFormat:@"%@%@", obj, value[obj] ?: @""];
  29. }];
  30. [str appendString:key];
  31. // MD5
  32. NSString* uxs = [sdk_md5(str) uppercaseString];
  33. NSString* dkey = [uxs substringFromIndex:uxs.length - 8];
  34. // DES encrypt.
  35. return [sdk_encrypt([uxs dataUsingEncoding:NSUTF8StringEncoding], dkey) base64EncodedStringWithOptions:0];
  36. }
  37. id sdk_encrypt(NSData* value, NSString* key) {
  38. // Create temporary buffer.
  39. NSInteger count = MAX(value.length * 2, 1024);
  40. void* ptr = malloc(count);
  41. size_t lenght = 0;
  42. // Call the encryption algorithm.
  43. CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmDES, ccPKCS7Padding, key.UTF8String, key.length, "\x1\x2\x3\x4\x5\x6\x7\x8", value.bytes, value.length, ptr, count, &lenght);
  44. // Check encryption result.
  45. if (result != kCCSuccess) {
  46. return nil;
  47. }
  48. // Base64 encoding of the encrypted result.
  49. NSData* data = [NSData dataWithBytes:ptr length:lenght];
  50. free(ptr);
  51. return data;
  52. }
  53. id sdk_decrypt(NSData* value, NSString* key) {
  54. // Create temporary buffer.
  55. NSInteger count = MAX(value.length * 2, 1024);
  56. void* ptr = malloc(count);
  57. size_t lenght = 0;
  58. // Call the encryption algorithm.
  59. CCCryptorStatus result = CCCrypt(kCCDecrypt, kCCAlgorithmDES, ccPKCS7Padding, key.UTF8String, key.length, "\x1\x2\x3\x4\x5\x6\x7\x8", value.bytes, value.length, ptr, count, &lenght);
  60. // Check encryption result.
  61. if (result != kCCSuccess) {
  62. return nil;
  63. }
  64. // Base64 encoding of the encrypted result.
  65. NSData* data = [NSData dataWithBytes:ptr length:lenght];
  66. free(ptr);
  67. return data;
  68. }
  69. @interface AutoLayoutBasedI375: NSLayoutConstraint
  70. @end
  71. @interface AutoLayoutBasedI414: NSLayoutConstraint
  72. @end
  73. @implementation AutoLayoutBasedI375
  74. - (CGFloat)constant {
  75. return CGFloatBasedI375(super.constant);
  76. }
  77. @end
  78. @implementation AutoLayoutBasedI414
  79. - (CGFloat)constant {
  80. return CGFloatBasedI414(super.constant);
  81. }
  82. @end
  83. BOOL sdk_verifyPhone(NSString* value) {
  84. NSPredicate* p = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^1[3456789]\\d{9}$", nil];
  85. return [p evaluateWithObject:value];
  86. }