PHP code example of fpoirotte / tomcrypt

1. Go to this page and download the library: Download fpoirotte/tomcrypt 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/ */

    

fpoirotte / tomcrypt example snippets


        $algo       = TOMCRYPT_CIPHER_RIJNDAEL;
        $mode       = TOMCRYPT_MODE_ECB;
        $plaintext  = "Confidential msg";
        $key        = "some secret key!";
        $ciphertext = tomcrypt_cipher_encrypt($algo, $key, $plaintext, $mode);
    

        $algo       = TOMCRYPT_CIPHER_RIJNDAEL;
        $mode       = TOMCRYPT_MODE_ECB;
        $key        = "some secret key!";
        $plaintext  = tomcrypt_cipher_decrypt($algo, $key, $ciphertext, $mode);
    

        $algo = TOMCRYPT_HASH_SHA256;

        // Returns the hash value for the given data in hexadecimal form
        $hash = tomcrypt_hash_string($algo, $data, false);

        // Returns the hash value for the given data in raw (binary) form
        $hash = tomcrypt_hash_string($algo, $data, true);

        // Returns the hash value for the given file in raw (binary) form
        $hash = tomcrypt_hash_file($algo, "/tmp/file", true);
    

        $algo1  = TOMCRYPT_MAC_HMAC;
        $hash   = TOMCRYPT_HASH_SHA1;
        $key    = "my secret key...";
        $data   = "some data here";

        // Returns the HMAC for the given data in hexadecimal form,
        // using the SHA-1 hashing algorithm.
        $hmac   = tomcrypt_mac_string($algo1, $hash, $key, $data, false);

        // Returns the PMAC for the given data in raw (binary) form,
        // using the Rijndael cipher algorithm.
        $algo2  = TOMCRYPT_MAC_PMAC;
        $cipher = TOMCRYPT_CIPHER_RIJNDAEL;
        $pmac   = tomcrypt_mac_string($algo2, $cipher, $key, $data, true);

        // Returns the HMAC for the given file in raw (binary) form,
        // using the SHA-1 hashing algorithm.
        $hmac   = tomcrypt_mac_file($algo1, $hash, $key, "/tmp/file", true);
    

        // Attempt to get 42 bytes of purely random data.
        // Returns FALSE if random data cannot be obtained in a secure way.
        $random = tomcrypt_rng_get_bytes(42, TOMCRYPT_RNG_SECURE);
    

        $rand0 = tomcrypt_rng_get_bytes(8, TOMCRYPT_RNG_RC4);
        $state = tomcrypt_rng_export(TOMCRYPT_RNG_RC4);

        tomcrypt_rng_import(TOMCRYPT_RNG_RC4, $state);
        $rand1 = tomcrypt_rng_get_bytes(8, TOMCRYPT_RNG_RC4);
        tomcrypt_rng_import(TOMCRYPT_RNG_RC4, $state);
        $rand2 = tomcrypt_rng_get_bytes(8, TOMCRYPT_RNG_RC4);

        var_dump($rand0 == $rand1); // bool(false)
        var_dump($rand1 == $rand2); // bool(true)