<?php
namespace App\Support\LegacyCrypto;

final class CiPassword
{
    private const PASS_KEY = 'WmhEbHNGbmFAKTE3MSEwJmNvSW5Sb29NKw==';
    private const PASS_SET = 0;

    private const METHOD = 'aes-256-ctr';
    private const IV = '567-av23-901-345';

    private static function opt(): int
    {
        // CI3에서 define('OPENSSL_ZERO_PADDING', 2) 하던 것과 동일하게 맞춤
        return defined('OPENSSL_ZERO_PADDING') ? OPENSSL_ZERO_PADDING : 2;
    }

    /** @return array{0:string,1:string,2:string} */
    public static function makeAll(string $plain): array
    {
        $key = self::PASS_KEY;
        $iv = self::IV;
        $opt = self::opt();

        $enc0 = hash_hmac('ripemd320', $plain, $key);
        $enc0 = hash('sha512', $enc0);
        $enc0 = openssl_encrypt($enc0, self::METHOD, $key, $opt, $iv);

        $enc1 = hash_hmac('ripemd320', $plain, $key);
        $enc1 = openssl_encrypt($enc1, self::METHOD, $key, $opt, $iv);
        $enc1 = hash('sha512', $enc1);

        $enc2 = openssl_encrypt($plain, self::METHOD, $key, $opt, $iv);
        $enc2 = hash('sha512', $enc2);
        $enc2 = hash_hmac('ripemd320', $enc2, $key);

        return [$enc0, $enc1, $enc2];
    }

    public static function make(string $plain, int $set = self::PASS_SET): string
    {
        $all = self::makeAll($plain);
        return $all[$set] ?? $all[0];
    }

    public static function makePass2(string $pin2): string
    {
        return hash('sha512', $pin2);
    }
}
