lib/salty.rb in salty-0.0.6 vs lib/salty.rb in salty-0.0.7

- old
+ new

@@ -1,38 +1,46 @@ require 'digest/sha1' -SALT_LENGTH=30 -ALPHA = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a +module Salty -def hash_fn(str) - sha512 = Digest::SHA2.new(512) - sha512.hexdigest(str) -end + SALT_LENGTH=30 + ALPHA = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a -def generate_salt - (1..SALT_LENGTH).map{ALPHA.sample}.join -end + def Salty.hash_fn(str) + sha512 = Digest::SHA2.new(512) + sha512.hexdigest(str) + end -def salted_hash(str,salt) - res = str - 100.times do - res = hash_fn(res+salt) + def Salty.generate_salt + (1..SALT_LENGTH).map{ALPHA.sample}.join end - res -end -def salty(str) - salt = generate_salt + def Salty.salted_hash(str,salt) + res = str + 100.times do + res = hash_fn(res+salt) + end + res + end - res = salted_hash(str,salt) + def Salty.hash(str) + salt = generate_salt - n = str.length - res[0...n] + salt + res[n..-1] -end + res = salted_hash(str,salt) -def salty_eq(unhashed,hashed) - n = unhashed.length - salt = hashed[n,SALT_LENGTH] - myhashed = hashed[0...n] + hashed[n+SALT_LENGTH..-1] + n = str.length + res[0...n] + salt + res[n..-1] + end - myhashed == salted_hash(unhashed,salt) + def Salty.check(unhashed,hashed) + begin + n = unhashed.length + salt = hashed[n,SALT_LENGTH] + myhashed = hashed[0...n] + hashed[n+SALT_LENGTH..-1] + + return myhashed == salted_hash(unhashed,salt) + rescue + return false + end + end + end