Class: UnixCrypt::MD5

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

Class Method Summary collapse

Methods inherited from Base

build, generate_salt

Class Method Details

.byte_indexesObject



67
68
69
# File 'lib/unix_crypt.rb', line 67

def self.byte_indexes
  [[0, 6, 12], [1, 7, 13], [2, 8, 14], [3, 9, 15], [4, 10, 5], [nil, nil, 11]]
end

.default_salt_lengthObject



64
# File 'lib/unix_crypt.rb', line 64

def self.default_salt_length; 6; end

.digestObject



62
# File 'lib/unix_crypt.rb', line 62

def self.digest; Digest::MD5; end

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/unix_crypt.rb', line 71

def self.hash(password, salt, ignored = nil)
  salt = salt[0..7]

  password = prepare_password(password)

  b = digest.digest("#{password}#{salt}#{password}")
  a_string = "#{password}$1$#{salt}#{b * (password.length/length)}#{b[0...password.length % length]}"

  password_length = password.length
  while password_length > 0
    a_string += (password_length & 1 != 0) ? "\x0" : password[0].chr
    password_length >>= 1
  end

  input = digest.digest(a_string)

  1000.times do |index|
    c_string = ((index & 1 != 0) ? password : input)
    c_string += salt unless index % 3 == 0
    c_string += password unless index % 7 == 0
    c_string += ((index & 1 != 0) ? input : password)
    input = digest.digest(c_string)
  end

  bit_specified_base64encode(input)
end

.identifierObject



65
# File 'lib/unix_crypt.rb', line 65

def self.identifier; 1; end

.lengthObject



63
# File 'lib/unix_crypt.rb', line 63

def self.length; 16; end