module SipHash

Public Class Methods

digest(key, msg) click to toggle source
# File lib/siphash.rb, line 3
def self.digest(key, msg)
  s = State.new(key)
  len = msg.size
  iter = len / 8

  iter.times do |i|
    m = msg.slice(i * 8, 8).unpack("Q<")[0]
    s.apply_block(m)
  end

  m = last_block(msg, len, iter)

  s.apply_block(m)
  s.finalize
  s.digest
end

Private Class Methods

last_block(msg, len, iter) click to toggle source
# File lib/siphash.rb, line 22
def self.last_block(msg, len, iter)
  last = (len << 56) & State::MASK_64;
    
  r = len % 8
  off = iter * 8

  last |= msg[off + 6].ord << 48 if r >= 7
  last |= msg[off + 5].ord << 40 if r >= 6
  last |= msg[off + 4].ord << 32 if r >= 5
  last |= msg[off + 3].ord << 24 if r >= 4
  last |= msg[off + 2].ord << 16 if r >= 3
  last |= msg[off + 1].ord << 8 if r >= 2
  last |= msg[off].ord if r >= 1
  last
end