Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/0333a7c54e11efa4c71fa623c39643c6 to your computer and use it in GitHub Desktop.
Save trycf/0333a7c54e11efa4c71fa623c39643c6 to your computer and use it in GitHub Desktop.
TryCF Gist
<!--- Function to generate a secure IV for AES encryption --->
<cffunction name="generateIV" returntype="string">
<cfset var ivBytes = createObject("java", "java.security.SecureRandom").init().generateSeed(16)>
<cfreturn binaryEncode(ivBytes, "base64")>
</cffunction>
<!--- Function to encrypt the password for PHP compatibility --->
<cffunction name="encryptPasswordForPHP" returntype="string">
<cfargument name="password" type="string" required="yes">
<cfset var key = "AES"> <!--- Securely generated and stored --->
<cfset var iv = generateIV()> <!--- Generate a correct 16-byte IV --->
<cfreturn encrypt(arguments.password, key, "AES/CBC/PKCS5Padding", "Base64", iv)>
</cffunction>
<!--- Function to hash the password for ColdFusion usage --->
<cffunction name="hashPasswordForCF" returntype="string">
<cfargument name="password" type="string" required="yes">
<cfreturn hash(arguments.password, "SHA-512", "UTF-8")>
</cffunction>
<!--- Function to store the passwords in different tables --->
<cffunction name="storePasswords" returntype="void">
<cfargument name="username" type="string" required="yes">
<cfargument name="password" type="string" required="yes">
<cfset var encryptedPassword = encryptPasswordForPHP(arguments.password)>
<cfset var hashedPassword = hashPasswordForCF(arguments.password)>
</cffunction>
<cfset username = "newUser">
<cfset password = "examplePassword123">
<!--- Store the passwords --->
<cfset storePasswords(username, password)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment