Class: Challah::Encrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/challah/encrypter.rb

Overview

Handles all encryption, hashing and comparison necessary for tokens and passwords.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#costObject

The number of times to hash the given password.



10
11
12
# File 'lib/challah/encrypter.rb', line 10

def cost
  @cost
end

#joinerObject

Used to join multiple parameters for a given encrypt command.



15
16
17
# File 'lib/challah/encrypter.rb', line 15

def joiner
  @joiner
end

Class Method Details

.compare(*args) ⇒ Object



42
43
44
# File 'lib/challah/encrypter.rb', line 42

def self.compare(*args)
  new().compare(*args)
end

.encrypt(*args) ⇒ Object



46
47
48
# File 'lib/challah/encrypter.rb', line 46

def self.encrypt(*args)
  new().encrypt(*args)
end

.hash(*args) ⇒ Object



50
51
52
# File 'lib/challah/encrypter.rb', line 50

def self.hash(*args)
  new().hash(*args)
end

.md5(*args) ⇒ Object



54
55
56
# File 'lib/challah/encrypter.rb', line 54

def self.md5(*args)
  new().md5(*args)
end

Instance Method Details

#compare(crypted_string, plain_string) ⇒ Object

Returns true if the the bcrypted value of a is equal to b



36
37
38
39
40
# File 'lib/challah/encrypter.rb', line 36

def compare(crypted_string, plain_string)
  BCrypt::Password.new(crypted_string).is_password?(plain_string)
rescue BCrypt::Errors::InvalidHash
  false
end

#encrypt(secret) ⇒ Object



31
32
33
# File 'lib/challah/encrypter.rb', line 31

def encrypt(secret)
  BCrypt::Password.create(secret, cost: cost)
end

#hash(*tokens) ⇒ Object

Passwords and secure objects are encrypted (hashed) in a one-way technique. This way any item stored in the database can never be reversed into an actual password.



21
22
23
24
25
# File 'lib/challah/encrypter.rb', line 21

def hash(*tokens)
  result = tokens.flatten.join(joiner)
  cost.times { result = Digest::SHA512.hexdigest(result) }
  result
end

#md5(*tokens) ⇒ Object



27
28
29
# File 'lib/challah/encrypter.rb', line 27

def md5(*tokens)
  Digest::MD5.hexdigest(tokens.flatten.join(joiner))
end