Class: Digest::RubySHA3

Inherits:
Class
  • Object
show all
Defined in:
lib/iota/crypto/sha3_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) ⇒ RubySHA3

Returns a new instance of RubySHA3.



22
23
24
25
# File 'lib/iota/crypto/sha3_ruby.rb', line 22

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

Instance Method Details

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



27
28
29
30
# File 'lib/iota/crypto/sha3_ruby.rb', line 27

def << s
  @buffer << s.unpack('C*').pack('C*')
  self
end

#digest(data = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/iota/crypto/sha3_ruby.rb', line 38

def digest(data = nil)
  if data
    update(data)
    value = finish
  else
    cloned = dup
    value = cloned.finish
  end
  value
end

#finishObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/iota/crypto/sha3_ruby.rb', line 58

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

  buffer_dup = @buffer.dup
  buffer_dup << "\x01" << "\0" * (width - buffer_dup.size % width)
  buffer_dup[-1] = (buffer_dup[-1].ord | 0x80).chr.unpack('C*').pack('C*')

  0.step buffer_dup.size - 1, width do |j|
    quads = buffer_dup[j, width].unpack 'Q*'
    (width / 8).times do |i|
      s[i] ^= quads[i]
    end

    keccak s
  end

  s.pack('Q*')[0, @size]
end

#hexdigest(data = nil) ⇒ Object



49
50
51
52
# File 'lib/iota/crypto/sha3_ruby.rb', line 49

def hexdigest(data = nil)
  value = digest(data)
  value.unpack("H*").first
end

#inspectObject



54
55
56
# File 'lib/iota/crypto/sha3_ruby.rb', line 54

def inspect
  "#<#{self.class}: #{hexdigest}>"
end

#resetObject



33
34
35
36
# File 'lib/iota/crypto/sha3_ruby.rb', line 33

def reset
  # @buffer.clear # CHANGE: DO NOT CLEAR BUFFER AS WE NEED
  self
end