PHP: How can I generate a HmacSHA256 signature of a string

前端 未结 4 1546
误落风尘
误落风尘 2020-12-08 06:18

Is there any way to create a HmacSHA256 signature of a string in php?

4条回答
  •  难免孤独
    2020-12-08 06:59

    Here is an example of datatrans transaction signing (swiss e-payment solution) before call PSP with HMAC-SHA-256.

    Hope it could help some developers.

    $hmacKey    = 30911337928580013;
    
    $merchantId = 1100004624;
    $amount     = $total * 100;
    $currency   = 'EUR';
    $refno      = $orderId;
    
    // HMAC Hex to byte
    $secret     = hex2bin("$hmacKey");
    
    // Concat infos
    $string     = $merchantId . $amount. $currency . $refno;
    
    // generate SIGN
    $sign       = bin2hex(hash_hmac('sha256', $string, $secret)); 
    

    Note: the merchant ID and HMAC key are both from Datatrans documentation available here : https://admin.sandbox.datatrans.com/showcase/doc/Technical_Implementation_Guide.pdf

提交回复
热议问题