0

I'm using this solution to convert to md5 hash https://stackoverflow.com/a/32166735/1898829

and this is the code that I create the hash parameter I send to 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
        ]
    }
}

but I get an error from marvel saying That hash, timestamp and key combination is invalid

update

here is the date time func

   func stringValue() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSS'Z'"
        return dateFormatter.string(from: self)
    }

here is the print out of the md5 hash

(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
user1898829
  • 3,437
  • 6
  • 34
  • 62

1 Answers1

0

I solved it by doing this

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) }
}
user1898829
  • 3,437
  • 6
  • 34
  • 62