【问题标题】:Kotlin(Android) MD5 checking with PHP SignatureKotlin(Android) MD5 使用 PHP 签名检查
【发布时间】:2019-08-07 11:56:57
【问题描述】:

我已经为此工作了好几天。

我们的后端有一个使用 PHP 完成的签名检查:

private $HMAC_ALGO = 'md5';

public function decodeAndValidateMessage($data,$signature,$secretkey) {
    if (!is_string($data)) {
      throw new InvalidRequestException($data);
    }

    $decodedData = base64_decode($data);

    // if not json returned the throw exception...
    $jsonDecoded = json_decode($decodedData,true);
    if (!$jsonDecoded) {
      throw new InvalidRequestException($decodedData);
    }

    // validate
    $signatureRef = base64_encode(hash_hmac($this->HMAC_ALGO,$decodedData,$secretkey,true));
    if ($signature === $signatureRef) {
      return $jsonDecoded;
    } else {
      throw new InvalidSignatureException();
    }
}

我让它在 iOS 上运行:

func hmac(_ algorithm: HMACAlgorithm, key: String) -> String {
    let cKey = key.cString(using: String.Encoding.utf8)
    let cData = self.cString(using: String.Encoding.utf8)
    var result = [CUnsignedChar](repeating: 0, count: Int(algorithm.digestLength()))
    CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, Int(strlen(cKey!)), cData!, Int(strlen(cData!)), &result)
    let hmacData:Data = Data(bytes: UnsafePointer<UInt8>(result), count: (Int(algorithm.digestLength())))
    let hmacBase64 = hmacData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    print(String(hmacBase64))
    return String(hmacBase64)
}

关于在 Kotlin/Android 上执行此操作的任何想法/帮助?我被困在InvalidSignatureException 错误上。

fun generateSignature(data : HashMap<String, Any>) : String {
    val hmac = Mac.getInstance("HmacMD5")
    hmac.init(SecretKeySpec(Constant.PRIVATEKEY.toByteArray(Charsets.UTF_8), hmac.algorithm))
    return Base64.encodeToString(data.toString().toByteArray(),Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_CLOSE + Base64.NO_WRAP)
}

谢谢 :D 非常感谢您的帮助 :D

更新:

只是为了让我的问题更简单?

是否可以将iOS代码行翻译成Kotlin?

枚举 HMAC 算法 {

case md5, sha1, sha224, sha256, sha384, sha512

func toCCHmacAlgorithm() -> CCHmacAlgorithm {
    var result: Int = 0
    switch self {
    case .md5:
        result = kCCHmacAlgMD5
    case .sha1:
        result = kCCHmacAlgSHA1
    case .sha224:
        result = kCCHmacAlgSHA224
    case .sha256:
        result = kCCHmacAlgSHA256
    case .sha384:
        result = kCCHmacAlgSHA384
    case .sha512:
        result = kCCHmacAlgSHA512
    }
    return CCHmacAlgorithm(result)
}

func digestLength() -> Int {
    var result: CInt = 0
    switch self {
    case .md5:
        result = CC_MD5_DIGEST_LENGTH
    case .sha1:
        result = CC_SHA1_DIGEST_LENGTH
    case .sha224:
        result = CC_SHA224_DIGEST_LENGTH
    case .sha256:
        result = CC_SHA256_DIGEST_LENGTH
    case .sha384:
        result = CC_SHA384_DIGEST_LENGTH
    case .sha512:
        result = CC_SHA512_DIGEST_LENGTH
    }
    return Int(result)
}

}

这就是我调用函数的方式

var 参数:字典

params.generateSignature()

【问题讨论】:

  • 您能否提供有关该错误的更多详细信息?也许回溯和一些数据样本以其各自的预期结果传递给函数。它将帮助人们定位错误的根源。
  • 你好@DamienFlament 我不能给出它包含敏感数据的数据......
  • @DamienFlament。让我们说 hashmap 也是 iOS 的 json 字符串
  • $signatureRef = base64_encode(hash_hmac($this->HMAC_ALGO,$decodedData,$secretkey,true)); if ($signature === $signatureRef) { return $jsonDecoded; } else { 抛出新的 InvalidSignatureException(); }
  • 这是错误的来源。看来 android/kotlin 的 md5 不一样

标签: php android ios kotlin hash


【解决方案1】:

fun generateSignature(data : HashMap) : String { val hmac = Mac.getInstance("HmacMD5") hmac.init(SecretKeySpec(Constant.PRIVATEKEY.toByteArray(Charsets.UTF_8), hmac.algorithm)) 返回 Base64.encodeToString(data.toString().toByteArray(),Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_CLOSE + Base64.NO_WRAP) }

终于有人找到答案了。

我的错误是 hashmap 应该在 JSONObject 下运行

var obj = JsonObject(数据)

并使用 obj.toString() :D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多