PPaste!

sha256_init.rb

Home - All the pastes - Authored by Thooms

Raw version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env ruby
# encoding: utf-8

# The initial hash value H(0) is the sequence of 8 32-bits words which
# are obtained by taking the fractional parts of the square root of
# the 8 first prime numbers

FirstPrimes = [2, 3, 5, 7, 11, 13, 17, 19]
if __FILE__ == $0
  FirstPrimes.each_with_index do |prime,index|
    square_root = Math.sqrt(prime)
    current = square_root - square_root.to_i
    result = 0
    32.times do
      current *= 2
      result <<= 1
      if current > 1
        result |= 1
        current -= 1
      end
    end
    puts "H#{index + 1}(0) = #{result.to_s(16)}"
  end
end