Skip to content

Instantly share code, notes, and snippets.

@InvoxiPlayGames
Created February 16, 2023 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InvoxiPlayGames/896f3f75cc41b1bc87557715ba566976 to your computer and use it in GitHub Desktop.
Save InvoxiPlayGames/896f3f75cc41b1bc87557715ba566976 to your computer and use it in GitHub Desktop.
Discord Interactions signature validation in native PHP 7.2+
<?php
$timestamp = $_SERVER['HTTP_X_SIGNATURE_TIMESTAMP'];
$signature = hex2bin($_SERVER['HTTP_X_SIGNATURE_ED25519']);
if ($signature === false) {
die(http_response_code(403));
}
$publickey = hex2bin("[ YOUR_APP_PUBLIC_KEY ]"); // REPLACE THIS WITH YOUR OWN APP'S!
$postdata = file_get_contents('php://input');
// signature verification;
// requires PHP7.2+ compiled with sodium support and extension=sodium to be enabled in php.ini
$data_to_verify = $timestamp . $postdata;
if (sodium_crypto_sign_verify_detached($signature, $data_to_verify, $publickey) !== true) {
die(http_response_code(403));
}
// parse your $postdata here...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment