Module: Bundler::Digest

Defined in:
lib/bundler/digest.rb

Constant Summary collapse

SHA1_WORDS =

The initial constant values for the 32-bit constant words A, B, C, D, and E, respectively.

[0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0].freeze
SHA1_MASK =

The 8-bit field used for bitwise ‘AND` masking. Defaults to `0xFFFFFFFF`.

0xFFFFFFFF

Class Method Summary collapse

Class Method Details

.sha1(string) ⇒ Object



14
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
52
53
54
# File 'lib/bundler/digest.rb', line 14

def sha1(string)
  unless string.is_a?(String)
    raise TypeError, "can't convert #{string.class.inspect} into String"
  end

  buffer = string.b

  words = SHA1_WORDS.dup
  generate_split_buffer(buffer) do |chunk|
    w = []
    chunk.each_slice(4) do |a, b, c, d|
      w << (((a << 8 | b) << 8 | c) << 8 | d)
    end
    a, b, c, d, e = *words
    (16..79).each do |i|
      w[i] = SHA1_MASK & rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1)
    end
    0.upto(79) do |i|
      case i
      when  0..19
        f = ((b & c) | (~b & d))
        k = 0x5A827999
      when 20..39
        f = (b ^ c ^ d)
        k = 0x6ED9EBA1
      when 40..59
        f = ((b & c) | (b & d) | (c & d))
        k = 0x8F1BBCDC
      when 60..79
        f = (b ^ c ^ d)
        k = 0xCA62C1D6
      end
      t = SHA1_MASK & rotate(a, 5) + f + e + k + w[i]
      a, b, c, d, e = t, a, SHA1_MASK & rotate(b, 30), c, d # rubocop:disable Style/ParallelAssignment
    end
    mutated = [a, b, c, d, e]
    words.map!.with_index {|word, index| SHA1_MASK & (word + mutated[index]) }
  end

  words.pack("N*").unpack("H*").first
end