class Digest::SHA3

Constants

PILN
RNDC
ROTC

Public Class Methods

new(hash_size = 512) click to toggle source
# File lib/sha3-pure-ruby.rb, line 23
def initialize hash_size = 512
  @size = hash_size / 8
  @buffer = ''
end

Public Instance Methods

<<(s) click to toggle source
# File lib/sha3-pure-ruby.rb, line 28
def << s
  @buffer << s
  self
end
Also aliased as: update
finish() click to toggle source
# File lib/sha3-pure-ruby.rb, line 39
def finish
  s = Array.new 25, 0
  width = 200 - @size * 2
  
  buffer = @buffer
  buffer << "\x01" << "\0" * (width - buffer.size % width)
  buffer[-1] = (buffer[-1].ord | 0x80).chr
  
  0.step buffer.size - 1, width do |j|
    quads = buffer[j, width].unpack 'Q*'
    (width / 8).times do |i|
      s[i] ^= quads[i]
    end
    
    keccak s
  end
  
  s.pack('Q*')[0, @size]
end
reset() click to toggle source
# File lib/sha3-pure-ruby.rb, line 34
def reset
  @buffer.clear
  self
end
update(s)
Alias for: <<

Private Instance Methods

keccak(s) click to toggle source
# File lib/sha3-pure-ruby.rb, line 60
def keccak s
  24.times.each_with_object [] do |round, a|
    # Theta
    5.times do |i|
      a[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]
    end
    
    5.times do |i|
      t = a[(i + 4) % 5] ^ rotate(a[(i + 1) % 5], 1)
      0.step 24, 5 do |j|
        s[j + i] ^= t
      end
    end
    
    # Rho Pi
    t = s[1]
    24.times do |i|
      j = PILN[i]
      a[0] = s[j]
      s[j] = rotate t, ROTC[i]
      t = a[0]
    end
    
    # Chi
    0.step 24, 5 do |j|
      5.times do |i|
        a[i] = s[j + i]
      end
      
      5.times do |i|
        s[j + i] ^= ~a[(i + 1) % 5] & a[(i + 2) % 5]
      end
    end
    
    # Iota
    s[0] ^= RNDC[round]
  end
end
rotate(x, y) click to toggle source
# File lib/sha3-pure-ruby.rb, line 99
def rotate x, y
  (x << y | x >> 64 - y) & (1 << 64) - 1
end