Class: CryptoToolchain::Utilities::MD4

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/utilities/md4.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ MD4

Returns a new instance of MD4.



21
22
23
# File 'lib/crypto_toolchain/utilities/md4.rb', line 21

def initialize(message)
  @original = message
end

Class Method Details

.bindigest(str, state: INITIAL_STATE, append_length: 0) ⇒ Object



10
11
12
# File 'lib/crypto_toolchain/utilities/md4.rb', line 10

def bindigest(str, state: INITIAL_STATE, append_length: 0)
  CryptoToolchain::Utilities::MD4.new(str).bindigest(state: state, append_length: append_length)
end

.digestObject



13
14
15
# File 'lib/crypto_toolchain/utilities/md4.rb', line 13

def bindigest(str, state: INITIAL_STATE, append_length: 0)
  CryptoToolchain::Utilities::MD4.new(str).bindigest(state: state, append_length: append_length)
end

.hexdigest(str, state: INITIAL_STATE, append_length: 0) ⇒ Object



6
7
8
# File 'lib/crypto_toolchain/utilities/md4.rb', line 6

def hexdigest(str, state: INITIAL_STATE, append_length: 0 )
  CryptoToolchain::Utilities::MD4.new(str).hexdigest(state: state, append_length: append_length)
end

.padding(str) ⇒ Object



15
16
17
18
# File 'lib/crypto_toolchain/utilities/md4.rb', line 15

def padding(str)
  num_null_pad = (56 - (str.bytesize + 1) ) % 64
  0x80.chr + (0.chr * num_null_pad) + [(str.bytesize * 8)].pack("Q<")
end

Instance Method Details

#bindigest(state: INITIAL_STATE, append_length: 0) ⇒ Object Also known as: digest



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
62
63
64
65
66
67
68
69
# File 'lib/crypto_toolchain/utilities/md4.rb', line 29

def bindigest(state: INITIAL_STATE, append_length: 0)
  running_state = registers_from(state)

  length = original.bytesize + append_length

  padding_len = (56 - (length + 1) ) % 64
  str_length = [(length * 8)].pack("Q<")
  padding = (0x80.chr + (0.chr * padding_len) + str_length)

  (original + padding).in_blocks(64).each do |block|
    w = block.unpack("L<16")

    a, b, c, d = running_state
    # Extraction of each 16-operation round into a loop over four elements originally
    # found at https://rosettacode.org/wiki/MD4#Ruby
    [0, 4, 8, 12].each do |i|
      a = f(a, b, c, d, w[i]).lrot(3)
      d = f(d, a, b, c, w[i+1]).lrot(7)
      c = f(c, d, a, b, w[i+2]).lrot(11)
      b = f(b, c, d, a, w[i+3]).lrot(19)
    end
    [0, 1, 2, 3].each do |i|
      a = g(a, b, c, d, w[i]).lrot(3)
      d = g(d, a, b, c, w[i+4]).lrot(5)
      c = g(c, d, a, b, w[i+8]).lrot(9)
      b = g(b, c, d, a, w[i+12]).lrot(13)
    end
    [0, 2, 1, 3].each do |i|
      a = h(a, b, c, d, w[i]).lrot(3)
      d = h(d, a, b, c, w[i+8]).lrot(9)
      c = h(c, d, a, b, w[i+4]).lrot(11)
      b = h(b, c, d, a, w[i+12]).lrot(15)
    end

    [a, b, c, d].each_with_index do |val, i|
      running_state[i] = (running_state[i] + val) & 0xffffffff
    end
  end

  running_state.pack("L<4")
end

#hexdigest(state: INITIAL_STATE, append_length: 0) ⇒ Object



25
26
27
# File 'lib/crypto_toolchain/utilities/md4.rb', line 25

def hexdigest(state: INITIAL_STATE, append_length: 0)
  bindigest(state: state, append_length: append_length).unpack("H*").join
end