ruby-hmac-0.4.0/0000755000175000017500000000000011561276015013322 5ustar vasudevvasudevruby-hmac-0.4.0/metadata.yml0000644000175000017500000000456611561276015015640 0ustar vasudevvasudev--- !ruby/object:Gem::Specification name: ruby-hmac version: !ruby/object:Gem::Version version: 0.4.0 platform: ruby authors: - Daiki Ueno - Geoffrey Grosenbach autorequire: bindir: bin cert_chain: [] date: 2010-01-20 00:00:00 -08:00 default_executable: dependencies: - !ruby/object:Gem::Dependency name: rubyforge type: :development version_requirement: version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 2.0.3 version: - !ruby/object:Gem::Dependency name: gemcutter type: :development version_requirement: version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.2.1 version: - !ruby/object:Gem::Dependency name: hoe type: :development version_requirement: version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 2.5.0 version: description: |- This module provides common interface to HMAC functionality. HMAC is a kind of "Message Authentication Code" (MAC) algorithm whose standard is documented in RFC2104. Namely, a MAC provides a way to check the integrity of information transmitted over or stored in an unreliable medium, based on a secret key. Originally written by Daiki Ueno. Converted to a RubyGem by Geoffrey Grosenbach email: - "" - boss@topfunky.com executables: [] extensions: [] extra_rdoc_files: - History.txt - Manifest.txt - README.txt files: - History.txt - Manifest.txt - README.txt - Rakefile - lib/hmac-md5.rb - lib/hmac-rmd160.rb - lib/hmac-sha1.rb - lib/hmac-sha2.rb - lib/hmac.rb - lib/ruby_hmac.rb - test/test_hmac.rb has_rdoc: true homepage: http://ruby-hmac.rubyforge.org licenses: [] post_install_message: rdoc_options: - --main - README.txt require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: requirements: [] rubyforge_project: ruby-hmac rubygems_version: 1.3.5 signing_key: specification_version: 3 summary: This module provides common interface to HMAC functionality test_files: - test/test_hmac.rb ruby-hmac-0.4.0/Rakefile0000644000175000017500000000110411561276015014763 0ustar vasudevvasudevrequire 'rubygems' require 'hoe' $:.unshift(File.dirname(__FILE__) + "/lib") require 'hmac' Hoe.spec 'ruby-hmac' do developer "Daiki Ueno", "" developer "Geoffrey Grosenbach", "boss@topfunky.com" end Hoe.plugin :minitest Hoe.plugin :git Hoe.plugin :gemcutter 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 if system %(ruby -e "require 'pkg/ruby-hmac-#{HMAC::VERSION}/lib/hmac'") puts "\nThe library files are present" end end task :release => :verify ruby-hmac-0.4.0/Manifest.txt0000644000175000017500000000024111561276015015626 0ustar vasudevvasudevHistory.txt Manifest.txt README.txt Rakefile lib/hmac-md5.rb lib/hmac-rmd160.rb lib/hmac-sha1.rb lib/hmac-sha2.rb lib/hmac.rb lib/ruby_hmac.rb test/test_hmac.rb ruby-hmac-0.4.0/lib/0000755000175000017500000000000011561276015014070 5ustar vasudevvasudevruby-hmac-0.4.0/lib/hmac-sha1.rb0000644000175000017500000000031711561276015016160 0ustar vasudevvasudevrequire 'hmac' require 'digest/sha1' module HMAC class SHA1 < Base def initialize(key = nil) super(Digest::SHA1, 64, 20, key) end public_class_method :new, :digest, :hexdigest end end ruby-hmac-0.4.0/lib/hmac-rmd160.rb0000644000175000017500000000032511561276015016334 0ustar vasudevvasudevrequire 'hmac' require 'digest/rmd160' module HMAC class RMD160 < Base def initialize(key = nil) super(Digest::RMD160, 64, 20, key) end public_class_method :new, :digest, :hexdigest end end ruby-hmac-0.4.0/lib/hmac.rb0000644000175000017500000000555411561276015015336 0ustar vasudevvasudev# Copyright (C) 2001 Daiki Ueno # This library is distributed under the terms of the Ruby license. # This module provides common interface to HMAC engines. # HMAC standard is documented in RFC 2104: # # H. Krawczyk et al., "HMAC: Keyed-Hashing for Message Authentication", # RFC 2104, February 1997 # # These APIs are inspired by JCE 1.2's javax.crypto.Mac interface. # # # # Source repository is at # # http://github.com/topfunky/ruby-hmac/tree/master module HMAC VERSION = '0.4.0' class Base def initialize(algorithm, block_size, output_length, key) @algorithm = algorithm @block_size = block_size @output_length = output_length @initialized = false @key_xor_ipad = '' @key_xor_opad = '' set_key(key) unless key.nil? end private def check_status unless @initialized raise RuntimeError, "The underlying hash algorithm has not yet been initialized." end end public 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 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.pack("C*") @key_xor_opad = key_xor_opad.pack("C*") @md = @algorithm.new @initialized = true end def reset_key @key_xor_ipad.gsub!(/./, '?') @key_xor_opad.gsub!(/./, '?') @key_xor_ipad[0..-1] = '' @key_xor_opad[0..-1] = '' @initialized = false end def update(text) check_status # perform inner H md = @algorithm.new md.update(@key_xor_ipad) md.update(text) str = md.digest # perform outer H md = @algorithm.new md.update(@key_xor_opad) md.update(str) @md = md end alias << update def digest check_status @md.digest end def hexdigest check_status @md.hexdigest end alias to_s hexdigest # These two class methods below are safer than using above # 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.update(text) hmac.digest ensure hmac.reset_key end end def Base.hexdigest(key, text) hmac = self.new(key) begin hmac.update(text) hmac.hexdigest ensure hmac.reset_key end end private_class_method :new, :digest, :hexdigest end end ruby-hmac-0.4.0/lib/hmac-sha2.rb0000644000175000017500000000102111561276015016152 0ustar vasudevvasudevrequire 'hmac' require 'digest/sha2' module HMAC class SHA256 < Base def initialize(key = nil) super(Digest::SHA256, 64, 32, key) end public_class_method :new, :digest, :hexdigest end class SHA384 < Base def initialize(key = nil) super(Digest::SHA384, 128, 48, key) end public_class_method :new, :digest, :hexdigest end class SHA512 < Base def initialize(key = nil) super(Digest::SHA512, 128, 64, key) end public_class_method :new, :digest, :hexdigest end end ruby-hmac-0.4.0/lib/hmac-md5.rb0000644000175000017500000000031411561276015016006 0ustar vasudevvasudevrequire 'hmac' require 'digest/md5' module HMAC class MD5 < Base def initialize(key = nil) super(Digest::MD5, 64, 16, key) end public_class_method :new, :digest, :hexdigest end end ruby-hmac-0.4.0/lib/ruby_hmac.rb0000644000175000017500000000006411561276015016366 0ustar vasudevvasudev# Convenience file to match gem name require 'hmac' ruby-hmac-0.4.0/History.txt0000644000175000017500000000063011561276015015523 0ustar vasudevvasudev== 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] * Move hoe to build dependency if build deps are supported [Blaine Cook] == 0.3.1 / 2007-08-13 * Converted to gem by Geoffrey Grosenbach * Tests converted to Test::Unit ruby-hmac-0.4.0/README.txt0000644000175000017500000000300711561276015015020 0ustar vasudevvasudev= ruby-hmac * http://ruby-hmac.rubyforge.org == DESCRIPTION: This module provides common interface to HMAC functionality. HMAC is a kind of "Message Authentication Code" (MAC) algorithm whose standard is documented in RFC2104. Namely, a MAC provides a way to check the integrity of information transmitted over or stored in an unreliable medium, based on a secret key. Originally written by Daiki Ueno. Converted to a RubyGem by Geoffrey Grosenbach == LICENSE: (The MIT License) Copyright (c) 2007 Daiki Ueno Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-hmac-0.4.0/test/0000755000175000017500000000000011561276015014301 5ustar vasudevvasudevruby-hmac-0.4.0/test/test_hmac.rb0000755000175000017500000000442211561276015016602 0ustar vasudevvasudev#!/usr/bin/env ruby $: << File.dirname(__FILE__) + "/../lib" require "hmac-md5" require "hmac-sha1" begin require "minitest/unit" rescue LoadError require "rubygems" require "minitest/unit" end MiniTest::Unit.autorun class TestHmac < MiniTest::Unit::TestCase 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