Ios 如何向NWProtocolTLS.Options()添加自定义字符串

Ios 如何向NWProtocolTLS.Options()添加自定义字符串,ios,swift,networking,Ios,Swift,Networking,Multipeer Connectivity framework具有一个参数,用于添加peerID,例如设备名称: var peerID: MCPeerID! var mcSession: MCSession! peerID = MCPeerID(displayName: UIDevice.current.name) mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .require

Multipeer Connectivity framework具有一个参数,用于添加peerID,例如设备名称:

var peerID: MCPeerID!
var mcSession: MCSession!

peerID = MCPeerID(displayName: UIDevice.current.name)
mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required)
如何将相同的内容添加到NWProtocolTLS.Options()

我看了看苹果公司在其发展过程中的做法。他们使用安全密码,但我不需要任何复杂的东西,我只需要设备名或uid

使用

苹果的代码:

// Create TLS options using a passcode to derive a pre-shared key.
func tlsOptions(passcode: String) -> NWProtocolTLS.Options {
    let tlsOptions = NWProtocolTLS.Options()

    let authenticationKey = SymmetricKey(data: passcode.data(using: .utf8)!)
    var authenticationCode = HMAC<SHA256>.authenticationCode(for: "TicTacToe".data(using: .utf8)!, using: authenticationKey)

    let authenticationDispatchData = withUnsafeBytes(of: &authenticationCode) { (ptr: UnsafeRawBufferPointer) in
        DispatchData(bytes: ptr)
    }

    sec_protocol_options_add_pre_shared_key(tlsOptions.securityProtocolOptions,
                                            authenticationDispatchData as __DispatchData,
                                            stringToDispatchData("TicTacToe")! as __DispatchData)
    sec_protocol_options_append_tls_ciphersuite(tlsOptions.securityProtocolOptions,
                                                tls_ciphersuite_t(rawValue: TLS_PSK_WITH_AES_128_GCM_SHA256)!)
    return tlsOptions
}

// Create a utility function to encode strings as pre-shared key data.
func stringToDispatchData(_ string: String) -> DispatchData? {
    guard let stringData = string.data(using: .unicode) else {
        return nil
    }
    let dispatchData = withUnsafeBytes(of: stringData) { (ptr: UnsafeRawBufferPointer) in
        DispatchData(bytes: UnsafeRawBufferPointer(start: ptr.baseAddress, count: stringData.count))
    }
    return dispatchData
}
//使用密码派生预共享密钥创建TLS选项。
func tlsOptions(密码:String)->NWProtocolTLS.Options{
设tlsOptions=NWProtocolTLS.Options()
让authenticationKey=SymmetricKey(数据:passcode.data(使用:.utf8)!)
var authenticationCode=HMAC.authenticationCode(用于“TicTacToe”.data(使用:.utf8)!,使用:authenticationKey)
让authenticationDispatchData=withUnsafeBytes(of:&authenticationCode){(ptr:UnsafeRawBufferPointer)位于
DispatchData(字节:ptr)
}
sec协议选项添加预共享密钥(tlsOptions.securityProtocolOptions、,
身份验证DispatchData为uu DispatchData,
stringToDispatchData(“TicTacToe”)!作为DispatchData)
sec_协议_选项_附加_tls_密码套件(tlsOptions.securityProtocolOptions,
tls_密码套件(原始值:tls_PSK_,带AES_128_GCM_SHA256)!)
返回时间间隔
}
//创建一个实用函数,将字符串编码为预共享密钥数据。
func stringToDispatchData(\uString:string)->DispatchData?{
guard let stringData=string.data(使用:.unicode)else{
归零
}
让dispatchData=withUnsafeBytes(of:stringData){(ptr:UnsafeRawBufferPointer)在
DispatchData(字节:UnsafeRawBufferPointer(开始:ptr.baseAddress,计数:stringData.count))
}
返回调度数据
}
// Create TLS options using a passcode to derive a pre-shared key.
func tlsOptions(passcode: String) -> NWProtocolTLS.Options {
    let tlsOptions = NWProtocolTLS.Options()

    let authenticationKey = SymmetricKey(data: passcode.data(using: .utf8)!)
    var authenticationCode = HMAC<SHA256>.authenticationCode(for: "TicTacToe".data(using: .utf8)!, using: authenticationKey)

    let authenticationDispatchData = withUnsafeBytes(of: &authenticationCode) { (ptr: UnsafeRawBufferPointer) in
        DispatchData(bytes: ptr)
    }

    sec_protocol_options_add_pre_shared_key(tlsOptions.securityProtocolOptions,
                                            authenticationDispatchData as __DispatchData,
                                            stringToDispatchData("TicTacToe")! as __DispatchData)
    sec_protocol_options_append_tls_ciphersuite(tlsOptions.securityProtocolOptions,
                                                tls_ciphersuite_t(rawValue: TLS_PSK_WITH_AES_128_GCM_SHA256)!)
    return tlsOptions
}

// Create a utility function to encode strings as pre-shared key data.
func stringToDispatchData(_ string: String) -> DispatchData? {
    guard let stringData = string.data(using: .unicode) else {
        return nil
    }
    let dispatchData = withUnsafeBytes(of: stringData) { (ptr: UnsafeRawBufferPointer) in
        DispatchData(bytes: UnsafeRawBufferPointer(start: ptr.baseAddress, count: stringData.count))
    }
    return dispatchData
}