Module: Authify::Core::SecureHashing

Defined in:
lib/authify/core/secure_hashing.rb

Overview

Some handy methods for SHA512 and Base64

Instance Method Summary collapse

Instance Method Details

#compare_salted_sha512(string, hashed_string) ⇒ Object



30
31
32
33
# File 'lib/authify/core/secure_hashing.rb', line 30

def compare_salted_sha512(string, hashed_string)
  salt = hashed_string[-16..-1]
  salted_sha512(string, salt) == hashed_string
end

#decode64String

Decode a Base64 encoded string

Returns:

  • (String)


13
14
15
# File 'lib/authify/core/secure_hashing.rb', line 13

def decode64
  Base64.decode64(string)
end

#peppered_sha512(seed, pepper = rand(9**99).to_s) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/authify/core/secure_hashing.rb', line 35

def peppered_sha512(seed, pepper = rand(9**99).to_s)
  # Run our string through 10 rounds of SHA512, each time "peppering" it with random data
  10.times do
    seed = sha512(seed + pepper)
  end
  seed
end

#salted_sha512(string, salt = nil) ⇒ Object



25
26
27
28
# File 'lib/authify/core/secure_hashing.rb', line 25

def salted_sha512(string, salt = nil)
  salt ||= rand(9**99).to_s[0...16]
  sha512(string + salt) + salt
end

#sha512(string) ⇒ Object



21
22
23
# File 'lib/authify/core/secure_hashing.rb', line 21

def sha512(string)
  to_hex(Digest::SHA512.digest(string))
end

#to_64(string) ⇒ String

Convert a string to a Base64 encoded version of itself

Returns:

  • (String)


7
8
9
# File 'lib/authify/core/secure_hashing.rb', line 7

def to_64(string)
  Base64.encode64(string).chomp.delete("\n")
end

#to_hex(data) ⇒ Object



17
18
19
# File 'lib/authify/core/secure_hashing.rb', line 17

def to_hex(data)
  data.unpack('H*').first.chomp.delete("\n")
end