security.cr 653 B

1234567891011121314151617181920212223242526
  1. #
  2. # Woe security related helper functions.
  3. #
  4. CRYPT_MAKE_SALT_AID = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
  5. # Generates salt for use by crypt.
  6. def crypt_make_salt
  7. c1 = CRYPT_MAKE_SALT_AID[rand(CRYPT_MAKE_SALT_AID.length)]
  8. c2 = CRYPT_MAKE_SALT_AID[rand(CRYPT_MAKE_SALT_AID.length)]
  9. return c1 + c2
  10. end
  11. # Crypt with salt generation.
  12. def crypt(pass, salt = nil)
  13. salt = crypt_make_salt unless salt
  14. return pass.to_s.crypt(salt)
  15. end
  16. # Challenge crypt password trypass against the hash hash
  17. def crypt_challenge?(trypass, hash)
  18. salt = hash[0, 2]
  19. tryhash = trypass.to_s.crypt(salt)
  20. return tryhash == hash
  21. end