· 6 years ago · Jul 10, 2019, 06:34 AM
1<?php
2namespace common\utils;
3
4use common\utils\Timeanddate;
5
6class EncryptionDB {
7
8 public static function encryptor($action, $string) {
9 $output = false;
10
11 $encrypt_method = "AES-256-CBC";
12 //pls set your unique hashing key
13
14 $currentTime = Timeanddate::getCurrentDate();
15 $secret_key = 'NgoprekToko';
16 $secret_iv = 'ngararoprektoko12345'.$currentTime;
17
18
19 // hash
20 $key = hash('sha256', $secret_key);
21
22 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
23 $iv = substr(hash('sha256', $secret_iv), 0, 16);
24
25 //do the encyption given text/string/number
26 if( $action == 'encrypt' ) {
27 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
28 $output = base64_encode($output);
29 }
30 else if( $action == 'decrypt' ){
31 //decrypt the given text/string/number
32 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
33 }
34
35 return $output;
36 }
37
38}
39?>