/usr/lib/ruby/1.9.1/digest/hmac.rb is in libruby1.9.1 1.9.3.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | # == License
#
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
#
# Documentation by Akinori MUSHA
#
# All rights reserved. You can redistribute and/or modify it under
# the same terms as Ruby.
#
# $Id: hmac.rb 31594 2011-05-16 20:52:55Z drbrain $
#
warn "use of the experimetal library 'digest/hmac' is discouraged; require 'openssl' and use OpenSSL::HMAC instead." if $VERBOSE
require 'digest'
module Digest
# = digest/hmac.rb
#
# An experimental implementation of HMAC keyed-hashing algorithm
#
# == Overview
#
# CAUTION: Use of this library is discouraged, because this
# implementation was meant to be experimental but somehow got into the
# 1.9 series without being noticed. Please use OpenSSL::HMAC in the
# "openssl" library instead.
#
# == Examples
#
# require 'digest/hmac'
#
# # one-liner example
# puts Digest::HMAC.hexdigest("data", "hash key", Digest::SHA1)
#
# # rather longer one
# hmac = Digest::HMAC.new("foo", Digest::RMD160)
#
# buf = ""
# while stream.read(16384, buf)
# hmac.update(buf)
# end
#
# puts hmac.bubblebabble
#
class HMAC < Digest::Class
# Creates a Digest::HMAC instance.
def initialize(key, digester)
@md = digester.new
block_len = @md.block_length
if key.bytesize > block_len
key = @md.digest(key)
end
ipad = Array.new(block_len, 0x36)
opad = Array.new(block_len, 0x5c)
key.bytes.each_with_index { |c, i|
ipad[i] ^= c
opad[i] ^= c
}
@key = key.freeze
@ipad = ipad.pack('C*').freeze
@opad = opad.pack('C*').freeze
@md.update(@ipad)
end
def initialize_copy(other) # :nodoc:
@md = other.instance_eval { @md.clone }
end
# call-seq:
# hmac.update(string) -> hmac
# hmac << string -> hmac
#
# Updates the hmac using a given +string+ and returns self.
def update(text)
@md.update(text)
self
end
alias << update
# call-seq:
# hmac.reset -> hmac
#
# Resets the hmac to the initial state and returns self.
def reset
@md.reset
@md.update(@ipad)
self
end
def finish # :nodoc:
d = @md.digest!
@md.update(@opad)
@md.update(d)
@md.digest!
end
private :finish
# call-seq:
# hmac.digest_length -> Integer
#
# Returns the length in bytes of the hash value of the digest.
def digest_length
@md.digest_length
end
# call-seq:
# hmac.block_length -> Integer
#
# Returns the block length in bytes of the hmac.
def block_length
@md.block_length
end
# call-seq:
# hmac.inspect -> string
#
# Creates a printable version of the hmac object.
def inspect
sprintf('#<%s: key=%s, digest=%s>', self.class.name, @key.inspect, @md.inspect.sub(/^\#<(.*)>$/) { $1 });
end
end
end
if $0 == __FILE__
eval DATA.gets(nil), nil, $0, DATA.lineno
end
__END__
require 'test/unit'
module TM_HMAC
def test_s_hexdigest
cases.each { |h|
digesters.each { |d|
assert_equal(h[:hexdigest], Digest::HMAC.hexdigest(h[:data], h[:key], d))
}
}
end
def test_hexdigest
cases.each { |h|
digesters.each { |d|
hmac = Digest::HMAC.new(h[:key], d)
hmac.update(h[:data])
assert_equal(h[:hexdigest], hmac.hexdigest)
}
}
end
def test_reset
cases.each { |h|
digesters.each { |d|
hmac = Digest::HMAC.new(h[:key], d)
hmac.update("test")
hmac.reset
hmac.update(h[:data])
assert_equal(h[:hexdigest], hmac.hexdigest)
}
}
end
end
class TC_HMAC_MD5 < Test::Unit::TestCase
include TM_HMAC
def digesters
[Digest::MD5, Digest::MD5.new]
end
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
def cases
[
{
:key => "\x0b" * 16,
:data => "Hi There",
:hexdigest => "9294727a3638bb1c13f48ef8158bfc9d",
}, {
:key => "Jefe",
:data => "what do ya want for nothing?",
:hexdigest => "750c783e6ab0b503eaa86e310a5db738",
}, {
:key => "\xaa" * 16,
:data => "\xdd" * 50,
:hexdigest => "56be34521d144c88dbb8c733f0e8b3f6",
}, {
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
:data => "\xcd" * 50,
:hexdigest => "697eaf0aca3a3aea3a75164746ffaa79",
}, {
:key => "\x0c" * 16,
:data => "Test With Truncation",
:hexdigest => "56461ef2342edc00f9bab995690efd4c",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
:hexdigest => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
:hexdigest => "6f630fad67cda0ee1fb1f562db3aa53e",
}
]
end
end
class TC_HMAC_SHA1 < Test::Unit::TestCase
include TM_HMAC
def digesters
[Digest::SHA1, Digest::SHA1.new]
end
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
def cases
[
{
:key => "\x0b" * 20,
:data => "Hi There",
:hexdigest => "b617318655057264e28bc0b6fb378c8ef146be00",
}, {
:key => "Jefe",
:data => "what do ya want for nothing?",
:hexdigest => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
}, {
:key => "\xaa" * 20,
:data => "\xdd" * 50,
:hexdigest => "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
}, {
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
:data => "\xcd" * 50,
:hexdigest => "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
}, {
:key => "\x0c" * 20,
:data => "Test With Truncation",
:hexdigest => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
:hexdigest => "aa4ae5e15272d00e95705637ce8a3b55ed402112",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
:hexdigest => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
}
]
end
end
class TC_HMAC_RMD160 < Test::Unit::TestCase
include TM_HMAC
def digesters
[Digest::RMD160, Digest::RMD160.new]
end
# Taken from RFC 2286: Test Cases for HMAC-RIPEMD160 and HMAC-RIPEMD128
def cases
[
{
:key => "\x0b" * 20,
:data => "Hi There",
:hexdigest => "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668",
}, {
:key => "Jefe",
:data => "what do ya want for nothing?",
:hexdigest => "dda6c0213a485a9e24f4742064a7f033b43c4069",
}, {
:key => "\xaa" * 20,
:data => "\xdd" * 50,
:hexdigest => "b0b105360de759960ab4f35298e116e295d8e7c1",
}, {
:key => "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
:data => "\xcd" * 50,
:hexdigest => "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4",
}, {
:key => "\x0c" * 20,
:data => "Test With Truncation",
:hexdigest => "7619693978f91d90539ae786500ff3d8e0518e39",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
:hexdigest => "6466ca07ac5eac29e1bd523e5ada7605b791fd8b",
}, {
:key => "\xaa" * 80,
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
:hexdigest => "69ea60798d71616cce5fd0871e23754cd75d5a0a",
}
]
end
end
|