m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/engine.rb
blob: 7b9ddf0e638cd25f4d40777b9467afefe921be31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class InvalidChecksumError < StandardError
end

class Engine
  def initialize hash
    validate_checksum hash
    hash_to_state hash
  end

  def validate_checksum hash
    if !checksum_valid? hash
      raise InvalidChecksumError
    end
  end

  def checksum_valid? hash
    if hash.empty?
      return true
    end

    expected = checksum hash
    expected == hash['checksum']
  end

  def checksum hash
    string = hash
      .keys
      .filter { |k| k != 'checksum' }
      .sort
      .map { |k| "#{k}:#{hash[k]}" }
      .append("secret:#{secret}")
      .join '|'
    Digest::SHA2.hexdigest string
  end

  def hash_to_state hash
  end

  def step command
  end

  def state_h
    hash = state_to_hash
    hash['checksum'] = checksum hash
    hash
  end

  def state_to_hash
    {}
  end

  def message
  end
end