PHP code example of brainfoolong / php-ascon

1. Go to this page and download the library: Download brainfoolong/php-ascon library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brainfoolong / php-ascon example snippets


use Nullix\Ascon\Ascon;
// convenient usage (generating random nonce and hashing keys for you)
$key = "mypassword";
$message = ["this can be any data type 😎 文 or encoding", 123];
$associatedData = "Some data 😋 文 This data is not contained in the encrypt output but must be passed to both encrypt and decrypt.";
$encrypted = Ascon::encryptToHex($key, $message, $associatedData);
$decrypted = Ascon::decryptFromHex($key, $encrypted, $associatedData);

// raw usage of basic methods
// key must be 16 bytes or 20 bytes, depending on variant
$key = [0x90, 0x80, 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0xAA, 0x90, 0x90, 0x90, 0x90, 0xCC, 0xEF];
// nonce must be 16 bytes and should always be random bytes, you must use same nonce for encrypt and decrypt the same message
$nonce = random_bytes(16);
// this is the text you want to encrypt
$plaintext = "Hi, i am a secret message!";
// associated data is not being encrypted, but is taken into account in the ciphertext
// this means, you can only decrypt when you pass the exact same associated data to the decrypt function as well
// so you can make sure that associated data and plaintext is not manipulated for given encrypted message
// this is optional and can be an empty string
$associatedData = "Some data to pass to encryption and decryption - This data is not contained in the ciphertext output.";
$ciphertextByteArray = Ascon::encrypt($key, $nonce, $associatedData, $plaintext);
$plaintextDecrypted = Ascon::decrypt($key, $nonce, $associatedData, $ciphertextByteArray);

var_dump(Ascon::hash('Testmessage'));
var_dump(Ascon::mac($key, 'Testmessage'));