123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // XSUtils.m
- // XenonSDK
- //
- // Created by SAGESSE on 2019/5/28.
- // Copyright © 2019 SAGESSE. All rights reserved.
- //
- #import "XSUtils.h"
- #import <Security/Security.h>
- #import <CommonCrypto/CommonCrypto.h>
- #import <SystemConfiguration/SystemConfiguration.h>
- NSString* sdk_md5(NSString* value) {
- unsigned char buff[CC_MD5_DIGEST_LENGTH] = {};
- const char* ptr = value.UTF8String;
- CC_MD5(ptr, (CC_LONG)strlen(ptr), buff);
- NSMutableString* result = [[NSMutableString alloc] init];
- for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) {
- [result appendFormat:@"%02x", buff[i]];
- }
- return result;
- }
- NSString* sdk_signature(NSDictionary* value, NSString* key) {
-
- NSMutableString* str = [NSMutableString new];
- NSArray* keys = [value.allKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
- return [obj1 compare:obj2];
- }];
- [keys enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [str appendFormat:@"%@%@", obj, value[obj] ?: @""];
- }];
- [str appendString:key];
-
- // MD5
- NSString* uxs = [sdk_md5(str) uppercaseString];
- NSString* dkey = [uxs substringFromIndex:uxs.length - 8];
-
- // DES encrypt.
- return [sdk_encrypt([uxs dataUsingEncoding:NSUTF8StringEncoding], dkey) base64EncodedStringWithOptions:0];
- }
- id sdk_encrypt(NSData* value, NSString* key) {
-
- // Create temporary buffer.
- NSInteger count = MAX(value.length * 2, 1024);
- void* ptr = malloc(count);
- size_t lenght = 0;
-
- // Call the encryption algorithm.
- 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);
-
- // Check encryption result.
- if (result != kCCSuccess) {
- return nil;
- }
-
- // Base64 encoding of the encrypted result.
- NSData* data = [NSData dataWithBytes:ptr length:lenght];
- free(ptr);
- return data;
- }
- id sdk_decrypt(NSData* value, NSString* key) {
-
- // Create temporary buffer.
- NSInteger count = MAX(value.length * 2, 1024);
-
- void* ptr = malloc(count);
- size_t lenght = 0;
-
- // Call the encryption algorithm.
- 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);
-
- // Check encryption result.
- if (result != kCCSuccess) {
- return nil;
- }
-
- // Base64 encoding of the encrypted result.
- NSData* data = [NSData dataWithBytes:ptr length:lenght];
- free(ptr);
- return data;
- }
- @interface AutoLayoutBasedI375: NSLayoutConstraint
- @end
- @interface AutoLayoutBasedI414: NSLayoutConstraint
- @end
- @implementation AutoLayoutBasedI375
- - (CGFloat)constant {
- return CGFloatBasedI375(super.constant);
- }
- @end
- @implementation AutoLayoutBasedI414
- - (CGFloat)constant {
- return CGFloatBasedI414(super.constant);
- }
- @end
- BOOL sdk_verifyPhone(NSString* value) {
- NSPredicate* p = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^1[3456789]\\d{9}$", nil];
- return [p evaluateWithObject:value];
- }
|