class Digest::Keccak256
Constants
- PILN
- RNDC
- ROTC
Public Class Methods
new()
click to toggle source
# File lib/keccak256.rb, line 22 def initialize @size = 256 / 8 @buffer = '' end
Public Instance Methods
<<(s)
click to toggle source
# File lib/keccak256.rb, line 27 def << s @buffer << s self end
Also aliased as: update
finish()
click to toggle source
# File lib/keccak256.rb, line 38 def finish s = Array.new 25, 0 width = 200 - @size * 2 padding = "\x01" buffer = @buffer buffer << padding << "\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/keccak256.rb, line 33 def reset @buffer.clear self end
Private Instance Methods
keccak(s)
click to toggle source
# File lib/keccak256.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/keccak256.rb, line 99 def rotate x, y (x << y | x >> 64 - y) & (1 << 64) - 1 end