Created
April 24, 2017 10:42
-
-
Save gskema/0e4025bd1be54b13858dff90589c0d3a to your computer and use it in GitHub Desktop.
Obscure arbitrary security-sensitive string in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Obscures sensitive string (like a security token), | |
* but preserves minimal information about letters, digits, spaces and unknown characters. | |
* | |
* @param string $string | |
* | |
* @return string | |
*/ | |
protected function obscure(string $string) | |
{ | |
// Input: '_ ?$$EFdasdasda das56 14a6512341 2341 )_% UIML: ' | |
// Output: '?_???aaaaaaaaaa_aaa00_00a0000000_0000_???_aaaa?_' | |
$obscured = preg_replace('/[^a-z\s\d]/i', '?', $string); | |
$obscured = preg_replace('/[a-z]/i', 'a', $obscured); | |
$obscured = preg_replace('/\d/', '0', $obscured); | |
$obscured = preg_replace('/\s/', '_', $obscured); | |
return $obscured; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment