Skip to main content

Signature Generation

The signature is used to authenticate requests sent to the SNAP API. Signatures are generated from data sent to the Snap API. Winpay uses the type Asymmetric Without Get Token to generate signatures, so merchants do not need to request get tokens first to create signatures.

Private Key & Public Key

Before a merchant can generate a signature, it must have an RSA private key and a public key. To get the private key and public key, merchants can generate their own or use private keys and public keys from third-party sites such as: https://cryptotools.net/rsagen.

Formula Signature

Signatures are generated from data sent to the Snap API. Here is the formula for generating a signature:


stringToSign = HTTPMethod +":"+ EndpointUrl +":"+ Lowercase(HexEncode(SHA-256(minify(RequestBody)))) + ":" + TimeStamp
signature = base64_encode(SHA256withRSA(private_key, stringToSign))

Source Code Signature Creation

$httpMethod = 'POST';
$endpointUrl = '/v1.0/transfer-va/create-va';
$timestamp = '2023-09-19T12:11:14+07:00';
$payload = '
{
"customerNo": "08123456789",
"virtualAccountName": "CHUS PANDI",
"trxId": "INV-000000001",
"totalAmount": {
"value": "10000.00",
"currency": "IDR"
},
"virtualAccountTrxType": "c",
"expiredDate": "2023-11-02T17:18:48+07:00",
"additionalInfo": {
"channel": "BSI"
}
}
';

$body = json_decode($payload);
$hashedBody = strtolower(bin2hex(hash('sha256', json_encode($body, JSON_UNESCAPED_SLASHES), true)));

$stringToSign = [
$httpMethod,
$endpointUrl,
$hashedBody,
$timestamp
];

$signature = '';
$stringToSign = implode(':', $stringToSign);

$privKey = openssl_get_privatekey('./your-private-key-file.pem');
openssl_sign($stringToSign, $signature, $privKey, OPENSSL_ALGO_SHA256);
$encodedSignature = base64_encode($signature);
echo 'Your Signature:' $encodedSignature.