mardi 29 janvier 2019

How can I store random_bytes in a database in PHP?

Inside of my migration I have a string column named encryption_key which looks like this:

$table->string('encryption_key')->unique();

I have a Controller that uses a trait to generate the encryption commands.

public function create()
{
    $this->header->insert([
        'encryption_key' => $this->issueKey()
    ]);

    $this->participants->insert([
        'chat_id' => DB::getPdo()->lastInsertId(),
        'user_id' => Auth::id()
    ]);

    return response(['status' => true, 'chat_id' => DB::getPdo()->lastInsertId()], 200)
        ->header('Content-Type', 'application/json');
}

The trait looks like this so far

trait LiveChat
{
    protected function issueKey()
    {
        return random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
    }
}

However, upon testing this I receive this error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xFFX\x8Af\x1F$...' for column 'encryption_key' at row 1 (SQL: insert into chat_headers (encryption_key) values (ÿXŠf\x1F$¨ì™ÒÂø¢Ú!£”…¸ÈÍØ7ÿDå\x00Œ¿3ê))

How can I store this key to use for future reference inside my DB?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire