PHP code example of madmatt / silverstripe-encrypt-at-rest
1. Go to this page and download the library: Download madmatt/silverstripe-encrypt-at-rest 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/ */
madmatt / silverstripe-encrypt-at-rest example snippets
use Madmatt\EncryptAtRest\FieldType\EncryptedVarchar;
class SecureDataObject extends DataObject {
private static $db = [
'NormalText' => 'Varchar'
'SecureText' => EncryptedVarchar::class
];
}
// Via DataObject::get()
$obj = SecureDataObject::get()->first()->SecureText; // Returns the decrypted string from the field
// Getting the DB field
$obj = SecureDataObject::get()->first();
$field = $obj->dbObject('SecureText'); // Returns an EncryptedVarchar object
$uppercase = $field->UpperCase(); // Method on DBString, returns a string
use Madmatt\EncryptAtRest\AtRestCryptoService;
use SilverStripe\Assets\File;
use SilverStripe\Core\Injector\Injector;
$text = 'This is an unencrypted string!';
/** @var AtRestCryptoService $service */
$service = Injector::inst()->get(AtRestCryptoService::class);
$encryptedText = $service->encrypt($text); // Returns encrypted string starting with `def`
$unencryptedText = $service->decrypt($encryptedText); // Returns 'This is an unencrypted string!'
$file = File::get()->byID(1); // Presume this is a file that contains the text string above
// This will encrypt the file contents, delete the original file from the filesystem and create a new file at the same path with .enc appended to the filename
$encryptedFile = $service->encryptFile($file);
// This will decrypt the file contents, delete the encrypted file from the filesystem and create a new file at the same path with .enc stripped from the filename
$decryptedFile = $service->decryptFile($encryptedFile);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.