PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/security/cryptLib.cfc

https://bitbucket.org/farcry/core
ColdFusion CFScript | 95 lines | 67 code | 28 blank | 0 comment | 0 complexity | 55ac491c68f60fd1eaff24f699f69280 MD5 | raw file
  1. <cfcomponent hint="I encode and verify password hashes. I support multiple hashing algorithms to make upgrades easier." output="false">
  2. <cffunction access="public" name="init" returntype="cryptLib" output="false" hint="Constructor">
  3. <cfset var comp = "" />
  4. <cfset var oHash = "" />
  5. <cfset variables.stHashes = structNew() />
  6. <cfloop list="#application.factory.oUtils.getComponents('security')#" index="comp">
  7. <cfif not listFindNoCase("PasswordHash",comp) and application.factory.oUtils.extends(application.factory.oUtils.getPath("security",comp),"farcry.core.packages.security.PasswordHash")>
  8. <cfset oHash = createobject("component",application.factory.oUtils.getPath("security",comp)).init() />
  9. <cfset variables.stHashes[oHash.alias] = oHash />
  10. </cfif>
  11. </cfloop>
  12. <cfset variables.lOrderedHashes = ArrayToList(structsort(variables.stHashes,"numeric","asc","seq")) />
  13. <cfreturn this />
  14. </cffunction>
  15. <cffunction name="encodePassword" access="public" returntype="string" output="false" hint="Convert a clear password to its encoded value">
  16. <cfargument name="password" type="string" required="true" hint="Input password" />
  17. <cfargument name="hashName" type="string" default="#getDefaultHashName()#" hint="Alias of hash algorithm to encode password" />
  18. <cfreturn getHashComponent(arguments.hashName).encode(arguments.password) />
  19. </cffunction>
  20. <cffunction name="passwordMatchesHash" access="public" returntype="boolean" output="false" hint="Check if a clear password matches an encoded hash">
  21. <cfargument name="password" type="string" hint="Input password" required="true" />
  22. <cfargument name="hashedPassword" type="string" required="true" hint="Hashed password" />
  23. <cfreturn findHash(hashedPassword=arguments.hashedPassword).passwordMatch(password=arguments.password,hashedPassword=arguments.hashedPassword) />
  24. </cffunction>
  25. <cffunction name="hashedPasswordIsStale" access="public" returntype="boolean" output="false" hint="Is the hashed password stale (i.e. needs to be regenerated)?">
  26. <cfargument name="hashedPassword" type="string" required="true" hint="Hashed password" />
  27. <cfargument name="password" type="string" required="true" hint="Source password" />
  28. <cfargument name="hashName" type="string" default="#getDefaultHashName()#" hint="Alias of hash algorithm that hashed password should be using" />
  29. <cfset var oHash = getHashComponent(arguments.hashName) />
  30. <cfreturn not oHash.matchesHashFormat(arguments.hashedPassword) or not oHash.passwordMatch(password=arguments.password,hashedPassword=arguments.hashedPassword,bCheckHashStrength=true) />
  31. </cffunction>
  32. <cffunction name="getDefaultHashName" access="public" returntype="PasswordHash" output="false" hint="Return the alias of the default algorithm used to encoded passwords">
  33. <cfreturn ListFirst(variables.lOrderedHashes) />
  34. </cffunction>
  35. <cffunction name="isHashAlgorithmSupported" access="public" returntype="boolean" output="false" hint="Is this hash algorithm supported?">
  36. <cfargument name="hashName" type="string" required="true" hint="Alias of hash algorithm" />
  37. <cfreturn structKeyExists(variables.stHashes,arguments.hashName) and variables.stHashes[arguments.hashName].isAvailable() />
  38. </cffunction>
  39. <cffunction name="getHashComponent" access="public" returntype="PasswordHash" output="false" hint="Return a hash algorithm component">
  40. <cfargument name="hashName" type="string" default="#getDefaultHashName()#" hint="Alias of hash algorithm" />
  41. <cfreturn variables.stHashes[arguments.hashName] />
  42. </cffunction>
  43. <cffunction name="getOrderedHashArray" access="public" returntype="array" output="false" hint="Return an array of supported PasswordHash components in order of priority">
  44. <cfset var hashKey = "" />
  45. <cfset var oHash = "" />
  46. <cfset var aHashes = arrayNew(1) />
  47. <cfloop list="#variables.lOrderedHashes#" index="hashKey">
  48. <cfset oHash = variables.stHashes[hashKey] />
  49. <cfif oHash.isAvailable()>
  50. <cfset ArrayAppend(aHashes,oHash) />
  51. </cfif>
  52. </cfloop>
  53. <cfreturn aHashes />
  54. </cffunction>
  55. <cffunction name="findHash" access="public" output="false" returntype="PasswordHash" hint="Returns a PasswordHash component that can verify this hashed password">
  56. <cfargument name="hashedPassword" type="string" hint="Hashed password string" required="true" />
  57. <cfset var hashKey = "" />
  58. <cfset var oHash = "" />
  59. <cfloop list="#variables.lOrderedHashes#" index="hashKey">
  60. <cfset oHash = variables.stHashes[hashKey] />
  61. <cfif oHash.isAvailable() and oHash.matchesHashFormat(arguments.hashedPassword)>
  62. <cfreturn oHash />
  63. </cfif>
  64. </cfloop>
  65. <cfthrow message="Password hash does not match any available hash formats" detail="Hashed password did not match any available PasswordHash objects. Did you override or delete NullHash.cfc?" />
  66. </cffunction>
  67. </cfcomponent>