77|
88| These are core functions necessary to run the nano-framework:
99|
10- | 1. url_path() - retrieves the URL path substring separated by '/'
11- | 2. route_rpc() - JSON-RPC v2.0 compatibility layer
12- | 3. route_auto() - automatic routing of URL path to Class and method
13- | 4. route_class() - routes URL path request to Controllers
14- | 5. view() - passes data and renders the View
15- | 6. pdo_conn() - PHP Data Objects (PDO) database connection
16- | 7. api_response() - handles API response
17- | 8. api_call() - handles API call
18- | 9. firewall() - web application firewall
19- | 10. force_ssl() - force application to use SSL
20- | 11. esc() - uses htmlspecialchars() to prevent XSS
21- | 12. csrf_token() - uses sessions to create per request CSRF token
10+ | 1. url_path() - retrieves the URL path substring separated by '/'
11+ | 2. route_rpc() - JSON-RPC v2.0 compatibility layer
12+ | 3. route_auto() - automatic routing of URL path to Class and method
13+ | 4. route_class() - routes URL path request to Controllers
14+ | 5. view() - passes data and renders the View
15+ | 6. pdo_conn() - PHP Data Objects (PDO) database connection
16+ | 7. api_response() - handles API response
17+ | 8. api_call() - handles API call
18+ | 9. firewall() - web application firewall
19+ | 10. force_ssl() - force application to use SSL
20+ | 11. esc() - uses htmlspecialchars() to prevent XSS
21+ | 12. csrf_token() - uses sessions to create per request CSRF token
22+ | 13. encrypt() - encrypt data using AES-CBC-HMAC
23+ | 14. decrypt() - decrypt data using AES-CBC-HMAC
2224|
2325*/
2426
@@ -355,4 +357,53 @@ function csrf_token()
355357
356358 }
357359
360+ }
361+
362+ /**
363+ * Encrypt data using AES-CBC-HMAC
364+ *
365+ * @param string $plaintext - Plaintext to be encrypted
366+ */
367+
368+ function encrypt ($ plaintext )
369+ {
370+
371+ $ cipher = CIPHER_METHOD ;
372+ $ key = hash ('sha256 ' , PASS_PHRASE . md5 (PASS_PHRASE ));
373+ $ key_hmac = hash ('sha256 ' , md5 (PASS_PHRASE ));
374+ $ iv_len = openssl_cipher_iv_length ($ cipher );
375+ $ iv = random_bytes ($ iv_len );
376+
377+ $ ciphertext = openssl_encrypt ($ plaintext , $ cipher , $ key , 0 , $ iv );
378+ $ hash = hash_hmac ('sha256 ' , $ ciphertext , $ key_hmac );
379+
380+ return base64_encode ($ ciphertext . ':: ' . $ hash . ':: ' . $ iv );
381+
382+ }
383+
384+ /**
385+ * Decrypt data using AES-CBC-HMAC
386+ *
387+ * @param string $encypted - base64_encoded ciphertext, hash and iv
388+ */
389+
390+ function decrypt ($ encrypted )
391+ {
392+
393+ if ( ! isset ($ encrypted ) || empty ($ encrypted ) ) { return '' ; }
394+
395+ $ cipher = CIPHER_METHOD ;
396+ $ key = hash ('sha256 ' , PASS_PHRASE . md5 (PASS_PHRASE ));
397+ $ key_hmac = hash ('sha256 ' , md5 (PASS_PHRASE ));
398+
399+ list ($ ciphertext , $ hash , $ iv ) = explode (':: ' , base64_decode ($ encrypted ));
400+ $ digest = hash_hmac ('sha256 ' , $ ciphertext , $ key_hmac );
401+
402+ if (hash_equals ($ hash , $ digest )) {
403+ return openssl_decrypt ($ ciphertext , $ cipher , $ key , 0 , $ iv );
404+ }
405+ else {
406+ return 'Please verify authenticity of ciphertext. ' ;
407+ }
408+
358409}
0 commit comments