Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.4 KB

NOTES.md

File metadata and controls

60 lines (47 loc) · 1.4 KB

Notes

Digest - Keccak, SHA3

  • add Keccak256, Keccak512, (sub)classes - why? why not?
  • add SHA3_512, SHA3_256, etc. (sub)classes - why? why not?

add generate_tests.rb

# This will generate a test suite.
# Based on python-sha3's test suite.

FILES = [
  ['test/data/ShortMsgKAT_224.txt', 224],
  ['test/data/ShortMsgKAT_256.txt', 256],
  ['test/data/ShortMsgKAT_384.txt', 384],
  ['test/data/ShortMsgKAT_512.txt', 512],
  ['test/data/LongMsgKAT_224.txt', 224],
]

def generate
  puts %Q{
    # encoding: binary
    # This file generated by generate_tests.rb
    require 'test/unit'
    class KeccakTests < Test::Unit::TestCase
  }

  FILES.each do |path, hashlen|
    contents = File.read(path).split('Len = ')
    contents.each do |test|
      lines = test.split("\n")
      if !lines.empty? && lines[0] !~ /^#/
        length = lines[0].to_i
        if length % 8 == 0 && length != 0
          msg_raw = [lines[1].split(' = ').last].pack("H*")
          md = lines[2].split(' = ').last.downcase
          name = File.basename(path).split('.')[0]
          puts %Q{
            def test_#{name}_#{length}
              inst = Digest::Keccak.new(#{hashlen})
              inst.update(#{msg_raw.inspect})
              assert_equal #{md.inspect}, inst.hexdigest
            end
          }
        end
      end
    end
  end

  puts "end"
end

generate