rubysl-digest 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/.travis.yml +7 -0
- data/README.md +2 -2
- data/Rakefile +0 -1
- data/ext/rubysl/digest/bubblebabble/.gitignore +2 -0
- data/ext/rubysl/digest/bubblebabble/bubblebabble.c +147 -0
- data/ext/rubysl/digest/bubblebabble/bubblebabble.h +2 -0
- data/ext/rubysl/digest/bubblebabble/depend +3 -0
- data/ext/rubysl/digest/bubblebabble/extconf.h +4 -0
- data/ext/rubysl/digest/bubblebabble/extconf.rb +6 -0
- data/ext/rubysl/digest/defs.h +19 -0
- data/ext/rubysl/digest/digest.c +660 -0
- data/ext/rubysl/digest/digest.h +32 -0
- data/ext/rubysl/digest/extconf.rb +10 -0
- data/ext/rubysl/digest/md5/.gitignore +2 -0
- data/ext/rubysl/digest/md5/extconf.rb +29 -0
- data/ext/rubysl/digest/md5/md5.c +422 -0
- data/ext/rubysl/digest/md5/md5.h +80 -0
- data/ext/rubysl/digest/md5/md5init.c +40 -0
- data/ext/rubysl/digest/md5/md5ossl.c +9 -0
- data/ext/rubysl/digest/md5/md5ossl.h +13 -0
- data/ext/rubysl/digest/rmd160/.gitignore +2 -0
- data/ext/rubysl/digest/rmd160/extconf.rb +28 -0
- data/ext/rubysl/digest/rmd160/rmd160.c +457 -0
- data/ext/rubysl/digest/rmd160/rmd160.h +56 -0
- data/ext/rubysl/digest/rmd160/rmd160init.c +40 -0
- data/ext/rubysl/digest/rmd160/rmd160ossl.c +8 -0
- data/ext/rubysl/digest/rmd160/rmd160ossl.h +19 -0
- data/ext/rubysl/digest/sha1/.gitignore +2 -0
- data/ext/rubysl/digest/sha1/extconf.rb +28 -0
- data/ext/rubysl/digest/sha1/sha1.c +269 -0
- data/ext/rubysl/digest/sha1/sha1.h +39 -0
- data/ext/rubysl/digest/sha1/sha1init.c +40 -0
- data/ext/rubysl/digest/sha1/sha1ossl.c +10 -0
- data/ext/rubysl/digest/sha1/sha1ossl.h +20 -0
- data/ext/rubysl/digest/sha2/.gitignore +2 -0
- data/ext/rubysl/digest/sha2/extconf.rb +24 -0
- data/ext/rubysl/digest/sha2/sha2.c +919 -0
- data/ext/rubysl/digest/sha2/sha2.h +109 -0
- data/ext/rubysl/digest/sha2/sha2init.c +52 -0
- data/lib/digest/bubblebabble.rb +1 -0
- data/lib/digest/hmac.rb +302 -0
- data/lib/digest/md5.rb +23 -0
- data/lib/digest/rmd160.rb +1 -0
- data/lib/digest/sha1.rb +23 -0
- data/lib/digest/sha2.rb +74 -0
- data/lib/digest.rb +1 -0
- data/lib/rubysl/digest/digest.rb +88 -0
- data/lib/{rubysl-digest → rubysl/digest}/version.rb +1 -1
- data/lib/rubysl/digest.rb +2 -0
- data/rubysl-digest.gemspec +25 -17
- data/spec/hexencode_spec.rb +30 -0
- data/spec/md5/append_spec.rb +6 -0
- data/spec/md5/block_length_spec.rb +11 -0
- data/spec/md5/digest_bang_spec.rb +12 -0
- data/spec/md5/digest_length_spec.rb +11 -0
- data/spec/md5/digest_spec.rb +31 -0
- data/spec/md5/equal_spec.rb +37 -0
- data/spec/md5/file_spec.rb +42 -0
- data/spec/md5/hexdigest_bang_spec.rb +13 -0
- data/spec/md5/hexdigest_spec.rb +31 -0
- data/spec/md5/inspect_spec.rb +11 -0
- data/spec/md5/length_spec.rb +7 -0
- data/spec/md5/reset_spec.rb +14 -0
- data/spec/md5/shared/constants.rb +16 -0
- data/spec/md5/shared/length.rb +8 -0
- data/spec/md5/shared/sample.rb +15 -0
- data/spec/md5/shared/update.rb +7 -0
- data/spec/md5/size_spec.rb +7 -0
- data/spec/md5/to_s_spec.rb +21 -0
- data/spec/md5/update_spec.rb +6 -0
- data/spec/sha1/digest_spec.rb +19 -0
- data/spec/sha1/file_spec.rb +42 -0
- data/spec/sha1/shared/constants.rb +16 -0
- data/spec/sha256/append_spec.rb +6 -0
- data/spec/sha256/block_length_spec.rb +11 -0
- data/spec/sha256/digest_bang_spec.rb +12 -0
- data/spec/sha256/digest_length_spec.rb +11 -0
- data/spec/sha256/digest_spec.rb +31 -0
- data/spec/sha256/equal_spec.rb +36 -0
- data/spec/sha256/file_spec.rb +42 -0
- data/spec/sha256/hexdigest_bang_spec.rb +13 -0
- data/spec/sha256/hexdigest_spec.rb +31 -0
- data/spec/sha256/inspect_spec.rb +11 -0
- data/spec/sha256/length_spec.rb +7 -0
- data/spec/sha256/reset_spec.rb +14 -0
- data/spec/sha256/shared/constants.rb +16 -0
- data/spec/sha256/shared/length.rb +8 -0
- data/spec/sha256/shared/update.rb +7 -0
- data/spec/sha256/size_spec.rb +7 -0
- data/spec/sha256/to_s_spec.rb +20 -0
- data/spec/sha256/update_spec.rb +6 -0
- data/spec/sha384/append_spec.rb +6 -0
- data/spec/sha384/block_length_spec.rb +11 -0
- data/spec/sha384/digest_bang_spec.rb +12 -0
- data/spec/sha384/digest_length_spec.rb +11 -0
- data/spec/sha384/digest_spec.rb +31 -0
- data/spec/sha384/equal_spec.rb +36 -0
- data/spec/sha384/file_spec.rb +42 -0
- data/spec/sha384/hexdigest_bang_spec.rb +13 -0
- data/spec/sha384/hexdigest_spec.rb +31 -0
- data/spec/sha384/inspect_spec.rb +11 -0
- data/spec/sha384/length_spec.rb +7 -0
- data/spec/sha384/reset_spec.rb +14 -0
- data/spec/sha384/shared/constants.rb +17 -0
- data/spec/sha384/shared/length.rb +8 -0
- data/spec/sha384/shared/update.rb +7 -0
- data/spec/sha384/size_spec.rb +7 -0
- data/spec/sha384/to_s_spec.rb +20 -0
- data/spec/sha384/update_spec.rb +6 -0
- data/spec/sha512/append_spec.rb +6 -0
- data/spec/sha512/block_length_spec.rb +11 -0
- data/spec/sha512/digest_bang_spec.rb +12 -0
- data/spec/sha512/digest_length_spec.rb +11 -0
- data/spec/sha512/digest_spec.rb +31 -0
- data/spec/sha512/equal_spec.rb +36 -0
- data/spec/sha512/file_spec.rb +42 -0
- data/spec/sha512/hexdigest_bang_spec.rb +13 -0
- data/spec/sha512/hexdigest_spec.rb +31 -0
- data/spec/sha512/inspect_spec.rb +11 -0
- data/spec/sha512/length_spec.rb +7 -0
- data/spec/sha512/reset_spec.rb +14 -0
- data/spec/sha512/shared/constants.rb +16 -0
- data/spec/sha512/shared/length.rb +8 -0
- data/spec/sha512/shared/update.rb +7 -0
- data/spec/sha512/size_spec.rb +7 -0
- data/spec/sha512/to_s_spec.rb +20 -0
- data/spec/sha512/update_spec.rb +6 -0
- metadata +283 -88
- data/lib/rubysl-digest.rb +0 -7
@@ -0,0 +1,109 @@
|
|
1
|
+
/*
|
2
|
+
* sha2.h
|
3
|
+
*
|
4
|
+
* Version 1.0.0beta1
|
5
|
+
*
|
6
|
+
* Written by Aaron D. Gifford <me@aarongifford.com>
|
7
|
+
*
|
8
|
+
* Copyright 2000 Aaron D. Gifford. All rights reserved.
|
9
|
+
*
|
10
|
+
* Redistribution and use in source and binary forms, with or without
|
11
|
+
* modification, are permitted provided that the following conditions
|
12
|
+
* are met:
|
13
|
+
* 1. Redistributions of source code must retain the above copyright
|
14
|
+
* notice, this list of conditions and the following disclaimer.
|
15
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
* notice, this list of conditions and the following disclaimer in the
|
17
|
+
* documentation and/or other materials provided with the distribution.
|
18
|
+
* 3. Neither the name of the copyright holder nor the names of contributors
|
19
|
+
* may be used to endorse or promote products derived from this software
|
20
|
+
* without specific prior written permission.
|
21
|
+
*
|
22
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
|
23
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
|
26
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
28
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
29
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
30
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
31
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
32
|
+
* SUCH DAMAGE.
|
33
|
+
*
|
34
|
+
*/
|
35
|
+
|
36
|
+
/* $RoughId: sha2.h,v 1.3 2002/02/24 08:14:32 knu Exp $ */
|
37
|
+
/* $Id: sha2.h 11708 2007-02-12 23:01:19Z shyouhei $ */
|
38
|
+
|
39
|
+
#ifndef __SHA2_H__
|
40
|
+
#define __SHA2_H__
|
41
|
+
|
42
|
+
#ifdef __cplusplus
|
43
|
+
extern "C" {
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#include "defs.h"
|
47
|
+
|
48
|
+
|
49
|
+
/*** SHA-256/384/512 Various Length Definitions ***********************/
|
50
|
+
#define SHA256_BLOCK_LENGTH 64
|
51
|
+
#define SHA256_DIGEST_LENGTH 32
|
52
|
+
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
|
53
|
+
#define SHA384_BLOCK_LENGTH 128
|
54
|
+
#define SHA384_DIGEST_LENGTH 48
|
55
|
+
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
|
56
|
+
#define SHA512_BLOCK_LENGTH 128
|
57
|
+
#define SHA512_DIGEST_LENGTH 64
|
58
|
+
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
|
59
|
+
|
60
|
+
|
61
|
+
/*** SHA-256/384/512 Context Structures *******************************/
|
62
|
+
|
63
|
+
typedef struct _SHA256_CTX {
|
64
|
+
uint32_t state[8];
|
65
|
+
uint64_t bitcount;
|
66
|
+
uint8_t buffer[SHA256_BLOCK_LENGTH];
|
67
|
+
} SHA256_CTX;
|
68
|
+
typedef struct _SHA512_CTX {
|
69
|
+
uint64_t state[8];
|
70
|
+
uint64_t bitcount[2];
|
71
|
+
uint8_t buffer[SHA512_BLOCK_LENGTH];
|
72
|
+
} SHA512_CTX;
|
73
|
+
|
74
|
+
typedef SHA512_CTX SHA384_CTX;
|
75
|
+
|
76
|
+
|
77
|
+
#ifdef RUBY
|
78
|
+
#define SHA256_Init rb_Digest_SHA256_Init
|
79
|
+
#define SHA256_Update rb_Digest_SHA256_Update
|
80
|
+
#define SHA256_Finish rb_Digest_SHA256_Finish
|
81
|
+
|
82
|
+
#define SHA384_Init rb_Digest_SHA384_Init
|
83
|
+
#define SHA384_Update rb_Digest_SHA384_Update
|
84
|
+
#define SHA384_Finish rb_Digest_SHA384_Finish
|
85
|
+
|
86
|
+
#define SHA512_Init rb_Digest_SHA512_Init
|
87
|
+
#define SHA512_Update rb_Digest_SHA512_Update
|
88
|
+
#define SHA512_Finish rb_Digest_SHA512_Finish
|
89
|
+
#endif
|
90
|
+
|
91
|
+
/*** SHA-256/384/512 Function Prototypes ******************************/
|
92
|
+
void SHA256_Init _((SHA256_CTX *));
|
93
|
+
void SHA256_Update _((SHA256_CTX*, const uint8_t*, size_t));
|
94
|
+
void SHA256_Finish _((SHA256_CTX*, uint8_t[SHA256_DIGEST_LENGTH]));
|
95
|
+
|
96
|
+
void SHA384_Init _((SHA384_CTX*));
|
97
|
+
void SHA384_Update _((SHA384_CTX*, const uint8_t*, size_t));
|
98
|
+
void SHA384_Finish _((SHA384_CTX*, uint8_t[SHA384_DIGEST_LENGTH]));
|
99
|
+
|
100
|
+
void SHA512_Init _((SHA512_CTX*));
|
101
|
+
void SHA512_Update _((SHA512_CTX*, const uint8_t*, size_t));
|
102
|
+
void SHA512_Finish _((SHA512_CTX*, uint8_t[SHA512_DIGEST_LENGTH]));
|
103
|
+
|
104
|
+
#ifdef __cplusplus
|
105
|
+
}
|
106
|
+
#endif /* __cplusplus */
|
107
|
+
|
108
|
+
#endif /* __SHA2_H__ */
|
109
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/* $RoughId: sha2init.c,v 1.3 2001/07/13 20:00:43 knu Exp $ */
|
2
|
+
/* $Id: sha2init.c 19020 2008-09-01 07:48:53Z nobu $ */
|
3
|
+
|
4
|
+
#include "digest.h"
|
5
|
+
#include "sha2.h"
|
6
|
+
|
7
|
+
#define FOREACH_BITLEN(func) func(256) func(384) func(512)
|
8
|
+
|
9
|
+
#define DEFINE_ALGO_METADATA(bitlen) \
|
10
|
+
static const rb_digest_metadata_t sha##bitlen = { \
|
11
|
+
RUBY_DIGEST_API_VERSION, \
|
12
|
+
SHA##bitlen##_DIGEST_LENGTH, \
|
13
|
+
SHA##bitlen##_BLOCK_LENGTH, \
|
14
|
+
sizeof(SHA##bitlen##_CTX), \
|
15
|
+
(rb_digest_hash_init_func_t)SHA##bitlen##_Init, \
|
16
|
+
(rb_digest_hash_update_func_t)SHA##bitlen##_Update, \
|
17
|
+
(rb_digest_hash_finish_func_t)SHA##bitlen##_Finish, \
|
18
|
+
};
|
19
|
+
|
20
|
+
FOREACH_BITLEN(DEFINE_ALGO_METADATA)
|
21
|
+
|
22
|
+
/*
|
23
|
+
* Classes for calculating message digests using the SHA-256/384/512
|
24
|
+
* Secure Hash Algorithm(s) by NIST (the US' National Institute of
|
25
|
+
* Standards and Technology), described in FIPS PUB 180-2.
|
26
|
+
*/
|
27
|
+
void
|
28
|
+
Init_sha2()
|
29
|
+
{
|
30
|
+
VALUE mDigest, cDigest_Base;
|
31
|
+
ID id_metadata;
|
32
|
+
|
33
|
+
#define DECLARE_ALGO_CLASS(bitlen) \
|
34
|
+
VALUE cDigest_SHA##bitlen;
|
35
|
+
|
36
|
+
FOREACH_BITLEN(DECLARE_ALGO_CLASS)
|
37
|
+
|
38
|
+
rb_require("digest");
|
39
|
+
|
40
|
+
id_metadata = rb_intern("metadata");
|
41
|
+
|
42
|
+
mDigest = rb_path2class("Digest");
|
43
|
+
cDigest_Base = rb_path2class("Digest::Base");
|
44
|
+
|
45
|
+
#define DEFINE_ALGO_CLASS(bitlen) \
|
46
|
+
cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
|
47
|
+
\
|
48
|
+
rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
|
49
|
+
Data_Wrap_Struct(rb_cObject, 0, 0, (void *)&sha##bitlen));
|
50
|
+
|
51
|
+
FOREACH_BITLEN(DEFINE_ALGO_CLASS)
|
52
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'digest/bubblebabble/bubblebabble'
|
data/lib/digest/hmac.rb
ADDED
@@ -0,0 +1,302 @@
|
|
1
|
+
# == License
|
2
|
+
#
|
3
|
+
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
|
4
|
+
#
|
5
|
+
# Documentation by Akinori MUSHA
|
6
|
+
#
|
7
|
+
# All rights reserved. You can redistribute and/or modify it under
|
8
|
+
# the same terms as Ruby.
|
9
|
+
#
|
10
|
+
# $Id$
|
11
|
+
#
|
12
|
+
|
13
|
+
warn "use of the experimetal library 'digest/hmac' is discouraged; require 'openssl' and use OpenSSL::HMAC instead." if $VERBOSE
|
14
|
+
|
15
|
+
require 'digest'
|
16
|
+
|
17
|
+
module Digest
|
18
|
+
# = digest/hmac.rb
|
19
|
+
#
|
20
|
+
# An experimental implementation of HMAC keyed-hashing algorithm
|
21
|
+
#
|
22
|
+
# == Overview
|
23
|
+
#
|
24
|
+
# CAUTION: Use of this library is discouraged, because this
|
25
|
+
# implementation was meant to be experimental but somehow got into the
|
26
|
+
# 1.9 series without being noticed. Please use OpenSSL::HMAC in the
|
27
|
+
# "openssl" library instead.
|
28
|
+
#
|
29
|
+
# == Examples
|
30
|
+
#
|
31
|
+
# require 'digest/hmac'
|
32
|
+
#
|
33
|
+
# # one-liner example
|
34
|
+
# puts Digest::HMAC.hexdigest("data", "hash key", Digest::SHA1)
|
35
|
+
#
|
36
|
+
# # rather longer one
|
37
|
+
# hmac = Digest::HMAC.new("foo", Digest::RMD160)
|
38
|
+
#
|
39
|
+
# buf = ""
|
40
|
+
# while stream.read(16384, buf)
|
41
|
+
# hmac.update(buf)
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# puts hmac.bubblebabble
|
45
|
+
#
|
46
|
+
class HMAC < Digest::Class
|
47
|
+
|
48
|
+
# Creates a Digest::HMAC instance.
|
49
|
+
|
50
|
+
def initialize(key, digester)
|
51
|
+
@md = digester.new
|
52
|
+
|
53
|
+
block_len = @md.block_length
|
54
|
+
|
55
|
+
if key.bytesize > block_len
|
56
|
+
key = @md.digest(key)
|
57
|
+
end
|
58
|
+
|
59
|
+
ipad = Array.new(block_len, 0x36)
|
60
|
+
opad = Array.new(block_len, 0x5c)
|
61
|
+
|
62
|
+
key.bytes.each_with_index { |c, i|
|
63
|
+
ipad[i] ^= c
|
64
|
+
opad[i] ^= c
|
65
|
+
}
|
66
|
+
|
67
|
+
@key = key.freeze
|
68
|
+
@ipad = ipad.pack('C*').freeze
|
69
|
+
@opad = opad.pack('C*').freeze
|
70
|
+
@md.update(@ipad)
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize_copy(other) # :nodoc:
|
74
|
+
@md = other.instance_eval { @md.clone }
|
75
|
+
end
|
76
|
+
|
77
|
+
# call-seq:
|
78
|
+
# hmac.update(string) -> hmac
|
79
|
+
# hmac << string -> hmac
|
80
|
+
#
|
81
|
+
# Updates the hmac using a given +string+ and returns self.
|
82
|
+
def update(text)
|
83
|
+
@md.update(text)
|
84
|
+
self
|
85
|
+
end
|
86
|
+
alias << update
|
87
|
+
|
88
|
+
# call-seq:
|
89
|
+
# hmac.reset -> hmac
|
90
|
+
#
|
91
|
+
# Resets the hmac to the initial state and returns self.
|
92
|
+
def reset
|
93
|
+
@md.reset
|
94
|
+
@md.update(@ipad)
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def finish # :nodoc:
|
99
|
+
d = @md.digest!
|
100
|
+
@md.update(@opad)
|
101
|
+
@md.update(d)
|
102
|
+
@md.digest!
|
103
|
+
end
|
104
|
+
private :finish
|
105
|
+
|
106
|
+
# call-seq:
|
107
|
+
# hmac.digest_length -> Integer
|
108
|
+
#
|
109
|
+
# Returns the length in bytes of the hash value of the digest.
|
110
|
+
def digest_length
|
111
|
+
@md.digest_length
|
112
|
+
end
|
113
|
+
|
114
|
+
# call-seq:
|
115
|
+
# hmac.block_length -> Integer
|
116
|
+
#
|
117
|
+
# Returns the block length in bytes of the hmac.
|
118
|
+
def block_length
|
119
|
+
@md.block_length
|
120
|
+
end
|
121
|
+
|
122
|
+
# call-seq:
|
123
|
+
# hmac.inspect -> string
|
124
|
+
#
|
125
|
+
# Creates a printable version of the hmac object.
|
126
|
+
def inspect
|
127
|
+
sprintf('#<%s: key=%s, digest=%s>', self.class.name, @key.inspect, @md.inspect.sub(/^\#<(.*)>$/) { $1 });
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
if $0 == __FILE__
|
133
|
+
eval DATA.gets(nil), nil, $0, DATA.lineno
|
134
|
+
end
|
135
|
+
|
136
|
+
__END__
|
137
|
+
|
138
|
+
require 'test/unit'
|
139
|
+
|
140
|
+
module TM_HMAC
|
141
|
+
def test_s_hexdigest
|
142
|
+
cases.each { |h|
|
143
|
+
digesters.each { |d|
|
144
|
+
assert_equal(h[:hexdigest], Digest::HMAC.hexdigest(h[:data], h[:key], d))
|
145
|
+
}
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_hexdigest
|
150
|
+
cases.each { |h|
|
151
|
+
digesters.each { |d|
|
152
|
+
hmac = Digest::HMAC.new(h[:key], d)
|
153
|
+
|
154
|
+
hmac.update(h[:data])
|
155
|
+
|
156
|
+
assert_equal(h[:hexdigest], hmac.hexdigest)
|
157
|
+
}
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_reset
|
162
|
+
cases.each { |h|
|
163
|
+
digesters.each { |d|
|
164
|
+
hmac = Digest::HMAC.new(h[:key], d)
|
165
|
+
hmac.update("test")
|
166
|
+
hmac.reset
|
167
|
+
hmac.update(h[:data])
|
168
|
+
|
169
|
+
assert_equal(h[:hexdigest], hmac.hexdigest)
|
170
|
+
}
|
171
|
+
}
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class TC_HMAC_MD5 < Test::Unit::TestCase
|
176
|
+
include TM_HMAC
|
177
|
+
|
178
|
+
def digesters
|
179
|
+
[Digest::MD5, Digest::MD5.new]
|
180
|
+
end
|
181
|
+
|
182
|
+
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
|
183
|
+
def cases
|
184
|
+
[
|
185
|
+
{
|
186
|
+
:key => "\x0b" * 16,
|
187
|
+
:data => "Hi There",
|
188
|
+
:hexdigest => "9294727a3638bb1c13f48ef8158bfc9d",
|
189
|
+
}, {
|
190
|
+
:key => "Jefe",
|
191
|
+
:data => "what do ya want for nothing?",
|
192
|
+
:hexdigest => "750c783e6ab0b503eaa86e310a5db738",
|
193
|
+
}, {
|
194
|
+
:key => "\xaa" * 16,
|
195
|
+
:data => "\xdd" * 50,
|
196
|
+
:hexdigest => "56be34521d144c88dbb8c733f0e8b3f6",
|
197
|
+
}, {
|
198
|
+
: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",
|
199
|
+
:data => "\xcd" * 50,
|
200
|
+
:hexdigest => "697eaf0aca3a3aea3a75164746ffaa79",
|
201
|
+
}, {
|
202
|
+
:key => "\x0c" * 16,
|
203
|
+
:data => "Test With Truncation",
|
204
|
+
:hexdigest => "56461ef2342edc00f9bab995690efd4c",
|
205
|
+
}, {
|
206
|
+
:key => "\xaa" * 80,
|
207
|
+
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
208
|
+
:hexdigest => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
|
209
|
+
}, {
|
210
|
+
:key => "\xaa" * 80,
|
211
|
+
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
212
|
+
:hexdigest => "6f630fad67cda0ee1fb1f562db3aa53e",
|
213
|
+
}
|
214
|
+
]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class TC_HMAC_SHA1 < Test::Unit::TestCase
|
219
|
+
include TM_HMAC
|
220
|
+
|
221
|
+
def digesters
|
222
|
+
[Digest::SHA1, Digest::SHA1.new]
|
223
|
+
end
|
224
|
+
|
225
|
+
# Taken from RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
|
226
|
+
def cases
|
227
|
+
[
|
228
|
+
{
|
229
|
+
:key => "\x0b" * 20,
|
230
|
+
:data => "Hi There",
|
231
|
+
:hexdigest => "b617318655057264e28bc0b6fb378c8ef146be00",
|
232
|
+
}, {
|
233
|
+
:key => "Jefe",
|
234
|
+
:data => "what do ya want for nothing?",
|
235
|
+
:hexdigest => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
|
236
|
+
}, {
|
237
|
+
:key => "\xaa" * 20,
|
238
|
+
:data => "\xdd" * 50,
|
239
|
+
:hexdigest => "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
|
240
|
+
}, {
|
241
|
+
: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",
|
242
|
+
:data => "\xcd" * 50,
|
243
|
+
:hexdigest => "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
|
244
|
+
}, {
|
245
|
+
:key => "\x0c" * 20,
|
246
|
+
:data => "Test With Truncation",
|
247
|
+
:hexdigest => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
|
248
|
+
}, {
|
249
|
+
:key => "\xaa" * 80,
|
250
|
+
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
251
|
+
:hexdigest => "aa4ae5e15272d00e95705637ce8a3b55ed402112",
|
252
|
+
}, {
|
253
|
+
:key => "\xaa" * 80,
|
254
|
+
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
255
|
+
:hexdigest => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
|
256
|
+
}
|
257
|
+
]
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
class TC_HMAC_RMD160 < Test::Unit::TestCase
|
262
|
+
include TM_HMAC
|
263
|
+
|
264
|
+
def digesters
|
265
|
+
[Digest::RMD160, Digest::RMD160.new]
|
266
|
+
end
|
267
|
+
|
268
|
+
# Taken from RFC 2286: Test Cases for HMAC-RIPEMD160 and HMAC-RIPEMD128
|
269
|
+
def cases
|
270
|
+
[
|
271
|
+
{
|
272
|
+
:key => "\x0b" * 20,
|
273
|
+
:data => "Hi There",
|
274
|
+
:hexdigest => "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668",
|
275
|
+
}, {
|
276
|
+
:key => "Jefe",
|
277
|
+
:data => "what do ya want for nothing?",
|
278
|
+
:hexdigest => "dda6c0213a485a9e24f4742064a7f033b43c4069",
|
279
|
+
}, {
|
280
|
+
:key => "\xaa" * 20,
|
281
|
+
:data => "\xdd" * 50,
|
282
|
+
:hexdigest => "b0b105360de759960ab4f35298e116e295d8e7c1",
|
283
|
+
}, {
|
284
|
+
: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",
|
285
|
+
:data => "\xcd" * 50,
|
286
|
+
:hexdigest => "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4",
|
287
|
+
}, {
|
288
|
+
:key => "\x0c" * 20,
|
289
|
+
:data => "Test With Truncation",
|
290
|
+
:hexdigest => "7619693978f91d90539ae786500ff3d8e0518e39",
|
291
|
+
}, {
|
292
|
+
:key => "\xaa" * 80,
|
293
|
+
:data => "Test Using Larger Than Block-Size Key - Hash Key First",
|
294
|
+
:hexdigest => "6466ca07ac5eac29e1bd523e5ada7605b791fd8b",
|
295
|
+
}, {
|
296
|
+
:key => "\xaa" * 80,
|
297
|
+
:data => "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
|
298
|
+
:hexdigest => "69ea60798d71616cce5fd0871e23754cd75d5a0a",
|
299
|
+
}
|
300
|
+
]
|
301
|
+
end
|
302
|
+
end
|
data/lib/digest/md5.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# just for compatibility; requiring "md5" is obsoleted
|
2
|
+
#
|
3
|
+
# $RoughId: md5.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
|
4
|
+
# $Id: md5.rb 12008 2007-03-06 10:12:12Z knu $
|
5
|
+
|
6
|
+
require 'digest/md5/md5'
|
7
|
+
|
8
|
+
class MD5 < Digest::MD5
|
9
|
+
class << self
|
10
|
+
alias orig_new new
|
11
|
+
def new(str = nil)
|
12
|
+
if str
|
13
|
+
orig_new.update(str)
|
14
|
+
else
|
15
|
+
orig_new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def md5(*args)
|
20
|
+
new(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'digest/rmd160/rmd160'
|
data/lib/digest/sha1.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# just for compatibility; requiring "sha1" is obsoleted
|
2
|
+
#
|
3
|
+
# $RoughId: sha1.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
|
4
|
+
# $Id: sha1.rb 12008 2007-03-06 10:12:12Z knu $
|
5
|
+
|
6
|
+
require 'digest/sha1/sha1'
|
7
|
+
|
8
|
+
class SHA1 < Digest::SHA1
|
9
|
+
class << self
|
10
|
+
alias orig_new new
|
11
|
+
def new(str = nil)
|
12
|
+
if str
|
13
|
+
orig_new.update(str)
|
14
|
+
else
|
15
|
+
orig_new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def sha1(*args)
|
20
|
+
new(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/digest/sha2.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
#--
|
2
|
+
# sha2.rb - defines Digest::SHA2 class which wraps up the SHA256,
|
3
|
+
# SHA384, and SHA512 classes.
|
4
|
+
#++
|
5
|
+
# Copyright (c) 2006 Akinori MUSHA <knu@iDaemons.org>
|
6
|
+
#
|
7
|
+
# All rights reserved. You can redistribute and/or modify it under the same
|
8
|
+
# terms as Ruby.
|
9
|
+
#
|
10
|
+
# $Id: sha2.rb 11708 2007-02-12 23:01:19Z shyouhei $
|
11
|
+
|
12
|
+
require 'digest'
|
13
|
+
require 'digest/sha2/sha2'
|
14
|
+
|
15
|
+
module Digest
|
16
|
+
#
|
17
|
+
# A meta digest provider class for SHA256, SHA384 and SHA512.
|
18
|
+
#
|
19
|
+
class SHA2 < Digest::Class
|
20
|
+
# call-seq:
|
21
|
+
# Digest::SHA2.new(bitlen = 256) -> digest_obj
|
22
|
+
#
|
23
|
+
# Creates a new SHA2 hash object with a given bit length.
|
24
|
+
def initialize(bitlen = 256)
|
25
|
+
case bitlen
|
26
|
+
when 256
|
27
|
+
@sha2 = Digest::SHA256.new
|
28
|
+
when 384
|
29
|
+
@sha2 = Digest::SHA384.new
|
30
|
+
when 512
|
31
|
+
@sha2 = Digest::SHA512.new
|
32
|
+
else
|
33
|
+
raise ArgumentError, "unsupported bit length: %s" % bitlen.inspect
|
34
|
+
end
|
35
|
+
@bitlen = bitlen
|
36
|
+
end
|
37
|
+
|
38
|
+
# :nodoc:
|
39
|
+
def reset
|
40
|
+
@sha2.reset
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
# :nodoc:
|
45
|
+
def update(str)
|
46
|
+
@sha2.update(str)
|
47
|
+
self
|
48
|
+
end
|
49
|
+
alias << update
|
50
|
+
|
51
|
+
def finish
|
52
|
+
@sha2.digest!
|
53
|
+
end
|
54
|
+
private :finish
|
55
|
+
|
56
|
+
def block_length
|
57
|
+
@sha2.block_length
|
58
|
+
end
|
59
|
+
|
60
|
+
def digest_length
|
61
|
+
@sha2.digest_length
|
62
|
+
end
|
63
|
+
|
64
|
+
# :nodoc:
|
65
|
+
def initialize_copy(other)
|
66
|
+
@sha2 = other.instance_eval { @sha2.clone }
|
67
|
+
end
|
68
|
+
|
69
|
+
# :nodoc:
|
70
|
+
def inspect
|
71
|
+
"#<%s:%d %s>" % [self.class.name, @bitlen, hexdigest]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/digest.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rubysl/digest"
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'digest/digest'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
def self.const_missing(name) # :nodoc:
|
5
|
+
case name
|
6
|
+
when :SHA256, :SHA384, :SHA512
|
7
|
+
lib = 'digest/sha2'
|
8
|
+
else
|
9
|
+
lib = File.join('digest', name.to_s.downcase)
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require lib
|
14
|
+
rescue LoadError
|
15
|
+
raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
|
16
|
+
end
|
17
|
+
unless Digest.const_defined?(name)
|
18
|
+
raise NameError, "uninitialized constant Digest::#{name}", caller(1)
|
19
|
+
end
|
20
|
+
Digest.const_get(name)
|
21
|
+
end
|
22
|
+
|
23
|
+
class ::Digest::Class
|
24
|
+
# creates a digest object and reads a given file, _name_.
|
25
|
+
#
|
26
|
+
# p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
|
27
|
+
# # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
|
28
|
+
def self.file(name)
|
29
|
+
new.file(name)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the base64 encoded hash value of a given _string_. The
|
33
|
+
# return value is properly padded with '=' and contains no line
|
34
|
+
# feeds.
|
35
|
+
def self.base64digest(str, *args)
|
36
|
+
[digest(str, *args)].pack('m0')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module Instance
|
41
|
+
# updates the digest with the contents of a given file _name_ and
|
42
|
+
# returns self.
|
43
|
+
def file(name)
|
44
|
+
File.open(name, "rb") {|f|
|
45
|
+
buf = ""
|
46
|
+
while f.read(16384, buf)
|
47
|
+
update buf
|
48
|
+
end
|
49
|
+
}
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
# If none is given, returns the resulting hash value of the digest
|
54
|
+
# in a base64 encoded form, keeping the digest's state.
|
55
|
+
#
|
56
|
+
# If a +string+ is given, returns the hash value for the given
|
57
|
+
# +string+ in a base64 encoded form, resetting the digest to the
|
58
|
+
# initial state before and after the process.
|
59
|
+
#
|
60
|
+
# In either case, the return value is properly padded with '=' and
|
61
|
+
# contains no line feeds.
|
62
|
+
def base64digest(str = nil)
|
63
|
+
[str ? digest(str) : digest].pack('m0')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns the resulting hash value and resets the digest to the
|
67
|
+
# initial state.
|
68
|
+
def base64digest!
|
69
|
+
[digest!].pack('m0')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# call-seq:
|
75
|
+
# Digest(name) -> digest_subclass
|
76
|
+
#
|
77
|
+
# Returns a Digest subclass by +name+.
|
78
|
+
#
|
79
|
+
# require 'digest'
|
80
|
+
#
|
81
|
+
# Digest("MD5")
|
82
|
+
# # => Digest::MD5
|
83
|
+
#
|
84
|
+
# Digest("Foo")
|
85
|
+
# # => LoadError: library not found for class Digest::Foo -- digest/foo
|
86
|
+
def Digest(name)
|
87
|
+
Digest.const_get(name)
|
88
|
+
end
|