security.rb 685 B

123456789101112131415161718192021222324252627
  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 Woe.crypt(pass, 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 = Woe.crypt(trypass, salt)
  20. p "cc", salt, tryhash, trypass
  21. return tryhash == hash
  22. end