PHP code example of paragonie / halite-legacy

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

    

paragonie / halite-legacy example snippets



use ParagonIE\Halite\Symmetric\{
    Crypto,
    EncryptionKey    
};
use ParagonIE\HaliteLegacy\V3\Symmetric\{
    Crypto as LegacyCrypto,
    EncryptionKey as LegacyKey
};
use ParagonIE\HaliteLegacy\V3\HiddenString as LegacyHiddenString;
use ParagonIE\HiddenString\HiddenString;

/**
 * @var EncryptionKey $encKey
 * @var LegacyKey $oldKey
 * @var string $ciphertext
 * @var LegacyHiddenString $plaintext
 */
$plaintext = LegacyCrypto::decrypt($ciphertext, $oldKey);
$storeMe = Crypto::encrypt(
    new HiddenString($plaintext->getString()),
    $encKey
);


use ParagonIE\HaliteLegacy\VersionHelper;
use ParagonIE\Halite\Halite\Symmetric\Crypto as SymmetricLatest;
use ParagonIE\Halite\HaliteLegacy\V4\Symmetric\Crypto as SymmetricV4;

// Version will be an integer, or it will throw
$inferredVersion = VersionHelper::inferVersionFromCiphertext($ciphertext);

if ($inferredVersion === VersionHelper::LATEST) {
    // If it's the latest, use Halite:
    $plaintext = SymmetricLatest::decrypt($ciphertext, $secretKey);
} elseif ($inferredVersion === 4) {
    // If it's an explicitly-supported legacy version, decrypt it
    $plaintext = SymmetricV4::decrypt($ciphertext, $secretKey);
    
    // Recommended: Re-encrypt with the latest version:
    $reEncrypted = SymmetricLatest::encrypt($plaintext, $secretKey);
} else {
    throw new Exception('We never supported v3 ciphertexts. Abort!');
}