"라라벨 Encrypter"의 두 판 사이의 차이

(새 문서: ==개요== ;라라벨 Encrypter <source lang='php'> <?php namespace Illuminate\Encryption; use RuntimeException; use Illuminate\Contracts\Encryption\DecryptException; use Illuminat...)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 4개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
;라라벨 Encrypter
;라라벨 Encrypter


<source lang='php'>
==Illuminate\Contracts\Encryption==
<syntaxhighlight lang='php'>
<?php
 
namespace Illuminate\Contracts\Encryption;
 
interface Encrypter
{
    public function encrypt($value);
    public function decrypt($payload);
}
</syntaxhighlight>
 
==Illuminate\Encryption==
<syntaxhighlight lang='php'>
<?php
<?php


64번째 줄: 77번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==같이 보기==
* [[라라벨 BaseEncrypter]]
* [[라라벨 EncryptCookies]]


[[분류: Illuminate\Contracts\Encryption]]
[[분류: Illuminate\Encryption]]
[[분류: Illuminate\Encryption]]

2020년 11월 2일 (월) 02:57 기준 최신판

라라벨 Encrypter

1 Illuminate\Contracts\Encryption[ | ]

<?php

namespace Illuminate\Contracts\Encryption;

interface Encrypter
{
    public function encrypt($value);
    public function decrypt($payload);
}

2 Illuminate\Encryption[ | ]

<?php

namespace Illuminate\Encryption;

use RuntimeException;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;

class Encrypter extends BaseEncrypter implements EncrypterContract
{
    protected $cipher;

    public function __construct($key, $cipher = 'AES-128-CBC')
    {
        $key = (string) $key;
        if (static::supported($key, $cipher)) {
            $this->key = $key;
            $this->cipher = $cipher;
        } else {
            throw new RuntimeException('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');
        }
    }

    public static function supported($key, $cipher)
    {
        $length = mb_strlen($key, '8bit');
        return ($cipher === 'AES-128-CBC' && $length === 16) || ($cipher === 'AES-256-CBC' && $length === 32);
    }

    public function encrypt($value)
    {
        $iv = random_bytes($this->getIvSize());
        $value = \openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv);
        if ($value === false) {
            throw new EncryptException('Could not encrypt the data.');
        }
        $mac = $this->hash($iv = base64_encode($iv), $value);
        $json = json_encode(compact('iv', 'value', 'mac'));
        if (! is_string($json)) {
            throw new EncryptException('Could not encrypt the data.');
        }
        return base64_encode($json);
    }

    public function decrypt($payload)
    {
        $payload = $this->getJsonPayload($payload);
        $iv = base64_decode($payload['iv']);
        $decrypted = \openssl_decrypt($payload['value'], $this->cipher, $this->key, 0, $iv);
        if ($decrypted === false) {
            throw new DecryptException('Could not decrypt the data.');
        }
        return unserialize($decrypted);
    }

    protected function getIvSize()
    {
        return 16;
    }
}

3 같이 보기[ | ]

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