ruby-hmac 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.3.1 / 2007-08-13
2
+
3
+ * Converted to gem by Geoffrey Grosenbach
4
+ * Tests converted to Test::Unit
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ lib
3
+ lib/hmac-md5.rb
4
+ lib/hmac-rmd160.rb
5
+ lib/hmac-sha1.rb
6
+ lib/hmac-sha2.rb
7
+ lib/hmac.rb
8
+ Manifest.txt
9
+ Rakefile
10
+ README.txt
11
+ test/
12
+ 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,15 @@
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
@@ -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,115 @@
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
+ module HMAC
15
+
16
+ VERSION = '0.3.1'
17
+
18
+ class Base
19
+ def initialize(algorithm, block_size, output_length, key)
20
+ @algorithm = algorithm
21
+ @block_size = block_size
22
+ @output_length = output_length
23
+ @status = STATUS_UNDEFINED
24
+ @key_xor_ipad = ''
25
+ @key_xor_opad = ''
26
+ set_key(key) unless key.nil?
27
+ end
28
+
29
+ private
30
+ def check_status
31
+ unless @status == STATUS_INITIALIZED
32
+ raise RuntimeError,
33
+ "The underlying hash algorithm has not yet been initialized."
34
+ end
35
+ end
36
+
37
+ public
38
+ def set_key(key)
39
+ # If key is longer than the block size, apply hash function
40
+ # to key and use the result as a real key.
41
+ key = @algorithm.digest(key) if key.size > @block_size
42
+ key_xor_ipad = "\x36" * @block_size
43
+ key_xor_opad = "\x5C" * @block_size
44
+ for i in 0 .. key.size - 1
45
+ key_xor_ipad[i] ^= key[i]
46
+ key_xor_opad[i] ^= key[i]
47
+ end
48
+ @key_xor_ipad = key_xor_ipad
49
+ @key_xor_opad = key_xor_opad
50
+ @md = @algorithm.new
51
+ @status = STATUS_INITIALIZED
52
+ end
53
+
54
+ def reset_key
55
+ @key_xor_ipad.gsub!(/./, '?')
56
+ @key_xor_opad.gsub!(/./, '?')
57
+ @key_xor_ipad[0..-1] = ''
58
+ @key_xor_opad[0..-1] = ''
59
+ @status = STATUS_UNDEFINED
60
+ end
61
+
62
+ def update(text)
63
+ check_status
64
+ # perform inner H
65
+ md = @algorithm.new
66
+ md.update(@key_xor_ipad)
67
+ md.update(text)
68
+ str = md.digest
69
+ # perform outer H
70
+ md = @algorithm.new
71
+ md.update(@key_xor_opad)
72
+ md.update(str)
73
+ @md = md
74
+ end
75
+ alias << update
76
+
77
+ def digest
78
+ check_status
79
+ @md.digest
80
+ end
81
+
82
+ def hexdigest
83
+ check_status
84
+ @md.hexdigest
85
+ end
86
+ alias to_s hexdigest
87
+
88
+ # These two class methods below are safer than using above
89
+ # instance methods combinatorially because an instance will have
90
+ # held a key even if it's no longer in use.
91
+ def Base.digest(key, text)
92
+ begin
93
+ hmac = self.new(key)
94
+ hmac.update(text)
95
+ hmac.digest
96
+ ensure
97
+ hmac.reset_key
98
+ end
99
+ end
100
+
101
+ def Base.hexdigest(key, text)
102
+ begin
103
+ hmac = self.new(key)
104
+ hmac.update(text)
105
+ hmac.hexdigest
106
+ ensure
107
+ hmac.reset_key
108
+ end
109
+ end
110
+
111
+ private_class_method :new, :digest, :hexdigest
112
+ end
113
+
114
+ STATUS_UNDEFINED, STATUS_INITIALIZED = 0, 1
115
+ 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,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: ruby-hmac
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.3.1
7
+ date: 2007-08-13 00:00:00 -07:00
8
+ summary: An implementation of the HMAC authentication code in Ruby.
9
+ require_paths:
10
+ - lib
11
+ email: boss@topfunky.com
12
+ homepage: http://ruby-hmac.rubyforge.org
13
+ rubyforge_project: ruby-hmac
14
+ 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.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daiki Ueno
31
+ - Geoffrey Grosenbach
32
+ files:
33
+ - History.txt
34
+ - lib
35
+ - lib/hmac-md5.rb
36
+ - lib/hmac-rmd160.rb
37
+ - lib/hmac-sha1.rb
38
+ - lib/hmac-sha2.rb
39
+ - lib/hmac.rb
40
+ - Manifest.txt
41
+ - Rakefile
42
+ - README.txt
43
+ - test/
44
+ - test/test_hmac.rb
45
+ test_files:
46
+ - test/test_hmac.rb
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ extra_rdoc_files:
51
+ - History.txt
52
+ - Manifest.txt
53
+ - README.txt
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies:
61
+ - !ruby/object:Gem::Dependency
62
+ name: hoe
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.2
69
+ version: