From: liangzhenying <867174124@qq.com> Date: Tue, 9 Jul 2024 02:36:29 +0000 (+0800) Subject: 鉴权接口更改 X-Git-Tag: deepseek~20 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=85e94a5122ab67e13a276cebd982594ec08f2f61;p=mirrors%2FMixSdk-iOS.git 鉴权接口更改 --- diff --git a/MixSdk/Example/Example/ViewController.swift b/MixSdk/Example/Example/ViewController.swift index f0dd220..93eaba3 100644 --- a/MixSdk/Example/Example/ViewController.swift +++ b/MixSdk/Example/Example/ViewController.swift @@ -65,11 +65,10 @@ extension ViewController:UITableViewDelegate,UITableViewDataSource { extension ViewController{ @objc func registerAction() { view.makeToastActivity(.center) - let sn = "MSCL1BKZ12105000042363" - let deviceId = "C0310000A57B" - let productKey = "ccvUoJOLbXn" - let secretKey = "nNRjkRpKTxvprGaW" - MixSdk.shared.auth.register(productKey: productKey, secretKey: secretKey, deviceId: deviceId,sn:sn){[weak self] deviceSecret,error in + let deviceId = "281106AFB0F7" + let productKey = "pafOCKLiUbeVHbdZlddH" + let secretKey = "HHosiYUNdG42t1cTgKOzIUPOvAVMI2QT" + MixSdk.shared.auth.register(productKey: productKey, secretKey: secretKey, deviceId: deviceId){[weak self] model,error in self?.view.hideToastActivity() if let err = error{ print("\(err)") @@ -77,17 +76,18 @@ extension ViewController{ } else{ self?.view.makeToast("register success!") - print(deviceSecret ?? "") + print(model!.deviceId) + print(model!.password) + print(model!.nonce) } } } @objc func loginAction() { view.makeToastActivity(.center) - let deviceId = "C0310000A57B" - let productKey = "ccvUoJOLbXn" - let secretKey = "nNRjkRpKTxvprGaW" - let deviceSecret = "twLGAEo3RqlfCY3QLOjppdXkMb5I4dGC" - MixSdk.shared.auth.login(productKey: productKey, secretKey: secretKey, deviceId: deviceId, deviceSecrect: deviceSecret){[weak self] isSuccess,error in + let deviceId = "281106AFB0F7" + let password = "RDSr+qCM3zLqTLVh3L/74B4Rhh5CsBU7D3/0KZMCknM="//注册获得 + let nonce = "WFY51NbA4lUff3Jd"//注册获得 + MixSdk.shared.auth.login(deviceId: deviceId, password: password,nonce:nonce){[weak self] isSuccess,error in self?.view.hideToastActivity() if let err = error{ self?.view.makeToast(err.msg) diff --git a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 897e53b..370fbd7 100644 Binary files a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 47af5c5..a1a8a88 100644 --- a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -68,21 +68,5 @@ - - - - diff --git a/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj b/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj index f7681df..14d1da6 100644 --- a/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj +++ b/MixSdk/MixSdk/MixSdk.xcodeproj/project.pbxproj @@ -17,7 +17,7 @@ 1811883F2C1A8B5200C23FAB /* CRC16.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188372C1A8B5200C23FAB /* CRC16.swift */; }; 181188402C1A8B5200C23FAB /* LRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188382C1A8B5200C23FAB /* LRC.swift */; }; 181188452C1AC35F00C23FAB /* MSError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188442C1AC35F00C23FAB /* MSError.swift */; }; - 181188472C1ACA6100C23FAB /* String+MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188462C1ACA6100C23FAB /* String+MD5.swift */; }; + 181188472C1ACA6100C23FAB /* String+Crypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188462C1ACA6100C23FAB /* String+Crypto.swift */; }; 1811884E2C1AEF0F00C23FAB /* MSTranslate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1811884D2C1AEF0F00C23FAB /* MSTranslate.swift */; }; 181188572C1BE24200C23FAB /* MSStt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181188562C1BE24200C23FAB /* MSStt.swift */; }; 18132AED2C1C1EA8002F6B52 /* MSTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18132AEC2C1C1EA8002F6B52 /* MSTool.swift */; }; @@ -33,6 +33,9 @@ 18204BFB2C19488900F94E69 /* Upload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18204BF52C19488800F94E69 /* Upload.swift */; }; 18204BFC2C19488900F94E69 /* HTTPSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18204BF62C19488800F94E69 /* HTTPSecurity.swift */; }; 18204BFD2C19488900F94E69 /* Operation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18204BF72C19488800F94E69 /* Operation.swift */; }; + 18314A862C3CD24E0028CE50 /* MSUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 18314A842C3CD24E0028CE50 /* MSUtils.m */; }; + 18314A872C3CD24E0028CE50 /* MSUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 18314A852C3CD24E0028CE50 /* MSUtils.h */; }; + 18314A892C3CD55E0028CE50 /* MSAuthModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18314A882C3CD55E0028CE50 /* MSAuthModel.swift */; }; 184FFC542C21B091006F664D /* MSAigcModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 184FFC532C21B091006F664D /* MSAigcModel.swift */; }; 18B74A1C2C1FD44A00FABBA9 /* MSTts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B74A1B2C1FD44A00FABBA9 /* MSTts.swift */; }; 18B74A1E2C1FDDB500FABBA9 /* MSChat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B74A1D2C1FDDB400FABBA9 /* MSChat.swift */; }; @@ -191,7 +194,7 @@ 181188372C1A8B5200C23FAB /* CRC16.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CRC16.swift; sourceTree = ""; }; 181188382C1A8B5200C23FAB /* LRC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LRC.swift; sourceTree = ""; }; 181188442C1AC35F00C23FAB /* MSError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSError.swift; sourceTree = ""; }; - 181188462C1ACA6100C23FAB /* String+MD5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+MD5.swift"; sourceTree = ""; }; + 181188462C1ACA6100C23FAB /* String+Crypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Crypto.swift"; sourceTree = ""; }; 1811884D2C1AEF0F00C23FAB /* MSTranslate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSTranslate.swift; sourceTree = ""; }; 181188562C1BE24200C23FAB /* MSStt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSStt.swift; sourceTree = ""; }; 18132AEC2C1C1EA8002F6B52 /* MSTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSTool.swift; sourceTree = ""; }; @@ -208,6 +211,9 @@ 18204BF52C19488800F94E69 /* Upload.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Upload.swift; sourceTree = ""; }; 18204BF62C19488800F94E69 /* HTTPSecurity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPSecurity.swift; sourceTree = ""; }; 18204BF72C19488800F94E69 /* Operation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operation.swift; sourceTree = ""; }; + 18314A842C3CD24E0028CE50 /* MSUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSUtils.m; sourceTree = ""; }; + 18314A852C3CD24E0028CE50 /* MSUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSUtils.h; sourceTree = ""; }; + 18314A882C3CD55E0028CE50 /* MSAuthModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSAuthModel.swift; sourceTree = ""; }; 184FFC532C21B091006F664D /* MSAigcModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSAigcModel.swift; sourceTree = ""; }; 18B74A1B2C1FD44A00FABBA9 /* MSTts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSTts.swift; sourceTree = ""; }; 18B74A1D2C1FDDB400FABBA9 /* MSChat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSChat.swift; sourceTree = ""; }; @@ -386,7 +392,9 @@ 181188362C1A8B5200C23FAB /* UTF8.swift */, 181188372C1A8B5200C23FAB /* CRC16.swift */, 181188382C1A8B5200C23FAB /* LRC.swift */, - 181188462C1ACA6100C23FAB /* String+MD5.swift */, + 181188462C1ACA6100C23FAB /* String+Crypto.swift */, + 18314A852C3CD24E0028CE50 /* MSUtils.h */, + 18314A842C3CD24E0028CE50 /* MSUtils.m */, 18132AEC2C1C1EA8002F6B52 /* MSTool.swift */, 18C42B4A2C23DA29000D8860 /* SbcToPcm.swift */, ); @@ -431,6 +439,7 @@ 18204BEF2C19457600F94E69 /* MSHttpManager.swift */, 1811882E2C1A89E600C23FAB /* MixSdk.swift */, 18204BC52C193E4B00F94E69 /* MSAuth.swift */, + 18314A882C3CD55E0028CE50 /* MSAuthModel.swift */, 1811884D2C1AEF0F00C23FAB /* MSTranslate.swift */, 18C42B3A2C22F284000D8860 /* MsTranslateModel.swift */, 18132AEE2C1C25CC002F6B52 /* MSAigc.swift */, @@ -792,6 +801,7 @@ 18C42BF52C242202000D8860 /* murmur3.h in Headers */, 18C42C582C242202000D8860 /* bsf.h in Headers */, 18C42C452C242202000D8860 /* avformat.h in Headers */, + 18314A872C3CD24E0028CE50 /* MSUtils.h in Headers */, 18C42C2C2C242202000D8860 /* channel_layout.h in Headers */, 18C42C1A2C242202000D8860 /* pixelutils.h in Headers */, 18C42C5C2C242202000D8860 /* ac3_parser.h in Headers */, @@ -890,7 +900,7 @@ 18C42C7B2C24270D000D8860 /* MSAudioConvert.swift in Sources */, 18204BC62C193E4B00F94E69 /* MSAuth.swift in Sources */, 18132AED2C1C1EA8002F6B52 /* MSTool.swift in Sources */, - 181188472C1ACA6100C23FAB /* String+MD5.swift in Sources */, + 181188472C1ACA6100C23FAB /* String+Crypto.swift in Sources */, 18132AF22C1C4938002F6B52 /* SwiftWebSocket.swift in Sources */, 1811883F2C1A8B5200C23FAB /* CRC16.swift in Sources */, 181188452C1AC35F00C23FAB /* MSError.swift in Sources */, @@ -900,10 +910,12 @@ 18204BFD2C19488900F94E69 /* Operation.swift in Sources */, 1811883C2C1A8B5200C23FAB /* JSON.swift in Sources */, 181188402C1A8B5200C23FAB /* LRC.swift in Sources */, + 18314A892C3CD55E0028CE50 /* MSAuthModel.swift in Sources */, 18FBC2B92C38DA1D002240AF /* MSChatGptModel.swift in Sources */, 18204BFC2C19488900F94E69 /* HTTPSecurity.swift in Sources */, 18204BFB2C19488900F94E69 /* Upload.swift in Sources */, 18204BFA2C19488900F94E69 /* Request.swift in Sources */, + 18314A862C3CD24E0028CE50 /* MSUtils.m in Sources */, 1811882F2C1A89E600C23FAB /* MixSdk.swift in Sources */, 18C42B3B2C22F284000D8860 /* MsTranslateModel.swift in Sources */, 18C42B3F2C22F2E1000D8860 /* MSChatModel.swift in Sources */, diff --git a/MixSdk/MixSdk/MixSdk/MSAuth.swift b/MixSdk/MixSdk/MixSdk/MSAuth.swift index c1658d8..d8e78f4 100644 --- a/MixSdk/MixSdk/MixSdk/MSAuth.swift +++ b/MixSdk/MixSdk/MixSdk/MSAuth.swift @@ -9,7 +9,7 @@ import Foundation import CommonCrypto /// 注册回调 -public typealias MSRegisterCallBack = (_ deviceSecret: String?,_ error:MSError?) -> Void +public typealias MSRegisterCallBack = (_ data: MSRegisterModel?,_ error:MSError?) -> Void /// 登录回调 public typealias MSLoginCallBack = (_ isSuccess: Bool,_ error:MSError?) -> Void @@ -20,7 +20,7 @@ public class MSAuth { public private(set) var authorized = true func getUrl(_ api:String)->String{ - return "https://app.api.thisiot.com/da/auth/" + api + return "http://180.184.206.142/bz-api/" + api } /// 注册 @@ -30,21 +30,21 @@ public class MSAuth { /// - deviceId: 设备ID /// - sn: SN号 /// - completeHandler: 结果回调 - public func register(productKey:String,secretKey:String,deviceId:String,sn:String,completeHandler: @escaping MSRegisterCallBack){ + public func register(productKey:String,secretKey:String,deviceId:String,completeHandler: @escaping MSRegisterCallBack){ let timeStamp = "\(Int(Date().timeIntervalSince1970)*1000)" - let value = deviceId + sn + timeStamp + secretKey - let signValue = value.md5 + let value = deviceId + productKey + timeStamp + let signValue = MSUtils.string(toHmacSha1: secretKey, text: value) let params = [ - "bid":productKey, + "pid":productKey, "deviceId":deviceId, - "signMethod":"MD5", + "signMethod":"HMACSHA1", "sign":signValue, "timeStamp":timeStamp, - "sn":sn ] - let url = getUrl("active") - Logger.log(params) + let url = getUrl("dev/auth/active") + Logger.log(value,secretKey,signValue) + Logger.log(url,params) HTTP.PUT(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { @@ -54,8 +54,12 @@ public class MSAuth { return } guard let dict = dict else {return completeHandler(nil,error)} - if let result = dict["success"] as? Bool,result == true,let data = dict["data"] as? [String:Any], let deviceSecret = data["deviceSecret"] as? String{ - completeHandler(deviceSecret,nil) + if let result = dict["success"] as? Bool,result == true,let dataDict = dict["data"] as? [String:Any]{ + let model = MSRegisterModel() + model.deviceId = dataDict["deviceId"] as? String ?? "" + model.password = dataDict["password"] as? String ?? "" + model.nonce = dataDict["nonce"] as? String ?? "" + completeHandler(model,nil) } else{ completeHandler(nil,MSError.reponseError(dict["code"] as? Int ?? 0,dict["msg"] as? String ?? "")) @@ -71,20 +75,16 @@ public class MSAuth { /// - deviceId: 设备ID /// - deviceSecrect: 注册返回的密码 /// - completeHandler: 结果回调 - public func login(productKey:String,secretKey:String,deviceId:String,deviceSecrect:String,completeHandler: @escaping MSLoginCallBack){ - let timeStamp = "\(Int(Date().timeIntervalSince1970*1000))" - let value = deviceId + deviceSecrect + timeStamp + secretKey - let signValue = value.md5 + public func login(deviceId:String,password:String,nonce:String,completeHandler: @escaping MSLoginCallBack){ let params = [ - "bid":productKey, "deviceId":deviceId, - "deviceSecret":deviceSecrect, - "timestamp":timeStamp, - "signMethod":"MD5", - "sign":signValue + "password":password, + "nonce":nonce, ] - let url = getUrl("login") - Logger.log(params) + + + let url = getUrl("dev/auth/sign_in") + Logger.log(url,params) HTTP.POST(url,parameters: params,requestSerializer: JSONParameterSerializer()){ response in DispatchQueue.main.sync { diff --git a/MixSdk/MixSdk/MixSdk/MSAuthModel.swift b/MixSdk/MixSdk/MixSdk/MSAuthModel.swift new file mode 100644 index 0000000..288a94d --- /dev/null +++ b/MixSdk/MixSdk/MixSdk/MSAuthModel.swift @@ -0,0 +1,29 @@ +// +// MSAuthModel.swift +// MixSdk +// +// Created by incar on 2024/7/9. +// + +import Foundation +///// 注册返回模型 +//public class MSRegisterModel:Codable{ +// /// 是否成功 +// public var success:Bool = false +// /// 提示语 +// public var msg:String = "请求失败" +// /// 状态码 +// public var code:Int = 400 +// /// 内容 +// public var data:MSRegisterData? +// +//} +/// 注册返回模型 +public class MSRegisterModel{ + /// 设备ID + public var deviceId:String = "" + /// 设备密码 + public var password:String = "" + /// 16个字符的随机字符串 + public var nonce:String = "" +} diff --git a/MixSdk/MixSdk/MixSdk/MixSdk-Bridging-Header.h b/MixSdk/MixSdk/MixSdk/MixSdk-Bridging-Header.h index b9d47ef..f14df85 100644 --- a/MixSdk/MixSdk/MixSdk/MixSdk-Bridging-Header.h +++ b/MixSdk/MixSdk/MixSdk/MixSdk-Bridging-Header.h @@ -6,3 +6,4 @@ // #import "AudioDecode/AiAudioDecoder.h" +#import "Tools/MSUtils.h" diff --git a/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift b/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift index 055624c..c09f993 100644 --- a/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift +++ b/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift @@ -48,4 +48,6 @@ class MSTool{ } return nil } + + } diff --git a/MixSdk/MixSdk/MixSdk/Tools/MSUtils.h b/MixSdk/MixSdk/MixSdk/Tools/MSUtils.h new file mode 100644 index 0000000..aee3037 --- /dev/null +++ b/MixSdk/MixSdk/MixSdk/Tools/MSUtils.h @@ -0,0 +1,38 @@ +// +// AiMouseTools.h +// AiMouse +// +// Created by incar on 2024/2/28. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MSUtils : NSObject ++ (NSArray *)stringToArray:(NSString *)jsonString; ++ (NSString *)arrayToString:(NSArray *)arr; + ++ (NSDictionary *)stringToDictionary:(NSString *)jsonString; ++ (NSString *)dictionaryToString:(NSDictionary *)dic; + ++ (NSDictionary *)dataToDictionary:(NSData *)jsonData; ++ (NSData *)dictionaryToData:(NSDictionary *)dicJson; + + ++ (NSString *)dataToHexString:(NSData *)data; + ++ (NSString *)dataToHexString_placeholder:(NSData *)data; + ++ (NSData *)hexStringToData:(NSString *)hexStr ; + ++ (NSString *)dataToUtf8String:(NSData *)data; + ++ (NSString *)stringTomd5:(NSString *)str; + ++ (NSString *)stringToHmacSha1:(NSString*)key text:(NSString*)text; + ++ (NSString *)MD5ForUpper32:(NSString *)str; +@end + +NS_ASSUME_NONNULL_END diff --git a/MixSdk/MixSdk/MixSdk/Tools/MSUtils.m b/MixSdk/MixSdk/MixSdk/Tools/MSUtils.m new file mode 100644 index 0000000..7a42982 --- /dev/null +++ b/MixSdk/MixSdk/MixSdk/Tools/MSUtils.m @@ -0,0 +1,167 @@ +// +// AiMouseTools.m +// AiMouse +// +// Created by incar on 2024/2/28. +// + +#import "MSUtils.h" +#import +#include +#include + +@implementation MSUtils ++ (NSArray *)stringToArray:(NSString *)jsonString{ + NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error; + NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; + return arr; +} ++ (NSString *)arrayToString:(NSArray *)arr{ + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error]; + NSString * string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + return string; +} ++ (NSDictionary *)stringToDictionary:(NSString *)jsonString{ + NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; + if (jsonData == nil) { + return nil; + } + NSError *error; + NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; + return dict; +} ++ (NSString *)dictionaryToString:(NSDictionary *)dic{ + if ([dic isKindOfClass:[NSDictionary class]]) { + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error]; + NSString * string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + return string; + } + else{ + return @""; + } +} + ++ (NSDictionary *)dataToDictionary:(NSData *)jsonData{ + if (![jsonData isKindOfClass:[NSData class]] || jsonData.length < 1) { + return nil; + } + id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; + if (![jsonObj isKindOfClass:[NSDictionary class]]) { + return nil; + } + return [NSDictionary dictionaryWithDictionary:(NSDictionary *)jsonObj]; +} + + ++ (NSData *)dictionaryToData:(NSDictionary *)dicJson{ + if (![dicJson isKindOfClass:[NSDictionary class]]) { + return nil; + } + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil]; + if (![jsonData isKindOfClass:[NSData class]]) { + return nil; + } + return jsonData; +} + ++ (NSString *)dataToHexString:(NSData *)data { + NSUInteger length = data.length; + NSMutableString *result = [NSMutableString stringWithCapacity:length * 2]; + const unsigned char *byte = data.bytes; + for (int i = 0; i < length; i++, byte++) { + [result appendFormat:@"%02X", *byte]; + } + return result; +} + ++ (NSString *)dataToHexString_placeholder:(NSData *)data { + NSUInteger length = data.length; + NSString *placeholder = @" "; + NSMutableString *result = [NSMutableString stringWithCapacity:length * 2]; + const unsigned char *byte = data.bytes; + for (int i = 0; i < length; i++, byte++) { + [result appendFormat:@"%02X%@", *byte, placeholder]; + } + return result; +} + ++ (NSData *)hexStringToData:(NSString *)hexStr { + hexStr = [hexStr stringByReplacingOccurrencesOfString:@" " withString:@""]; + hexStr = [hexStr lowercaseString]; + NSUInteger len = hexStr.length; + if (!len) return nil; + unichar *buf = malloc(sizeof(unichar) * len); + if (!buf) return nil; + [hexStr getCharacters:buf range:NSMakeRange(0, len)]; + + NSMutableData *result = [NSMutableData data]; + unsigned char bytes; + char str[3] = { '\0', '\0', '\0' }; + int i; + for (i = 0; i < len / 2; i++) { + str[0] = buf[i * 2]; + str[1] = buf[i * 2 + 1]; + bytes = strtol(str, NULL, 16); + [result appendBytes:&bytes length:1]; + } + free(buf); + return result; +} + ++ (NSString *)dataToUtf8String:(NSData *)data { + if (data.length > 0) { + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + } + return @""; +} + + ++ (NSString *)stringTomd5:(NSString *)str { + if (str.length == 0) { + return nil; + } + // 初始化C字符数组 + const char* original_str = (const char *)[[str dataUsingEncoding:NSUTF8StringEncoding] bytes]; + // 盛放数字校验的字符数组(长度为16bytes) + unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16 + CC_MD5(original_str, (uint)strlen(original_str), digist); + // NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10]; + NSMutableString* outPutStr = [NSMutableString new]; + for(int i =0; i