dtl19910708 2019-03-16 16:25
浏览 163

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

I been working on this for days.

Our backend have a signature checking which is done using 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();
    }
}

I made it work on 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)
}

Any idea/help on doing this on Kotlin/Android? I'm being stuck on InvalidSignatureException error.

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)
}

Thanks :D I really appreciate for any help :D

Update:

Just to make my question simpler?

Is it possible to make translate the iOS line of code to Kotlin?

enum 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)
}

}

this is how I call the function

var params : Dictionary

params.generateSignature()

  • 写回答

1条回答 默认 最新

  • doufei1988 2019-03-18 16:46
    关注

    fun generateSignature(data : HashMap) : 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) }

    Someone finally found out the answer.

    My mistake is hashmap should be run under JSONObject

    var obj = JsonObject(data)

    and use obj.toString() :D

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题