Class: UnixCrypt::SHABase

Inherits:
Base
  • Object
show all
Defined in:
lib/unix_crypt.rb

Direct Known Subclasses

SHA256, SHA512

Class Method Summary collapse

Methods inherited from Base

build

Class Method Details

.hash(password, salt, rounds = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unix_crypt.rb', line 76

def self.hash(password, salt, rounds = nil)
  rounds ||= 5000
  rounds = 1000        if rounds < 1000
  rounds = 999_999_999 if rounds > 999_999_999

  salt = salt[0..15]

  b = digest.digest("#{password}#{salt}#{password}")

  a_string = password + salt + b * (password.length/length) + b[0...password.length % length]

  password_length = password.length
  while password_length > 0
    a_string += (password_length & 1 != 0) ? b : password
    password_length >>= 1
  end

  input = a = digest.digest(a_string)

  dp = digest.digest(password * password.length)
  p = dp * (password.length/length) + dp[0...password.length % length]

  ds = digest.digest(salt * (16 + a.bytes.first))
  s = ds * (salt.length/length) + ds[0...salt.length % length]

  rounds.times do |index|
    c_string = ((index & 1 != 0) ? p : input)
    c_string += s unless index % 3 == 0
    c_string += p unless index % 7 == 0
    c_string += ((index & 1 != 0) ? input : p)
    input = digest.digest(c_string)
  end

  base64encode(input)
end