参数类型 'SecretSpec' 不符合预期的类型 'Sequence'

Posted

技术标签:

【中文标题】参数类型 \'SecretSpec\' 不符合预期的类型 \'Sequence\'【英文标题】:Argument type 'SecretSpec' does not conform to expected type 'Sequence'参数类型 'SecretSpec' 不符合预期的类型 'Sequence' 【发布时间】:2019-05-28 09:02:35 【问题描述】:

我想在 Playground 上加密一条消息。我的代码如下。我写了另一个类来生成一个密钥。将密钥转换为字符串时,会报错。

import UIKit

import Foundation
import CommonCrypto



class SecretSpec 
    var algorithm: String = "AES/ECB/PKCS5padding"
    var key = [UInt8]()


    func SecretSpec(key: [UInt8], algorithm: String)
        self.key = key
        self.algorithm = algorithm
    

    func getAlgorithm() -> String 
        return self.algorithm
    

    func getFormat() -> String 
        return "RAW"
    

    func getEncoded() -> [UInt8] 
        return self.key
    



    func hashCode() -> Int 
        var retval: Int = 0
        for i in 1...key.count-1 
            retval = retval + Int(key[i]) * Int(i)
        

        if (algorithm.lowercased() == "tripledes")
            retval = retval ^ Int("desede".hashValue)
            return retval
        else
            retval = retval ^ Int(algorithm.lowercased().hashValue)
            return retval
        
    




extension String 

    func aesEncrypt(key: String, options:Int = (kCCOptionECBMode + kCCOptionPKCS7Padding)) -> String? 
        if let keyData = key.data(using: String.Encoding.utf8),
            let data = self.data(using: String.Encoding.utf8),
            let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128) 

            let keyLength = size_t(kCCKeySizeAES128)
            let operation: CCOperation = UInt32(kCCEncrypt)
            let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
            let options: CCOptions = UInt32(options)

            var numBytesEncrypted :size_t = 0

            let cryptStatus = CCCrypt(operation, algoritm, options,
                                      (keyData as NSData).bytes, keyLength,
                                      nil, (data as NSData).bytes, data.count,
                                      cryptData.mutableBytes, cryptData.length,
                                      &numBytesEncrypted)


            if UInt32(cryptStatus) == UInt32(kCCSuccess) 
                cryptData.length = Int(numBytesEncrypted)

                var bytes = [UInt8](repeating: 0, count: cryptData.length)
                cryptData.getBytes(&bytes, length: cryptData.length)

                var hexString = ""
                for byte in bytes 
                    hexString += String(format:"%02x", UInt8(byte))
                

                return hexString
            
            else 
                return nil
            
        
        return nil
    




func MD5(_ string: String) -> String? 
    let length = Int(CC_MD5_DIGEST_LENGTH)
    var digest = [UInt8](repeating: 0, count: length)

    if let d = string.data(using: String.Encoding.utf8) 
        _ = d.withUnsafeBytes  (body: UnsafePointer<UInt8>) in
            CC_MD5(body, CC_LONG(d.count), &digest)
        
    


    return (0..<length).reduce("") 
        $0 + String(format: "%02x", digest[$1])
    




var mdT = "YourStrongKeyasdfghjklzxcvbnm"
var algorithm: String = "AES/ECB/PKCS7padding"

var s = MD5(mdT)
let buf: [UInt8] = Array(s!.utf8)

var skcSpec = SecretSpec()
skcSpec.SecretSpec(key: buf, algorithm: algorithm)



print("hello: \(skcSpec)")


skcSpec.getEncoded()
skcSpec.getFormat()
skcSpec.getAlgorithm()
skcSpec.hashCode()


let msg: NSMutableDictionary = NSMutableDictionary()

msg.setValue("uttam kumar", forKey: "name")
msg.setValue("1001", forKey: "id")

let msgData: NSData
var msgStr: String = ""
var requestUrl: String = ""

do 
    msgData = try JSONSerialization.data(withJSONObject: msg, options: JSONSerialization.WritingOptions()) as NSData
    msgStr = NSString(data: msgData as Data, encoding: String.Encoding.utf8.rawValue)! as String

 catch _ 
    print ("JSON Failure")



var skc = String(data: Data(skcSpec), encoding: .utf8)!
var encoded = msgStr.aesEncrypt(key: String(skc))!

print("encoded: \(encoded)")

我想得到'skc'的字符串值。并打印出来。但它给出了“参数类型'SecretSpec'不符合预期类型'Sequence'”错误。请帮帮我。

【问题讨论】:

【参考方案1】:

Data 没有自定义类型的初始化程序。您的意思是获取编码值吗?

var skc = String(data: Data(skcSpec.getEncoded()), encoding: .utf8)!

【讨论】:

以上是关于参数类型 'SecretSpec' 不符合预期的类型 'Sequence'的主要内容,如果未能解决你的问题,请参考以下文章

参数类型 '[HKCategoryType?]' 不符合预期的类型 'Hashable'

参数类型“customClass.Type”不符合预期类型“NSItemProviderWriting”

参数类型 'Range<Int>' 不符合预期的类型 'Sequence' Swift3

如何将符合协议的类声明为参数类型?

抽象通用视图持有者的类预期的 Kotlin 一种类型参数

Swift Init 不符合预期的“解码器”类型