Top Level Namespace

Defined Under Namespace

Modules: Dry Classes: Hash

Constant Summary collapse

APR1_MAGIC =
'$apr1$'
ITOA64 =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Instance Method Summary collapse

Instance Method Details

#apr1_check(password, hashed_password) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/dry-stack/apache_specific_md5.rb', line 53

def apr1_check(password, hashed_password)
  parts = hashed_password.split('$')
  return false if parts.length != 4 || parts[1] != 'apr1'

  salt = parts[2]
  apr1_crypt(password, salt) == hashed_password
end

#apr1_crypt(password, salt) ⇒ Object



15
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
# File 'lib/dry-stack/apache_specific_md5.rb', line 15

def apr1_crypt(password, salt)
  salt = salt[0, 8]
  ctx = Digest::MD5.new
  ctx.update(password + APR1_MAGIC + salt)
  final = Digest::MD5.digest(password + salt + password)

  password.length.times { |i| ctx.update(final[i % 16].chr) }

  length = password.length
  while length > 0
    ctx.update(length & 1 != 0 ? "\0" : password[0].chr)
    length >>= 1
  end

  final = ctx.digest

  1000.times do |i|
    ctx = Digest::MD5.new
    ctx.update(i & 1 != 0 ? password : final)
    ctx.update(salt) unless (i % 3).zero?
    ctx.update(password) unless (i % 7).zero?
    ctx.update(i & 1 != 0 ? final : password)
    final = ctx.digest
  end

  hashed = final.bytes
  result = [
    to64((hashed[0] << 16) | (hashed[6] << 8) | hashed[12], 4),
    to64((hashed[1] << 16) | (hashed[7] << 8) | hashed[13], 4),
    to64((hashed[2] << 16) | (hashed[8] << 8) | hashed[14], 4),
    to64((hashed[3] << 16) | (hashed[9] << 8) | hashed[15], 4),
    to64((hashed[4] << 16) | (hashed[10] << 8) | hashed[5], 4),
    to64(hashed[11], 2)
  ].join

  "#{APR1_MAGIC}#{salt}$#{result}"
end

#exec_i(cmd, input_string = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/dry-stack/command_line.rb', line 4

def exec_i(cmd, input_string = nil)
  puts "exec_i(inputs.size #{input_string&.size}): #{cmd}"
  Open3.popen3(cmd) do |i, o, e, wait_thr|
    i.puts input_string unless input_string.nil?
    i.close
    while line = o.gets; puts "o: " + line end
    while line = e.gets; puts "o: " + line end
    return_value = wait_thr.value
    puts "Error level was: #{return_value.exitstatus}" unless return_value.success?
    exit return_value.exitstatus unless return_value.success?
  end
end

#exec_o_lines(cmd) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/dry-stack/command_line.rb', line 17

def exec_o_lines(cmd,&)
  IO.popen(cmd, 'r') do |f|
    f.each_line do |line|
      yield line
    end
  end
end

#to64(value, length) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/dry-stack/apache_specific_md5.rb', line 6

def to64(value, length)
  result = ''
  length.times do
    result << ITOA64[value & 0x3f]
    value >>= 6
  end
  result
end