Class: CryptoToolchain::Utilities::SHA1

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ SHA1

Returns a new instance of SHA1.



21
22
23
# File 'lib/crypto_toolchain/utilities/sha1.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/sha1.rb', line 10

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

.digestObject



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

def bindigest(str, state: INITIAL_STATE, append_length: 0)
  CryptoToolchain::Utilities::SHA1.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/sha1.rb', line 6

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

.padding(str) ⇒ Object



15
16
17
18
# File 'lib/crypto_toolchain/utilities/sha1.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
# File 'lib/crypto_toolchain/utilities/sha1.rb', line 29

def bindigest(state: INITIAL_STATE, append_length: 0)
  h = registers_from(state).dup

  length = original.bytesize + append_length

  # while (string.size % 64) != 56
  num_null_pad = (56 - (length + 1) ) % 64
  padding = 0x80.chr + (0.chr * num_null_pad) + [length * 8].pack("Q>")

  (original + padding).in_blocks(64).each do |_block|
    w = _block.unpack("L>16")
    (16..79).each do |i|
      w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]).lrot(1)
    end

    a, b, c, d, e = h

    (0..79).each do |i|
      func, k = f_and_k_for(i)
      f = func.call(b, c, d)
      temp = (a.lrot(5) + f + e + k + w[i]) & 0xffffffff
      e = d
      d = c
      c = b.lrot(30)
      b = a
      a = temp
    end

    [a, b, c, d, e].each_with_index do |val, i|
      h[i] = (h[i] + val) & 0xffffffff
    end
  end
  h.pack("L>5")
end

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



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

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