Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes for Ruby 1.9. Update to newest Hoe.

  • Loading branch information...
commit d5fd0e1912fbd8e3a5ebb474de2073441b2ef539 1 parent f1eb2b2
Geoffrey Grosenbach authored
6 History.txt
View
@@ -1,3 +1,9 @@
+== 0.4.0 / 2010-01-19
+
+* Ruby 1.9 compatibility [Tim Kersey]
+* Minitest integration [Tim Kersey]
+* Updated to Hoe 2.5.0 [Geoffrey Grosenbach]
+
== 0.3.2 / 2008-08-20
* Removed spurious constants that cause warnings on load [Blaine Cook]
1  Manifest.txt
View
@@ -7,4 +7,5 @@ lib/hmac-rmd160.rb
lib/hmac-sha1.rb
lib/hmac-sha2.rb
lib/hmac.rb
+lib/ruby_hmac.rb
test/test_hmac.rb
6 README.txt
View
@@ -1,6 +1,6 @@
-ruby-hmac
- by Daiki Ueno
- http://ruby-hmac.rubyforge.org
+= ruby-hmac
+
+* http://ruby-hmac.rubyforge.org
== DESCRIPTION:
14 Rakefile
View
@@ -3,17 +3,13 @@ require 'hoe'
$:.unshift(File.dirname(__FILE__) + "/lib")
require 'hmac'
-Hoe.new('ruby-hmac', HMAC::VERSION) do |p|
- p.name = "ruby-hmac"
- p.author = ["Daiki Ueno", "Geoffrey Grosenbach"]
- p.email = 'boss@topfunky.com'
- p.summary = "An implementation of the HMAC authentication code in Ruby."
- p.description = "A MAC provides a way to check the integrity of information transmitted over or stored in an unreliable medium, based on a secret key."
- p.url = "http://ruby-hmac.rubyforge.org"
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
- p.remote_rdoc_dir = '' # Release to root
+Hoe.spec 'ruby-hmac' do
+ developer "Daiki Ueno", ""
+ developer "Geoffrey Grosenbach", "boss@topfunky.com"
end
+Hoe.plugin :minitest
+
desc "Simple require on packaged files to make sure they are all there"
task :verify => :package do
# An error message will be displayed if files are missing
21 lib/hmac.rb
View
@@ -17,7 +17,7 @@
module HMAC
- VERSION = '0.3.2'
+ VERSION = '0.4.0'
class Base
def initialize(algorithm, block_size, output_length, key)
@@ -43,14 +43,15 @@ def set_key(key)
# If key is longer than the block size, apply hash function
# to key and use the result as a real key.
key = @algorithm.digest(key) if key.size > @block_size
- key_xor_ipad = "\x36" * @block_size
- key_xor_opad = "\x5C" * @block_size
- for i in 0 .. key.size - 1
- key_xor_ipad[i] ^= key[i]
- key_xor_opad[i] ^= key[i]
+ akey = key.unpack("C*")
+ key_xor_ipad = ("\x36" * @block_size).unpack("C*")
+ key_xor_opad = ("\x5C" * @block_size).unpack("C*")
+ for i in 0 .. akey.size - 1
+ key_xor_ipad[i] ^= akey[i]
+ key_xor_opad[i] ^= akey[i]
end
- @key_xor_ipad = key_xor_ipad
- @key_xor_opad = key_xor_opad
+ @key_xor_ipad = key_xor_ipad.pack("C*")
+ @key_xor_opad = key_xor_opad.pack("C*")
@md = @algorithm.new
@initialized = true
end
@@ -93,8 +94,8 @@ def hexdigest
# instance methods combinatorially because an instance will have
# held a key even if it's no longer in use.
def Base.digest(key, text)
+ hmac = self.new(key)
begin
- hmac = self.new(key)
hmac.update(text)
hmac.digest
ensure
@@ -103,8 +104,8 @@ def Base.digest(key, text)
end
def Base.hexdigest(key, text)
+ hmac = self.new(key)
begin
- hmac = self.new(key)
hmac.update(text)
hmac.hexdigest
ensure
2  lib/ruby_hmac.rb
View
@@ -0,0 +1,2 @@
+# Convenience file to match gem name
+require 'hmac'
61 test/test_hmac.rb
View
@@ -1,11 +1,18 @@
#!/usr/bin/env ruby
-require 'test/unit'
+$: << File.dirname(__FILE__) + "/../lib"
+require "hmac-md5"
+require "hmac-sha1"
+begin
+ require "minitest/unit"
+rescue LoadError
+ require "rubygems"
+ require "minitest/unit"
+end
-require 'hmac-md5'
-require 'hmac-sha1'
+MiniTest::Unit.autorun
-class TestHMAC < Test::Unit::TestCase
+class TestHmac < MiniTest::Unit::TestCase
def test_s_digest
key = "\x0b" * 16
@@ -13,7 +20,7 @@ def test_s_digest
hmac = HMAC::MD5.new(key)
hmac.update(text)
-
+
assert_equal(hmac.digest, HMAC::MD5.digest(key, text))
end
@@ -23,70 +30,60 @@ def test_s_hexdigest
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")
+ "9294727a3638bb1c13f48ef8158bfc9d")
end
def test_hmac_md5_2
assert_equal(HMAC::MD5.hexdigest("Jefe", "what do ya want for nothing?"),
- "750c783e6ab0b503eaa86e310a5db738")
+ "750c783e6ab0b503eaa86e310a5db738")
end
def test_hmac_md5_3
assert_equal(HMAC::MD5.hexdigest("\xaa" * 16, "\xdd" * 50),
- "56be34521d144c88dbb8c733f0e8b3f6")
+ "56be34521d144c88dbb8c733f0e8b3f6")
end
def test_hmac_md5_4
assert_equal(HMAC::MD5.hexdigest(["0102030405060708090a0b0c0d0e0f10111213141516171819"].pack("H*"), "\xcd" * 50),
- "697eaf0aca3a3aea3a75164746ffaa79")
+ "697eaf0aca3a3aea3a75164746ffaa79")
end
def test_hmac_md5_5
assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
- "56461ef2342edc00f9bab995690efd4c")
+ "56461ef2342edc00f9bab995690efd4c")
end
-# def test_hmac_md5_6
-# assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
-# "56461ef2342edc00f9bab995")
-# 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")
+ "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")
+ "6f630fad67cda0ee1fb1f562db3aa53e")
end
def test_reset_key
hmac = HMAC::MD5.new("key")
- assert_nothing_raised {
- hmac.reset_key
- }
- assert_raise(RuntimeError) {
- hmac.update("foo")
- }
+ hmac.reset_key
+ assert_raises(RuntimeError) { hmac.update("foo") }
end
def test_set_key
hmac = HMAC::MD5.new
- assert_raise(RuntimeError) {
- hmac.update("foo")
- }
- assert_nothing_raised {
- hmac.reset_key
- }
- assert_raise(RuntimeError) {
- hmac.update("foo")
- }
+ assert_raises(RuntimeError) { hmac.update("foo") }
+ hmac.reset_key
+ assert_raises(RuntimeError) { hmac.update("foo") }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.