Calculating the Signature
Learn how to calculate and send the Signature header value to verify requests integrity
Last updated
Was this helpful?
Was this helpful?
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public static final String D24_AUTHORIZATION_SCHEME = "D24 ";
private static final String HMAC_SHA256 = "HmacSHA256";
public static String buildCashoutKeySignature(String apiSignature, String xDate, String cashoutApiKey, String JSONPayload)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
byte[] hmacSha256 = null;
Mac mac = Mac.getInstance(HMAC_SHA256);
SecretKeySpec secretKeySpec = new SecretKeySpec(apiSignature.getBytes(StandardCharsets.UTF_8), HMAC_SHA256);
mac.init(secretKeySpec);
hmacSha256 = mac.doFinal(buildByteArray(xDate, cashoutApiKey, JSONPayload));
return D24_AUTHORIZATION_SCHEME + toHexString(hmacSha256);
}
private static byte[] buildByteArray(String xDate, String cashoutApiKey, String JSONPayload) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(xDate.getBytes(StandardCharsets.UTF_8));
bos.write(cashoutApiKey.getBytes(StandardCharsets.UTF_8));
if (JSONPayload != null) {
bos.write(payload.getBytes(StandardCharsets.UTF_8));
}
return bos.toByteArray();
}
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Application
{
class Directa24Example
{
public readonly static string D24_AUTHORIZATION_SCHEME = "D24 ";
private readonly static string HMAC_SHA256 = "HmacSHA256";
public static String buildCashoutKeySignature(String apiSignature, String xDate, String cashoutApiKey, String jsonPayload)
{
byte[] hmacSha256 = null;
var apiSignatureEncod = Encoding.UTF8.GetBytes(apiSignature);
var hash = new HMACSHA256(apiSignatureEncod);
hmacSha256 = hash.ComputeHash(buildByteArray(xDate, cashoutApiKey, jsonPayload));
return D24_AUTHORIZATION_SCHEME + toHexString(hmacSha256).ToLower();
}
private static byte[] buildByteArray(String xDate, String cashoutApiKey, String jsonPayload)
{
try
{
MemoryStream stream = new MemoryStream();
var xDateEncod = Encoding.UTF8.GetBytes(xDate);
var cashoutApiKeyEncod = Encoding.UTF8.GetBytes(cashoutApiKey);
stream.Write(xDateEncod, 0, xDateEncod.Length);
stream.Write(cashoutApiKeyEncod, 0, cashoutApiKeyEncod.Length);
if (!string.IsNullOrWhiteSpace(jsonPayload))
{
var jsonPayloadEncod = Encoding.UTF8.GetBytes(jsonPayload);
stream.Write(jsonPayloadEncod, 0, jsonPayloadEncod.Length);
}
return stream.ToArray();
}
catch (Exception ex)
{
throw ex;
}
}
private static string toHexString(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", string.Empty);
}
}
}<?php
class Directa24Example {
const D24_AUTHORIZATION_SCHEME = "D24 ";
const HMAC_SHA256 = 'sha256';
public static function build_cashout_key_signature($api_signature, $x_date, $cashout_api_key, $json_payload) {
// Concatenate the content of the header X-Date, your deposits API Key (X-Login) and
// the whole JSON payload of the body of the request
$string = $x_date . $cashout_api_key . $json_payload;
// Generate the HASH by using yur own deposits API Signature and
// concatenate "D24 " in front of the hash
return self::D24_AUTHORIZATION_SCHEME . hash_hmac(self::HMAC_SHA256, $string, $api_signature);
}
}