paragonie / ciphersweet

Fast, searchable field-level encryption for PHP projects
https://ciphersweet.paragonie.com
Other
437 stars 32 forks source link

ParagonIE\CipherSweet\KeyProvider\StringProvider::__construct(): Argument #1 ($rawKey) must be of type string, null given #75

Closed Astelll closed 2 years ago

Astelll commented 2 years ago

I'm not sure that I'm asking in the right place, but I want to understand and solve the problem, so I'll write it here anyway.

I installed to my project developed with Laravel framework this library https://github.com/spatie/laravel-ciphersweet, which implements Ciphersweet use within Laravel, and tried to encrypt data in my DB with this command php artisan ciphersweet:encrypt "App\Models\User" da61321c8814fe47f6f647f26330bc34f88fd8c32c1df25af4f0b3a30dff00e9 , but got error ParagonIE\CipherSweet\KeyProvider\StringProvider::__construct(): Argument #1 ($rawKey) must be of type string, null given, called in /var/www/fastuser/data/www/api.dev.clanhall.net/vendor/spatie/laravel-ciphersweet/src/LaravelCipherSweetServiceProvider.php on line 52 on line 48-55 I see this code:

48 protected function buildKeyProvider(BackendInterface $backend): KeyProviderInterface
49    {
50        return match (config('ciphersweet.provider')) {
51            'file' => new FileProvider(config('ciphersweet.providers.file.path')),
52            'string' => new StringProvider(config('ciphersweet.providers.string.key')),
53            default => new RandomProvider($backend),
54        };
55    }

and then if I go to StringProvider I see

public function __construct(string $rawKey = '')
    {
        if (Binary::safeStrlen($rawKey) === 64) {
            $this->rootSymmetricKey = Hex::decode($rawKey);
        } elseif (Binary::safeStrlen($rawKey) === 44) {
            $this->rootSymmetricKey = Base64UrlSafe::decode($rawKey);
        } elseif (Binary::safeStrlen($rawKey) === 32) {
            $this->rootSymmetricKey = $rawKey;
        } else {
            throw new CryptoOperationException('Invalid key size');
        }
    }

I tried to google why PHP see empty string as null, but without success, and I'm not sure that this is the problem.

What am I doing wrong and how can I try to fix it? As I see, no one has ansered to my question in that thread, so maybe only me got this error, and maybe I don't know some basics.

I would appreciate your help.

paragonie-security commented 2 years ago

Somewhere, it's passing null to the constructor of StringProvider. This usually happens when something was expected to be defined, but it isn't.

StringProvider is strictly typed. This is probably a configuration issue with the laravel-ciphersweet package.

Astelll commented 2 years ago

Somewhere, it's passing null to the constructor of StringProvider. This usually happens when something was expected to be defined, but it isn't.

StringProvider is strictly typed. This is probably a configuration issue with the laravel-ciphersweet package.

Thank you for your answer Can I make a screenshot of your anwser and use it in in that thread?

paragonie-security commented 2 years ago

Sure

sandeep0108 commented 11 months ago

If you should written step of generating key (2. Generating the encrypting key) before publishing configuration line, So issue will not come like this

ParagonIE\CipherSweet\KeyProvider\StringProvider::__construct(): Argument #1 ($rawKey) must be of type string, null given, called in /var/www/html/riview_app/vendor/spatie/laravel-ciphersweet/src/LaravelCipherSweetServiceProvider.php on line 74