Module: Remitmd::Keccak

Defined in:
lib/remitmd/keccak.rb

Overview

Pure-Ruby Keccak-256 (Ethereum variant - NOT SHA-3).

SHA-3 uses different padding (0x06 instead of 0x01). This implementation matches Ethereum's keccak256.

Reference: https://keccak.team/keccak_specs_summary.html Rate = 1088 bits (136 bytes), Capacity = 512 bits, Output = 256 bits.

Constant Summary collapse

RATE_BYTES =
136
MASK64 =
0xFFFFFFFFFFFFFFFF
RC =
[
  0x0000000000000001, 0x0000000000008082, 0x800000000000808A, 0x8000000080008000,
  0x000000000000808B, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
  0x000000000000008A, 0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
  0x000000008000808B, 0x800000000000008B, 0x8000000000008089, 0x8000000000008003,
  0x8000000000008002, 0x8000000000000080, 0x000000000000800A, 0x800000008000000A,
  0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
].freeze
RHO =
[
  1, 62, 28, 27, 36, 44, 6, 55, 20, 3,
  10, 43, 25, 39, 41, 45, 15, 21, 8, 18,
  2, 61, 56, 14
].freeze
PI =
[
  10, 20, 5, 15, 16, 1, 11, 21, 6, 7,
  17, 2, 12, 22, 23, 8, 18, 3, 13, 14,
  24, 9, 19, 4
].freeze

Class Method Summary collapse

Class Method Details

.digest(data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/remitmd/keccak.rb', line 37

def digest(data)
  data = data.b if data.encoding != Encoding::BINARY
  padded = pad(data)
  state = Array.new(25, 0)
  offset = 0
  while offset < padded.bytesize
    absorb!(state, padded, offset)
    offset += RATE_BYTES
  end
  squeeze(state)
end

.hexdigest(data) ⇒ Object



49
50
51
# File 'lib/remitmd/keccak.rb', line 49

def hexdigest(data)
  digest(data).unpack1("H*")
end