function encrypt(PersonalData $data, CryptographicDetails $crypto): EncryptedPersonalData {

        if ( ! $crypto->encryption() == 'libsodium') {
            throw new CryptographicDetailsNotCompatibleWithEncryption("{$crypto->encryption()} received, expected 'libsodium'");
        }

        $dataString = $data->toString();
        $secretKey  = $crypto->key('secretKey');
        $nonce      = $crypto->key('nonce');

        $encrypted = sodium_crypto_secretbox($dataString, $nonce, $secretKey);

        return EncryptedPersonalData::fromString($encrypted);
    }
function encrypt(PersonalData $data, CryptographicDetails $crypto): EncryptedPersonalData {

        if ( ! $crypto->encryption() == 'libsodium') {
            throw new CryptographicDetailsNotCompatibleWithEncryption("{$crypto->encryption()} received, expected 'libsodium'");
        }

        $dataString = $data->toString();
        $secretKey  = $crypto->key('secretKey');
        $nonce      = $crypto->key('nonce');

        $encrypted = sodium_crypto_secretbox($dataString, $nonce, $secretKey);

        return EncryptedPersonalData::fromString($encrypted);
    }
function encrypt ( PersonalData $ data , CryptographicDetails $ crypto ) : EncryptedPersonalData { if ( ! $ crypto -> encryption ( ) == 'libsodium' ) { throw new CryptographicDetailsNotCompatibleWithEncryption ( "{$crypto->encryption()} received, expected 'libsodium'" ) ; } $ dataString = $ data -> toString ( ) ; $ secretKey = $ crypto -> key ( 'secretKey' ) ; $ nonce = $ crypto -> key ( 'nonce' ) ; $ encrypted = sodium_crypto_secretbox ( $ dataString , $ nonce , $ secretKey ) ; return EncryptedPersonalData :: fromString ( $ encrypted ) ; }
encrypt personal data and return an encrypted form

@param PersonalData $data
@param CryptographicDetails $crypto
@throws CryptographicDetailsDoNotContainKey
@throws CryptographicDetailsNotCompatibleWithEncryption
@return EncryptedPersonalData
encrypt personal data and return an encrypted form