Class: GasfreeSdk::Crypto::Keccak256

Inherits:
Digest::Class
  • Object
show all
Defined in:
lib/gasfree_sdk/crypto.rb

Overview

Keccak256 implementation for TRON EIP-712 signatures

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].freeze
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].freeze
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].freeze

Instance Method Summary collapse

Constructor Details

#initializeKeccak256



26
27
28
29
30
31
# File 'lib/gasfree_sdk/crypto.rb', line 26

def initialize
  @size = 256 / 8
  @buffer = "".dup  # Use dup to ensure it's not frozen

  super
end

Instance Method Details

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



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

def <<(string)
  @buffer << string.to_s
  self
end

#finishObject

rubocop:disable Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gasfree_sdk/crypto.rb', line 44

def finish # rubocop:disable Metrics/AbcSize
  string = Array.new 25, 0
  width = 200 - (@size * 2)
  padding = "\x01".dup

  buffer = @buffer.dup # Create a copy to avoid modifying the original
  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|
      string[i] ^= quads[i]
    end

    keccak string
  end

  string.pack("Q*")[0, @size]
end

#resetObject



39
40
41
42
# File 'lib/gasfree_sdk/crypto.rb', line 39

def reset
  @buffer = "".dup  # Create a new unfrozen string instead of clearing
  self
end