63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* PII(개인정보) 필드 암호화/복호화 헬퍼.
|
|
* encryption.key 가 .env에 설정된 경우에만 동작. 키가 없으면 평문 유지(기존 데이터 호환).
|
|
*
|
|
* 저장 형식: 암호화된 값은 "ENC:" + base64(암호문) 으로 저장. "ENC:" 없으면 평문(기존)으로 간주.
|
|
*/
|
|
if (! function_exists('pii_encrypt')) {
|
|
function pii_encrypt(?string $value): string
|
|
{
|
|
if ($value === null || $value === '') {
|
|
return '';
|
|
}
|
|
try {
|
|
$config = config('Encryption');
|
|
if ($config->key === '') {
|
|
return $value;
|
|
}
|
|
$encrypter = service('encrypter');
|
|
$encrypted = $encrypter->encrypt($value);
|
|
|
|
return 'ENC:' . base64_encode($encrypted);
|
|
} catch (Throwable) {
|
|
return $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('pii_decrypt')) {
|
|
function pii_decrypt(?string $value): string
|
|
{
|
|
if ($value === null || $value === '') {
|
|
return '';
|
|
}
|
|
if (strpos($value, 'ENC:') !== 0) {
|
|
return $value;
|
|
}
|
|
try {
|
|
$config = config('Encryption');
|
|
if ($config->key === '') {
|
|
return $value;
|
|
}
|
|
$encrypter = service('encrypter');
|
|
$raw = base64_decode(substr($value, 4), true);
|
|
if ($raw === false) {
|
|
return $value;
|
|
}
|
|
|
|
return $encrypter->decrypt($raw);
|
|
} catch (Throwable) {
|
|
return $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** 암호화 대상 개인정보 필드 (member 테이블) */
|
|
if (! defined('PII_ENCRYPTED_FIELDS')) {
|
|
define('PII_ENCRYPTED_FIELDS', ['mb_phone', 'mb_email']);
|
|
}
|