Sha256: d784dcf1ef02cba31468c3caae142babc03b9c6a203fa359e3dc3ffac631137d

Contents?: true

Size: 650 Bytes

Versions: 1

Compression:

Stored size: 650 Bytes

Contents

require 'digest/sha1'

SALT_LENGTH=30
ALPHA = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a

def hash_fn(str)
  sha512 = Digest::SHA2.new(512)
  sha512.hexdigest(str)
end

def generate_salt
  (1..SALT_LENGTH).map{ALPHA.sample}.join
end

def salted_hash(str,salt)
  res = str
  100.times do
    res = hash_fn(res+salt)
  end
  res
end

def salty(str)
  salt = generate_salt

  res = salted_hash(str,salt)

  n = str.length
  res[0...n] + salt + res[n..-1]
end

def salty_eq(unhashed,hashed)
  n = unhashed.length
  salt = hashed[n,SALT_LENGTH]
  myhashed = hashed[0...n] + hashed[n+SALT_LENGTH..-1]

  myhashed == salted_hash(unhashed,salt)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
salty-0.0.6 lib/salty.rb