使用PHP签名检查Kotlin(Android)MD5

问题描述 投票:1回答:1

我这几天都在研究这个问题。

我们的后端有一个使用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?

枚举HMACAlgorithm {

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:字典

params.generateSignature()

php android ios kotlin hash
1个回答
0
投票

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

有人终于找到了答案。

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

加载对象= json对象(数据)

并使用obj.toString():D

© www.soinside.com 2019 - 2024. All rights reserved.