Class: Digest::SHA3

Inherits:
Class
  • Object
show all
Defined in:
lib/sha3-pure-ruby.rb

Constant Summary collapse

PILN =
[10,  7, 11, 17, 18,  3,  5, 16,
 8, 21, 24,  4, 15, 23, 19, 13,
12,  2, 20, 14, 22,  9,  6,  1]
ROTC =
[ 1,  3,  6, 10, 15, 21, 28, 36,
45, 55,  2, 14, 27, 41, 56,  8,
25, 43, 62, 18, 39, 61, 20, 44]
RNDC =
[0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008]

Instance Method Summary collapse

Constructor Details

#initialize(hash_size = 512) ⇒ SHA3

Returns a new instance of SHA3.



23
24
25
26
# File 'lib/sha3-pure-ruby.rb', line 23

def initialize hash_size = 512
  @size = hash_size / 8
  @buffer = ''
end

Instance Method Details

#<<(s) ⇒ Object Also known as: update



28
29
30
31
# File 'lib/sha3-pure-ruby.rb', line 28

def << s
  @buffer << s
  self
end

#finishObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#resetObject



34
35
36
37
# File 'lib/sha3-pure-ruby.rb', line 34

def reset
  @buffer.clear
  self
end