· 5 years ago · Jan 02, 2020, 07:38 AM
1<?php
2
3
4	function h_encrypt(string $data): array
5	{
6
7		$secretKey = env('DATA_SECRET_KEY');
8		$secretIv = env('DATA_SECRET_IV');
9		$salt = env('DATA_SECRET_SALT');
10		$encryptMethod = 'AES-256-CBC';
11		$key = hash_hmac('sha256', $secretKey.$salt, $secretKey);
12		$iv = substr(hash_hmac('sha256', $secretIv.$salt, $secretKey), 0, 16);
13
14		$output = base64_encode(
15			openssl_encrypt($data, $encryptMethod, $key, 0, $iv)
16		);
17
18		return [
19			'success' => true,
20			'data' => $output
21		];
22	}
23
24
25	function h_decrypt(string $data): array
26	{
27
28		$secretKey = env('DATA_SECRET_KEY');
29		$secretIv = env('DATA_SECRET_IV');
30		$salt = env('DATA_SECRET_SALT');
31		$encryptMethod = 'AES-256-CBC';
32		$key = hash_hmac('sha256', $secretKey.$salt, $secretKey);
33		$iv = substr(hash_hmac('sha256', $secretIv.$salt, $secretKey), 0, 16);
34
35		$output = openssl_decrypt(
36			base64_decode($data), $encryptMethod, $key, 0, $iv
37		);
38
39		return [
40			'success' => true,
41			'data' => $output
42		];
43	}