Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Marvel api返回哈希、时间戳和键组合无效_Ios_Swift - Fatal编程技术网

Ios Marvel api返回哈希、时间戳和键组合无效

Ios Marvel api返回哈希、时间戳和键组合无效,ios,swift,Ios,Swift,我正在使用此解决方案转换为md5哈希 这是我创建散列参数并发送给marvel的代码 class CharacterListInteractorApi: CharacterListInteractor { // MARK: Dependencies private let client: NetworkLayer // MARK: - Properties var timeStamp: String = "" // MARK: - Life cycl

我正在使用此解决方案转换为md5哈希

这是我创建散列参数并发送给marvel的代码

class CharacterListInteractorApi: CharacterListInteractor {

    // MARK: Dependencies
    private let client: NetworkLayer

    // MARK: - Properties

    var timeStamp: String = ""

    // MARK: - Life cycle

    init(client: NetworkLayer) {
        self.client = client
    }

    // MARK: - Internal

    func verifySomething(someInput: String) -> Observable<Async<Any>> {
        timeStamp = Date().stringValue()
        return RxAlamofire
            .requestJSON(
                .get,
                 url,
                 parameters: parameters
            )
            .flatMap { (response, json) -> Observable<Any> in
                Observable.just(json)
            }.async()
    }
}

private extension CharacterListInteractorApi {
    var url: String {
        "https://gateway.marvel.com:443/v1/public/characters?apikey=ApiKey"
    }


    var hash: String { timeStamp+"privateKey"+"fbb2d7f9074949c9fb335f9a42e48678"
    }

    var md5Hash: Data {
        MD5(string: hash)
    }

    var parameters: [String: Any] {
        [
            "ts": timeStamp,
            "hash": md5Hash
        ]
    }
}
这是md5散列的打印输出

(lldb) po md5Hash
▿ 16 bytes
  - count : 16
  ▿ pointer : 0x0000600003941c20
    - pointerValue : 105553176304672
  ▿ bytes : 16 elements
    - 0 : 188
    - 1 : 255
    - 2 : 118
    - 3 : 230
    - 4 : 64
    - 5 : 100
    - 6 : 140
    - 7 : 75
    - 8 : 144
    - 9 : 42
    - 10 : 18
    - 11 : 136
    - 12 : 94
    - 13 : 27
    - 14 : 230
    - 15 : 252

我就是这样解决的

var md5: String {
    let data = self.data(using: .utf8)
    var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))

    _ = data!.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
        return CC_MD5(bytes.baseAddress, CC_LONG(data!.count), &digest)
    }

    return digest.reduce(into: "") { $0 += String(format: "%02x", $1) }
}

timeStamp=Date()。stringValue()将返回自1970年以来的时间?@Abulhassan我包括stringValue方法我个人认为它将是基于TIMINTERVALUCENCES1970的时间戳,而不是格式化的日期字符串。@Abulhassan他们在文档中提到ts-时间戳(或其他长字符串,可以根据请求逐个更改)所以我很确定这不是问题所在。我从我的has中添加了一个打印输出,以字节为单位,而不是十六进制十进制,我怀疑这是问题所在。
var md5: String {
    let data = self.data(using: .utf8)
    var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))

    _ = data!.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
        return CC_MD5(bytes.baseAddress, CC_LONG(data!.count), &digest)
    }

    return digest.reduce(into: "") { $0 += String(format: "%02x", $1) }
}