From: liangzhenying <867174124@qq.com> Date: Wed, 4 Dec 2024 03:27:43 +0000 (+0800) Subject: MSUtils由oc替换成swift X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=refs%2Fheads%2Fswift;p=mirrors%2FVoiceChatSdk_iOS.git MSUtils由oc替换成swift --- diff --git a/MixSdk/Example/Example/ViewController.swift b/MixSdk/Example/Example/ViewController.swift index 71a4711..7baba3d 100644 --- a/MixSdk/Example/Example/ViewController.swift +++ b/MixSdk/Example/Example/ViewController.swift @@ -34,7 +34,7 @@ class ViewController: UIViewController { self.title = "VoiceChat" self.view.backgroundColor = .white self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "扫描", style: .done, target: self, action: #selector(scanAction)) - self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .done, target: self, action: #selector(loginAction)) + self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", style: .done, target: self, action: #selector(registerAction)) tableView.delegate = self tableView.dataSource = self @@ -56,7 +56,7 @@ class ViewController: UIViewController { if peripheral.name == nil || (peripheral.name!.isEmpty){ return } - print(peripheral.name) + print(peripheral) dataArray.append(peripheral) tableView.reloadData() } diff --git a/MixSdk/MixSdk/MixSdk/MSAuth.swift b/MixSdk/MixSdk/MixSdk/MSAuth.swift index 04373d4..cd39993 100644 --- a/MixSdk/MixSdk/MixSdk/MSAuth.swift +++ b/MixSdk/MixSdk/MixSdk/MSAuth.swift @@ -31,7 +31,8 @@ public class MSAuth { let timeStamp = "\(Int(Date().timeIntervalSince1970)*1000)" let value = deviceId + productId + timeStamp - let signValue = MSUtils.string(toHmacSha1: productKey, text: value) +// let signValue = MSUtils.string(toHmacSha1: productKey, text: value) + let signValue = MSTool.stringToHmacSha1(key: productKey, text: value) let params = [ "pid":productId, "deviceId":deviceId, diff --git a/MixSdk/MixSdk/MixSdk/MSBle.swift b/MixSdk/MixSdk/MixSdk/MSBle.swift index 5bfe827..23f19dc 100644 --- a/MixSdk/MixSdk/MixSdk/MSBle.swift +++ b/MixSdk/MixSdk/MixSdk/MSBle.swift @@ -53,8 +53,8 @@ public class MSBle { Logger.log("[recv][info]") return } - Logger.log("[recv][info] " + data.joined("") + MSUtils.data(toUtf8String: data)) - if MSUtils.data(toHexString: data) == "7b2272737369223a2230227d"{ + Logger.log("[recv][info] " + data.joined("") + MSTool.dataToUtf8String(data)) + if data.joined("") == "7b2272737369223a2230227d"{ return } if let sender = lastSender{ @@ -159,7 +159,7 @@ extension MSBle{ let sender = dictionaryToSender(["cmd":"get_MAC"]) sender.completion = { result in Logger.log("\(result.description)") - if result.isSuccess,let value = MSUtils.data(toDictionary: result.data)["MAC"] as? String{ + if result.isSuccess,let dict = MSTool.dataToJson(result.data) ,let value = dict["MAC"] as? String{ finished(value) } else{ @@ -172,7 +172,7 @@ extension MSBle{ let sender = dictionaryToSender(["cmd":"get_productKey"]) sender.completion = { result in Logger.log("\(result.description)") - if result.isSuccess,let value = MSUtils.data(toDictionary: result.data)["productKey"] as? String{ + if result.isSuccess,let dict = MSTool.dataToJson(result.data) ,let value = dict["productKey"] as? String{ finished(value) } else{ @@ -185,7 +185,7 @@ extension MSBle{ let sender = dictionaryToSender(["cmd":"get_secretKey"]) sender.completion = { result in Logger.log("\(result.description)") - if result.isSuccess,let value = MSUtils.data(toDictionary: result.data)["secretKey"] as? String{ + if result.isSuccess,let dict = MSTool.dataToJson(result.data) ,let value = dict["secretKey"] as? String{ finished(value) } else{ @@ -198,7 +198,7 @@ extension MSBle{ let sender = dictionaryToSender(["cmd":"get_deviceSecret"]) sender.completion = { result in Logger.log("\(result.description)") - if result.isSuccess,let value = MSUtils.data(toDictionary: result.data)["deviceSecret"] as? String{ + if result.isSuccess,let dict = MSTool.dataToJson(result.data) ,let value = dict["deviceSecret"] as? String{ finished(value) } else{ @@ -259,8 +259,7 @@ extension MSBle{ extension MSBle{ func dictionaryToSender(_ dict: [String: Any]) ->MSBleSender{ let sender = MSBleSender() - let data = MSUtils.dictionary(toData: dict) - let str = MSUtils.data(toUtf8String: data) + let str = MSTool.dataToUtf8String(MSTool.jsonToData(dict)!) let list = splitCMD(str: str) Logger.log("[send][info][all][string] \(str)") if list.count > 1 { diff --git a/MixSdk/MixSdk/MixSdk/MSVoiceChatSdk-Bridging-Header.h b/MixSdk/MixSdk/MixSdk/MSVoiceChatSdk-Bridging-Header.h index 0204b79..ca0a9eb 100644 --- a/MixSdk/MixSdk/MixSdk/MSVoiceChatSdk-Bridging-Header.h +++ b/MixSdk/MixSdk/MixSdk/MSVoiceChatSdk-Bridging-Header.h @@ -6,5 +6,5 @@ // #import "AudioDecode/AiAudioDecoder.h" -#import "Tools/MSUtils.h" +//#import "Tools/MSUtils.h" #import "AudioTransform/AudioTransform.h" diff --git a/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift b/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift index d7b481a..995dba2 100644 --- a/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift +++ b/MixSdk/MixSdk/MixSdk/Tools/MSTool.swift @@ -6,8 +6,14 @@ // import UIKit +import CommonCrypto class MSTool{ + static func dataToUtf8String(_ data:Data) ->String{ + let text = String(data: data, encoding: .utf8) + return text ?? "" + } + static func stringToJson(_ text:String)->[String:Any]?{ guard let data = text.data(using: .utf8) else{ return nil @@ -24,7 +30,7 @@ class MSTool{ } static func jsonToData(_ json:[String:Any])->Data?{ do { - let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) + let jsonData = try JSONSerialization.data(withJSONObject: json, options: .fragmentsAllowed) return jsonData } catch { return nil @@ -79,4 +85,16 @@ class MSTool{ } return randomString } + + static func stringToHmacSha1(key: String, text: String) -> String { + let cKey = key.cString(using:.utf8)! + let cData = text.cString(using:.utf8)! + var cHMAC = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH)) + CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), cKey, strlen(cKey), cData, strlen(cData), &cHMAC) + var output = "" + for i in 0..