Last active
August 10, 2018 00:40
-
-
Save JamoCA/7fcaf312b72cf5c66acc54e4432eb3c0 to your computer and use it in GitHub Desktop.
maskCC: ColdFusion UDF to identify credit cards in a text string and mask them. Sanitize plain text form content before blindly sending via email.
This file contains 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
<cfscript> | |
/* maskCC - identify credit cards in a text string and mask them. | |
by James Moberg 8/9/2018 | |
https://gamesover2600.tumblr.com/post/176823552659/mask-credit-card-numbers-using-coldfusion | |
Gist: https://gist.github.com/JamoCA/7fcaf312b72cf5c66acc54e4432eb3c0 | |
Also posted on StackOverflow for discussion: https://stackoverflow.com/a/51777454/693068 | |
*/ | |
function maskCC(textFragment){ | |
var response = Javacast("string", arguments.textFragment); | |
var temp = {}; | |
temp.TestCCs = reMatch("\b(?:\d[ -]*?){13,16}\b", response); | |
if (arrayLen(temp.TestCCs)){ | |
for (temp.thisCC in temp.TestCCs) { | |
temp.cleanNum = reReplace(temp.thisCC,"[^[:digit:]]","","all"); | |
if( isValid("creditcard", Temp.cleanNum )){ | |
response = Replace(response, temp.thisCC, "************" & Right(Temp.cleanNum,4), "all"); | |
} | |
} | |
} | |
return response; | |
} | |
</cfscript> | |
<CFOUTPUT> | |
<pre> | |
#maskCC('4444 3333 2222 1111')# (with spaces) | |
#maskCC('4444-3333-2222-1111')# (with dashes) | |
#maskCC('4444333322221111')# (no separators) | |
#maskCC('1234567890123456')# (not a CC) | |
GENERIC TEXT FROM TEXTAREA FORM FIELD: | |
#maskCC('Here is my unsecure CCNum: 4444333322221111. | |
My transaction ID IS: 4567123498765283. Please refund.')# | |
</pre> | |
</CFOUTPUT> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment