Skip to content

Instantly share code, notes, and snippets.

@JoeAlamo
Last active April 7, 2016 12:06
Show Gist options
  • Save JoeAlamo/b225e7c7736c34424e42f88b63ebb8e4 to your computer and use it in GitHub Desktop.
Save JoeAlamo/b225e7c7736c34424e42f88b63ebb8e4 to your computer and use it in GitHub Desktop.
<?php
// vectors
$key = pack('H*', '1C9240A5EB55D38AF333888604F6B5F0473917C1402B80099DCA5CBC207075C0');
$nonce = pack('H*', '0102030405060708');
$additionalData = pack('H*', 'F33388860000000000004E91');
$plaintext = pack('H*', '496E7465726E65742D4472616674732061726520647261667420646F63756D656E74732076616C696420666F722061206D6178696D756D206F6620736978206D6F6E74687320616E64206D617920626520757064617465642C207265706C616365642C206F72206F62736F6C65746564206279206F7468657220646F63756D656E747320617420616E792074696D652E20497420697320696E617070726F70726961746520746F2075736520496E7465726E65742D447261667473206173207265666572656E6365206D6174657269616C206F7220746F2063697465207468656D206F74686572207468616E206173202FE2809C776F726B20696E2070726F67726573732E2FE2809D');
$plaintextLen = mb_strlen($plaintext, '8bit');
$expectedTag = pack('H*', 'EEAD9D67890CBB22392336FEA1851F38');
$ciphertext = \Sodium\crypto_aead_chacha20poly1305_encrypt($plaintext, $additionalData, $nonce, $key);
$actualTag = substr($ciphertext, $plaintextLen, 16);
echo '<pre>';
echo "KEY:\n";
echo bin2hex($key);
echo "\nPLAINTEXT:\n";
echo bin2hex($plaintext);
echo "\nADDITIONAL DATA:\n";
echo bin2hex($additionalData);
echo "\nNONCE:\n";
echo bin2hex($nonce);
echo "\nEXPECTED TAG:\n";
echo bin2hex($expectedTag);
echo "\nCIPHERTEXT:\n";
echo bin2hex($ciphertext);
echo "\nACTUAL TAG:\n";
echo bin2hex($actualTag);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment