라라벨 BaseEncrypter

1 개요[ | ]

라라벨 BaseEncrypter
<?php

namespace Illuminate\Encryption;

use Illuminate\Contracts\Encryption\DecryptException;

abstract class BaseEncrypter
{
    protected $key;

    protected function hash($iv, $value)
    {
        return hash_hmac('sha256', $iv.$value, $this->key);
    }

    protected function getJsonPayload($payload)
    {
        $payload = json_decode(base64_decode($payload), true);
        if (! $payload || $this->invalidPayload($payload)) {
            throw new DecryptException('The payload is invalid.');
        }
        if (! $this->validMac($payload)) {
            throw new DecryptException('The MAC is invalid.');
        }
        return $payload;
    }

    protected function invalidPayload($data)
    {
        return ! is_array($data) || ! isset($data['iv']) || ! isset($data['value']) || ! isset($data['mac']);
    }

    protected function validMac(array $payload)
    {
        $bytes = random_bytes(16);
        $calcMac = hash_hmac('sha256', $this->hash($payload['iv'], $payload['value']), $bytes, true);
        return hash_equals(hash_hmac('sha256', $payload['mac'], $bytes, true), $calcMac);
    }
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}