entangledstate-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.
- data/History.txt +9 -0
- data/Manifest.txt +10 -0
- data/README.txt +35 -0
- data/Rakefile +25 -0
- data/lib/hmac-md5.rb +11 -0
- data/lib/hmac-rmd160.rb +11 -0
- data/lib/hmac-sha1.rb +11 -0
- data/lib/hmac-sha2.rb +25 -0
- data/lib/hmac.rb +118 -0
- data/test/test_hmac.rb +92 -0
- metadata +75 -0
data/History.txt
ADDED
@@ -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
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -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
|
+
|
data/Rakefile
ADDED
@@ -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
|
data/lib/hmac-md5.rb
ADDED
data/lib/hmac-rmd160.rb
ADDED
data/lib/hmac-sha1.rb
ADDED
data/lib/hmac-sha2.rb
ADDED
@@ -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
|
data/lib/hmac.rb
ADDED
@@ -0,0 +1,118 @@
|
|
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
|
+
akey = key.unpack("C*")
|
47
|
+
key_xor_ipad = ("\x36" * @block_size).unpack("C*")
|
48
|
+
key_xor_opad = ("\x5C" * @block_size).unpack("C*")
|
49
|
+
for i in 0 .. akey.size - 1
|
50
|
+
key_xor_ipad[i] ^= akey[i]
|
51
|
+
key_xor_opad[i] ^= akey[i]
|
52
|
+
end
|
53
|
+
@key_xor_ipad = key_xor_ipad.pack("C*")
|
54
|
+
@key_xor_opad = key_xor_opad.pack("C*")
|
55
|
+
@md = @algorithm.new
|
56
|
+
@initialized = true
|
57
|
+
end
|
58
|
+
|
59
|
+
def reset_key
|
60
|
+
@key_xor_ipad.gsub!(/./, '?')
|
61
|
+
@key_xor_opad.gsub!(/./, '?')
|
62
|
+
@key_xor_ipad[0..-1] = ''
|
63
|
+
@key_xor_opad[0..-1] = ''
|
64
|
+
@initialized = false
|
65
|
+
end
|
66
|
+
|
67
|
+
def update(text)
|
68
|
+
check_status
|
69
|
+
# perform inner H
|
70
|
+
md = @algorithm.new
|
71
|
+
md.update(@key_xor_ipad)
|
72
|
+
md.update(text)
|
73
|
+
str = md.digest
|
74
|
+
# perform outer H
|
75
|
+
md = @algorithm.new
|
76
|
+
md.update(@key_xor_opad)
|
77
|
+
md.update(str)
|
78
|
+
@md = md
|
79
|
+
end
|
80
|
+
alias << update
|
81
|
+
|
82
|
+
def digest
|
83
|
+
check_status
|
84
|
+
@md.digest
|
85
|
+
end
|
86
|
+
|
87
|
+
def hexdigest
|
88
|
+
check_status
|
89
|
+
@md.hexdigest
|
90
|
+
end
|
91
|
+
alias to_s hexdigest
|
92
|
+
|
93
|
+
# These two class methods below are safer than using above
|
94
|
+
# instance methods combinatorially because an instance will have
|
95
|
+
# held a key even if it's no longer in use.
|
96
|
+
def Base.digest(key, text)
|
97
|
+
hmac = self.new(key)
|
98
|
+
begin
|
99
|
+
hmac.update(text)
|
100
|
+
hmac.digest
|
101
|
+
ensure
|
102
|
+
hmac.reset_key
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def Base.hexdigest(key, text)
|
107
|
+
hmac = self.new(key)
|
108
|
+
begin
|
109
|
+
hmac.update(text)
|
110
|
+
hmac.hexdigest
|
111
|
+
ensure
|
112
|
+
hmac.reset_key
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
private_class_method :new, :digest, :hexdigest
|
117
|
+
end
|
118
|
+
end
|
data/test/test_hmac.rb
ADDED
@@ -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,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: entangledstate-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
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.7.0
|
25
|
+
version:
|
26
|
+
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.
|
27
|
+
email: boss@topfunky.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/hmac-md5.rb
|
42
|
+
- lib/hmac-rmd160.rb
|
43
|
+
- lib/hmac-sha1.rb
|
44
|
+
- lib/hmac-sha2.rb
|
45
|
+
- lib/hmac.rb
|
46
|
+
- test/test_hmac.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://ruby-hmac.rubyforge.org
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --main
|
52
|
+
- README.txt
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: ruby-hmac
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: An implementation of the HMAC authentication code in Ruby.
|
74
|
+
test_files:
|
75
|
+
- test/test_hmac.rb
|