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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#!/usr/bin/env ruby
$: << File.dirname(__FILE__) + "/../lib"
require "hmac-md5"
require "hmac-sha1"
begin
require "minitest/autorun"
rescue LoadError
require "rubygems"
require "minitest/autorun"
end
Minitest.autorun
class TestHmac < Minitest::Test
def test_s_digest
key = "\x0b" * 16
text = "Hi There"
hmac = HMAC::MD5.new(key)
hmac.update(text)
assert_equal(hmac.digest, HMAC::MD5.digest(key, text))
end
def test_s_hexdigest
key = "\x0b" * 16
text = "Hi There"
hmac = HMAC::MD5.new(key)
hmac.update(text)
assert_equal(hmac.hexdigest, HMAC::MD5.hexdigest(key, text))
end
def test_hmac_md5_1
assert_equal(HMAC::MD5.hexdigest("\x0b" * 16, "Hi There"),
"9294727a3638bb1c13f48ef8158bfc9d")
end
def test_hmac_md5_2
assert_equal(HMAC::MD5.hexdigest("Jefe", "what do ya want for nothing?"),
"750c783e6ab0b503eaa86e310a5db738")
end
def test_hmac_md5_3
assert_equal(HMAC::MD5.hexdigest("\xaa" * 16, "\xdd" * 50),
"56be34521d144c88dbb8c733f0e8b3f6")
end
def test_hmac_md5_4
assert_equal(HMAC::MD5.hexdigest(["0102030405060708090a0b0c0d0e0f10111213141516171819"].pack("H*"), "\xcd" * 50),
"697eaf0aca3a3aea3a75164746ffaa79")
end
def test_hmac_md5_5
assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
"56461ef2342edc00f9bab995690efd4c")
end
# def test_hmac_md5_6
# assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
# "56461ef2342edc00f9bab995")
# end
def test_hmac_md5_7
assert_equal(HMAC::MD5.hexdigest("\xaa" * 80, "Test Using Larger Than Block-Size Key - Hash Key First"),
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
end
def test_hmac_md5_8
assert_equal(HMAC::MD5.hexdigest("\xaa" * 80, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
"6f630fad67cda0ee1fb1f562db3aa53e")
end
def test_reset_key
hmac = HMAC::MD5.new("key")
hmac.reset_key
assert_raises(RuntimeError) { hmac.update("foo") }
end
def test_set_key
hmac = HMAC::MD5.new
assert_raises(RuntimeError) { hmac.update("foo") }
hmac.reset_key
assert_raises(RuntimeError) { hmac.update("foo") }
end
end
|