Module: ChupakabraTools::Security

Defined in:
lib/chupakabra_tools/security.rb

Overview

Module Responsible for security features

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_secret(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chupakabra_tools/security.rb', line 16

def self.generate_secret(options={})
    options ||= {}
    options =
        {
            big: true,
            small: true,
            digits: true,
            specials: false,
            length: 8
        }.merge(options)

    chars = ""

    if options[:small] && options[:small] == true
        chars += ("a".."z").to_a.join
    end

    if options[:big] && options[:big] == true
        chars += ("A".."Z").to_a.join
    end

    if options[:digits] && options[:digits] == true
        chars += ("0".."9").to_a.join
    end

    if options[:specials] && options[:specials] == true
        chars += "!@#$\%^&*()_+<>/?"
    end

    if chars.blank?
        chars = ("a".."z").to_a.join
    end


    len = 8
    begin
        if options[:length] && options[:length].to_i > 5
            len = options[:length].to_i
        end
    rescue
    end

    new_pass = ""
    1.upto(len) { |i| new_pass << chars[rand(chars.size-1)] }
    new_pass
end

.get_password_hash(password, salt, algorithm = nil) ⇒ Object

Method gets a password hash with or without salt using supplied hashing algorithm. :sha512 is used if an algorithm is not supplied

Parameters:

  • password (String)

    Password to hash

  • salt (String)

    Salt for enforcing password protection security. Use nil if you want to hash password without salt

  • algorithm (:symbol) (defaults to: nil)

    Hashing algorithm. May be one of followings: :sha1, :sha256, :sha512.



68
69
70
71
72
# File 'lib/chupakabra_tools/security.rb', line 68

def self.get_password_hash(password, salt, algorithm = nil)
    data = password
    data = salt + data if salt && !salt.blank?
    self.hash_it(data, algorithm)
end

.hash_it(data, algorithm = nil) ⇒ Object

Method gets a hash using supplied hashing algorithm. :sha512 is used if an algorithm is not supplied

Parameters:

  • data (String)

    Data to hash

  • algorithm (:symbol) (defaults to: nil)

    Hashing algorithm: :sha1, :sha256, :sha512.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chupakabra_tools/security.rb', line 78

def self.hash_it(data, algorithm = nil)
    case algorithm
        when :sha1
            ::Digest::SHA1.hexdigest(data)
        when :sha256
            ::Digest::SHA256.hexdigest(data)
        when :sha512
            ::Digest::SHA512.hexdigest(data)
        else
            ::Digest::SHA512.hexdigest(data)
    end
end

Instance Method Details

#get_password_hashObject

Method gets a password hash with or without salt using supplied hashing algorithm. :sha512 is used if an algorithm is not supplied

Parameters:

  • password (String)

    Password to hash

  • salt (String)

    Salt for enforcing password protection security. Use nil if you want to hash password without salt

  • algorithm (:symbol)

    Hashing algorithm. May be one of followings: :sha1, :sha256, :sha512.



68
69
70
71
72
# File 'lib/chupakabra_tools/security.rb', line 68

def self.get_password_hash(password, salt, algorithm = nil)
    data = password
    data = salt + data if salt && !salt.blank?
    self.hash_it(data, algorithm)
end

#hash_itObject

Method gets a hash using supplied hashing algorithm. :sha512 is used if an algorithm is not supplied

Parameters:

  • data (String)

    Data to hash

  • algorithm (:symbol)

    Hashing algorithm: :sha1, :sha256, :sha512.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chupakabra_tools/security.rb', line 78

def self.hash_it(data, algorithm = nil)
    case algorithm
        when :sha1
            ::Digest::SHA1.hexdigest(data)
        when :sha256
            ::Digest::SHA256.hexdigest(data)
        when :sha512
            ::Digest::SHA512.hexdigest(data)
        else
            ::Digest::SHA512.hexdigest(data)
    end
end