Class: Digest::SHA3Lite

Inherits:
Class
  • Object
show all
Defined in:
lib/digest-lite/sha3.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) ⇒ SHA3Lite

Returns a new instance of SHA3Lite.



33
34
35
36
# File 'lib/digest-lite/sha3.rb', line 33

def initialize( hash_size = 512 )
  @size   = hash_size / 8
  @buffer = ''     ## todo/check: make sure buffer is with encoding BINARY - why? why not?
end

Instance Method Details

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



38
39
40
41
# File 'lib/digest-lite/sha3.rb', line 38

def <<( s )
  @buffer << s
  self
end

#finishObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/digest-lite/sha3.rb', line 49

def finish
  s = Array.new( 25, 0 )
  width = 200 - @size * 2

  ###  note: padding changed in final FIPS PUB 202 standard
  ##           from "\x01" to "\x06"
  padding = "\x06"

  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

#resetObject



44
45
46
47
# File 'lib/digest-lite/sha3.rb', line 44

def reset
  @buffer.clear
  self
end