API signature
Signature verification
ฝั่งที่รับข้อมูลต้องทำการตรวจสอบความถูกต้องและความปลอดภัยของข้อมูล ซึ่งจะส่งผ่าน header ที่มีชื่อว่า x-casino-signature โดยจะต้องใช้ authentication key ในการตรวจสอบ สามารถดูตัวอย่างโค้ดได้ตามข้างล่างนี้
String authenticationKey = '...'; // authentication key string
String httpRequestBody = '...'; // Request body string
SecretKeySpec key = new SecretKeySpec(authenticationKey.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key);
byte[] source = httpRequestBody.getBytes("UTF-8");
String signature = Base64.encodeBase64String(mac.doFinal(source));
// Compare x-signature-signature request header string and the signature
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
// ...
}
decoded, err := base64.StdEncoding.DecodeString(req.Header.Get("x-casino-signature"))
if err != nil {
// ...
}
hash := hmac.New(sha256.New, []byte("<authenticationKey>"))
hash.Write(body)
// Compare decoded signature and `hash.Sum(nil)` by using `hmac.Equal`
$authenticationKey = '...'; // Authentication key string
$httpRequestBody = '...'; // Request body string
$hash = hash_hmac('sha256', $httpRequestBody, $authenticationKey, true);
$signature = base64_encode($hash);
// Compare x-casino-signature request header string and the signature
const crypto = require("crypto");
const authenticationKey = "..."; // authentication key string
const body = "..."; // Request body string
const signature = crypto
.createHmac("SHA256", authenticationKey)
.update(body)
.digest("base64");
// Compare x-casino-signature request header and the signature
import base64
import hashlib
import hmac
authenticationKey = '...' # authentication key string
body = '...' # Request body string
hash = hmac.new(authenticationKey.encode('utf-8'),
body.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(hash)
# Compare x-casino-signature request header and the signature
Signature generation
ฝั่งที่ส่งข้อมูลต้องทำการสร้าง x-casino-signature แล้วส่งผ่าน header เพื่อให้ฝั่งรับใช้สำหรับตรวจสอบความถูกต้องและความปลอดภัยของข้อมูล
String authenticationKey = '...'; // authentication key string
String httpRequestBody = '...'; // Request body string
SecretKeySpec key = new SecretKeySpec(authenticationKey.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(key);
byte[] source = httpRequestBody.getBytes("UTF-8");
String signature = Base64.encodeBase64String(mac.doFinal(source));
req := map[string]interface{}{
}
body, err := json.Marshal(req)
if err != nil {
// ...
}
hash := hmac.New(sha256.New, []byte("<authenticationKey>"))
hash.Write(body)
signature := base64.StdEncoding.EncodeToString(hash.Sum(nil))
$authenticationKey = '...'; // Authentication key string
$httpRequestBody = '...'; // Request body string
$hash = hash_hmac('sha256', $httpRequestBody, $authenticationKey, true);
$signature = base64_encode($hash);
const crypto = require("crypto");
const authenticationKey = "..."; // authentication key string
const body = "..."; // Request body string
const signature = crypto
.createHmac("SHA256", authenticationKey)
.update(body)
.digest("base64");
import base64
import hashlib
import hmac
authenticationKey = '...' # authentication key string
body = '...' # Request body string
hash = hmac.new(authenticationKey.encode('utf-8'),
body.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(hash)
Last updated