topfunky-ruby-hmac 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ == 0.3.2 / 2008-08-20
2
+
3
+ * Removed spurious constants that cause warnings on load [Blaine Cook]
4
+ * Move hoe to build dependency if build deps are supported [Blaine Cook]
5
+
6
+ == 0.3.1 / 2007-08-13
7
+
8
+ * Converted to gem by Geoffrey Grosenbach
9
+ * Tests converted to Test::Unit
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/hmac-md5.rb
6
+ lib/hmac-rmd160.rb
7
+ lib/hmac-sha1.rb
8
+ lib/hmac-sha2.rb
9
+ lib/hmac.rb
10
+ test/test_hmac.rb
@@ -0,0 +1,35 @@
1
+ ruby-hmac
2
+ by Daiki Ueno
3
+ http://ruby-hmac.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ 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.
8
+
9
+ Originally written by Daiki Ueno. Converted to a RubyGem by Geoffrey Grosenbach
10
+
11
+ == LICENSE:
12
+
13
+ (The MIT License)
14
+
15
+ Copyright (c) 2007 Daiki Ueno
16
+
17
+ Permission is hereby granted, free of charge, to any person obtaining
18
+ a copy of this software and associated documentation files (the
19
+ 'Software'), to deal in the Software without restriction, including
20
+ without limitation the rights to use, copy, modify, merge, publish,
21
+ distribute, sublicense, and/or sell copies of the Software, and to
22
+ permit persons to whom the Software is furnished to do so, subject to
23
+ the following conditions:
24
+
25
+ The above copyright notice and this permission notice shall be
26
+ included in all copies or substantial portions of the Software.
27
+
28
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
29
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
32
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
33
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
34
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
+
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ $:.unshift(File.dirname(__FILE__) + "/lib")
4
+ require 'hmac'
5
+
6
+ Hoe.new('ruby-hmac', HMAC::VERSION) do |p|
7
+ p.name = "ruby-hmac"
8
+ p.author = ["Daiki Ueno", "Geoffrey Grosenbach"]
9
+ p.email = 'boss@topfunky.com'
10
+ p.summary = "An implementation of the HMAC authentication code in Ruby."
11
+ 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."
12
+ p.url = "http://ruby-hmac.rubyforge.org"
13
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
14
+ p.remote_rdoc_dir = '' # Release to root
15
+ end
16
+
17
+ desc "Simple require on packaged files to make sure they are all there"
18
+ task :verify => :package do
19
+ # An error message will be displayed if files are missing
20
+ if system %(ruby -e "require 'pkg/ruby-hmac-#{HMAC::VERSION}/lib/hmac'")
21
+ puts "\nThe library files are present"
22
+ end
23
+ end
24
+
25
+ task :release => :verify
@@ -0,0 +1,11 @@
1
+ require 'hmac'
2
+ require 'digest/md5'
3
+
4
+ module HMAC
5
+ class MD5 < Base
6
+ def initialize(key = nil)
7
+ super(Digest::MD5, 64, 16, key)
8
+ end
9
+ public_class_method :new, :digest, :hexdigest
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'hmac'
2
+ require 'digest/rmd160'
3
+
4
+ module HMAC
5
+ class RMD160 < Base
6
+ def initialize(key = nil)
7
+ super(Digest::RMD160, 64, 20, key)
8
+ end
9
+ public_class_method :new, :digest, :hexdigest
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'hmac'
2
+ require 'digest/sha1'
3
+
4
+ module HMAC
5
+ class SHA1 < Base
6
+ def initialize(key = nil)
7
+ super(Digest::SHA1, 64, 20, key)
8
+ end
9
+ public_class_method :new, :digest, :hexdigest
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'hmac'
2
+ require 'digest/sha2'
3
+
4
+ module HMAC
5
+ class SHA256 < Base
6
+ def initialize(key = nil)
7
+ super(Digest::SHA256, 64, 32, key)
8
+ end
9
+ public_class_method :new, :digest, :hexdigest
10
+ end
11
+
12
+ class SHA384 < Base
13
+ def initialize(key = nil)
14
+ super(Digest::SHA384, 128, 48, key)
15
+ end
16
+ public_class_method :new, :digest, :hexdigest
17
+ end
18
+
19
+ class SHA512 < Base
20
+ def initialize(key = nil)
21
+ super(Digest::SHA512, 128, 64, key)
22
+ end
23
+ public_class_method :new, :digest, :hexdigest
24
+ end
25
+ end
@@ -0,0 +1,117 @@
1
+ # Copyright (C) 2001 Daiki Ueno <ueno@unixuser.org>
2
+ # This library is distributed under the terms of the Ruby license.
3
+
4
+ # This module provides common interface to HMAC engines.
5
+ # HMAC standard is documented in RFC 2104:
6
+ #
7
+ # H. Krawczyk et al., "HMAC: Keyed-Hashing for Message Authentication",
8
+ # RFC 2104, February 1997
9
+ #
10
+ # These APIs are inspired by JCE 1.2's javax.crypto.Mac interface.
11
+ #
12
+ # <URL:http://java.sun.com/security/JCE1.2/spec/apidoc/javax/crypto/Mac.html>
13
+ #
14
+ # Source repository is at
15
+ #
16
+ # http://github.com/topfunky/ruby-hmac/tree/master
17
+
18
+ module HMAC
19
+
20
+ VERSION = '0.3.2'
21
+
22
+ class Base
23
+ def initialize(algorithm, block_size, output_length, key)
24
+ @algorithm = algorithm
25
+ @block_size = block_size
26
+ @output_length = output_length
27
+ @initialized = false
28
+ @key_xor_ipad = ''
29
+ @key_xor_opad = ''
30
+ set_key(key) unless key.nil?
31
+ end
32
+
33
+ private
34
+ def check_status
35
+ unless @initialized
36
+ raise RuntimeError,
37
+ "The underlying hash algorithm has not yet been initialized."
38
+ end
39
+ end
40
+
41
+ public
42
+ def set_key(key)
43
+ # If key is longer than the block size, apply hash function
44
+ # to key and use the result as a real key.
45
+ key = @algorithm.digest(key) if key.size > @block_size
46
+ key_xor_ipad = "\x36" * @block_size
47
+ key_xor_opad = "\x5C" * @block_size
48
+ for i in 0 .. key.size - 1
49
+ key_xor_ipad[i] ^= key[i]
50
+ key_xor_opad[i] ^= key[i]
51
+ end
52
+ @key_xor_ipad = key_xor_ipad
53
+ @key_xor_opad = key_xor_opad
54
+ @md = @algorithm.new
55
+ @initialized = true
56
+ end
57
+
58
+ def reset_key
59
+ @key_xor_ipad.gsub!(/./, '?')
60
+ @key_xor_opad.gsub!(/./, '?')
61
+ @key_xor_ipad[0..-1] = ''
62
+ @key_xor_opad[0..-1] = ''
63
+ @initialized = false
64
+ end
65
+
66
+ def update(text)
67
+ check_status
68
+ # perform inner H
69
+ md = @algorithm.new
70
+ md.update(@key_xor_ipad)
71
+ md.update(text)
72
+ str = md.digest
73
+ # perform outer H
74
+ md = @algorithm.new
75
+ md.update(@key_xor_opad)
76
+ md.update(str)
77
+ @md = md
78
+ end
79
+ alias << update
80
+
81
+ def digest
82
+ check_status
83
+ @md.digest
84
+ end
85
+
86
+ def hexdigest
87
+ check_status
88
+ @md.hexdigest
89
+ end
90
+ alias to_s hexdigest
91
+
92
+ # These two class methods below are safer than using above
93
+ # instance methods combinatorially because an instance will have
94
+ # held a key even if it's no longer in use.
95
+ def Base.digest(key, text)
96
+ begin
97
+ hmac = self.new(key)
98
+ hmac.update(text)
99
+ hmac.digest
100
+ ensure
101
+ hmac.reset_key
102
+ end
103
+ end
104
+
105
+ def Base.hexdigest(key, text)
106
+ begin
107
+ hmac = self.new(key)
108
+ hmac.update(text)
109
+ hmac.hexdigest
110
+ ensure
111
+ hmac.reset_key
112
+ end
113
+ end
114
+
115
+ private_class_method :new, :digest, :hexdigest
116
+ end
117
+ end
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+
5
+ require 'hmac-md5'
6
+ require 'hmac-sha1'
7
+
8
+ class TestHMAC < Test::Unit::TestCase
9
+
10
+ def test_s_digest
11
+ key = "\x0b" * 16
12
+ text = "Hi There"
13
+
14
+ hmac = HMAC::MD5.new(key)
15
+ hmac.update(text)
16
+
17
+ assert_equal(hmac.digest, HMAC::MD5.digest(key, text))
18
+ end
19
+
20
+ def test_s_hexdigest
21
+ key = "\x0b" * 16
22
+ text = "Hi There"
23
+
24
+ hmac = HMAC::MD5.new(key)
25
+ hmac.update(text)
26
+
27
+ assert_equal(hmac.hexdigest, HMAC::MD5.hexdigest(key, text))
28
+ end
29
+
30
+ def test_hmac_md5_1
31
+ assert_equal(HMAC::MD5.hexdigest("\x0b" * 16, "Hi There"),
32
+ "9294727a3638bb1c13f48ef8158bfc9d")
33
+ end
34
+
35
+ def test_hmac_md5_2
36
+ assert_equal(HMAC::MD5.hexdigest("Jefe", "what do ya want for nothing?"),
37
+ "750c783e6ab0b503eaa86e310a5db738")
38
+ end
39
+
40
+ def test_hmac_md5_3
41
+ assert_equal(HMAC::MD5.hexdigest("\xaa" * 16, "\xdd" * 50),
42
+ "56be34521d144c88dbb8c733f0e8b3f6")
43
+ end
44
+
45
+ def test_hmac_md5_4
46
+ assert_equal(HMAC::MD5.hexdigest(["0102030405060708090a0b0c0d0e0f10111213141516171819"].pack("H*"), "\xcd" * 50),
47
+ "697eaf0aca3a3aea3a75164746ffaa79")
48
+ end
49
+
50
+ def test_hmac_md5_5
51
+ assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
52
+ "56461ef2342edc00f9bab995690efd4c")
53
+ end
54
+
55
+ # def test_hmac_md5_6
56
+ # assert_equal(HMAC::MD5.hexdigest("\x0c" * 16, "Test With Truncation"),
57
+ # "56461ef2342edc00f9bab995")
58
+ # end
59
+
60
+ def test_hmac_md5_7
61
+ assert_equal(HMAC::MD5.hexdigest("\xaa" * 80, "Test Using Larger Than Block-Size Key - Hash Key First"),
62
+ "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
63
+ end
64
+
65
+ def test_hmac_md5_8
66
+ assert_equal(HMAC::MD5.hexdigest("\xaa" * 80, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
67
+ "6f630fad67cda0ee1fb1f562db3aa53e")
68
+ end
69
+
70
+ def test_reset_key
71
+ hmac = HMAC::MD5.new("key")
72
+ assert_nothing_raised {
73
+ hmac.reset_key
74
+ }
75
+ assert_raise(RuntimeError) {
76
+ hmac.update("foo")
77
+ }
78
+ end
79
+
80
+ def test_set_key
81
+ hmac = HMAC::MD5.new
82
+ assert_raise(RuntimeError) {
83
+ hmac.update("foo")
84
+ }
85
+ assert_nothing_raised {
86
+ hmac.reset_key
87
+ }
88
+ assert_raise(RuntimeError) {
89
+ hmac.update("foo")
90
+ }
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: topfunky-ruby-hmac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Daiki Ueno
8
+ - Geoffrey Grosenbach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-08-21 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ 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.
26
+ email: boss@topfunky.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.txt
39
+ - Rakefile
40
+ - lib/hmac-md5.rb
41
+ - lib/hmac-rmd160.rb
42
+ - lib/hmac-sha1.rb
43
+ - lib/hmac-sha2.rb
44
+ - lib/hmac.rb
45
+ - test/test_hmac.rb
46
+ has_rdoc: true
47
+ homepage: http://ruby-hmac.rubyforge.org
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: ruby-hmac
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: An implementation of the HMAC authentication code in Ruby.
73
+ test_files:
74
+ - test/test_hmac.rb