digest 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 +12 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +15 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/digest.gemspec +45 -0
- data/ext/digest/bubblebabble/bubblebabble.c +147 -0
- data/ext/digest/bubblebabble/extconf.rb +6 -0
- data/ext/digest/defs.h +19 -0
- data/ext/digest/digest.c +821 -0
- data/ext/digest/digest.h +64 -0
- data/ext/digest/digest_conf.rb +17 -0
- data/ext/digest/extconf.rb +11 -0
- data/ext/digest/md5/extconf.rb +19 -0
- data/ext/digest/md5/md5.c +424 -0
- data/ext/digest/md5/md5.h +80 -0
- data/ext/digest/md5/md5cc.h +19 -0
- data/ext/digest/md5/md5init.c +64 -0
- data/ext/digest/rmd160/extconf.rb +19 -0
- data/ext/digest/rmd160/rmd160.c +463 -0
- data/ext/digest/rmd160/rmd160.h +56 -0
- data/ext/digest/rmd160/rmd160init.c +60 -0
- data/ext/digest/sha1/extconf.rb +19 -0
- data/ext/digest/sha1/sha1.c +271 -0
- data/ext/digest/sha1/sha1.h +39 -0
- data/ext/digest/sha1/sha1cc.h +14 -0
- data/ext/digest/sha1/sha1init.c +66 -0
- data/ext/digest/sha2/extconf.rb +21 -0
- data/ext/digest/sha2/lib/sha2.rb +142 -0
- data/ext/digest/sha2/sha2.c +1081 -0
- data/ext/digest/sha2/sha2.h +225 -0
- data/ext/digest/sha2/sha2cc.h +31 -0
- data/ext/digest/sha2/sha2init.c +55 -0
- data/ext/digest/test.sh +30 -0
- data/ext/openssl/deprecation.rb +23 -0
- data/lib/digest.rb +109 -0
- metadata +132 -0
@@ -0,0 +1,225 @@
|
|
1
|
+
/*
|
2
|
+
* FILE: sha2.h
|
3
|
+
* AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
|
4
|
+
*
|
5
|
+
* Copyright (c) 2000-2001, Aaron D. Gifford
|
6
|
+
* All rights reserved.
|
7
|
+
*
|
8
|
+
* Redistribution and use in source and binary forms, with or without
|
9
|
+
* modification, are permitted provided that the following conditions
|
10
|
+
* are met:
|
11
|
+
* 1. Redistributions of source code must retain the above copyright
|
12
|
+
* notice, this list of conditions and the following disclaimer.
|
13
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
14
|
+
* notice, this list of conditions and the following disclaimer in the
|
15
|
+
* documentation and/or other materials provided with the distribution.
|
16
|
+
* 3. Neither the name of the copyright holder nor the names of contributors
|
17
|
+
* may be used to endorse or promote products derived from this software
|
18
|
+
* without specific prior written permission.
|
19
|
+
*
|
20
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
|
21
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
23
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
|
24
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
26
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
27
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
28
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
29
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
30
|
+
* SUCH DAMAGE.
|
31
|
+
*
|
32
|
+
* $OrigId: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
|
33
|
+
* $RoughId: sha2.h,v 1.3 2002/02/24 08:14:32 knu Exp $
|
34
|
+
* $Id$
|
35
|
+
*/
|
36
|
+
|
37
|
+
#ifndef __SHA2_H__
|
38
|
+
#define __SHA2_H__
|
39
|
+
|
40
|
+
#ifdef __cplusplus
|
41
|
+
extern "C" {
|
42
|
+
#endif
|
43
|
+
|
44
|
+
|
45
|
+
/*
|
46
|
+
* Import u_intXX_t size_t type definitions from system headers. You
|
47
|
+
* may need to change this, or define these things yourself in this
|
48
|
+
* file.
|
49
|
+
*/
|
50
|
+
#include <sys/types.h>
|
51
|
+
|
52
|
+
#ifdef RUBY
|
53
|
+
# ifdef HAVE_PROTOTYPES
|
54
|
+
# undef NOPROTO
|
55
|
+
# else
|
56
|
+
# define NOPROTO
|
57
|
+
# endif /* HAVE_PROTOTYPES */
|
58
|
+
# ifndef BYTE_ORDER
|
59
|
+
# define LITTLE_ENDIAN 1234
|
60
|
+
# define BIG_ENDIAN 4321
|
61
|
+
# ifdef WORDS_BIGENDIAN
|
62
|
+
# define BYTE_ORDER BIG_ENDIAN
|
63
|
+
# else
|
64
|
+
# define BYTE_ORDER LITTLE_ENDIAN
|
65
|
+
# endif
|
66
|
+
# endif /* BYTE_ORDER */
|
67
|
+
# define SHA2_USE_INTTYPES_H
|
68
|
+
#else /* RUBY */
|
69
|
+
#ifdef SHA2_USE_INTTYPES_H
|
70
|
+
|
71
|
+
#include <inttypes.h>
|
72
|
+
|
73
|
+
#endif /* SHA2_USE_INTTYPES_H */
|
74
|
+
#endif /* RUBY */
|
75
|
+
|
76
|
+
|
77
|
+
/*** SHA-256/384/512 Various Length Definitions ***********************/
|
78
|
+
#define SHA256_BLOCK_LENGTH 64
|
79
|
+
#define SHA256_DIGEST_LENGTH 32
|
80
|
+
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
|
81
|
+
#define SHA384_BLOCK_LENGTH 128
|
82
|
+
#define SHA384_DIGEST_LENGTH 48
|
83
|
+
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
|
84
|
+
#define SHA512_BLOCK_LENGTH 128
|
85
|
+
#define SHA512_DIGEST_LENGTH 64
|
86
|
+
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
|
87
|
+
|
88
|
+
|
89
|
+
/*** SHA-256/384/512 Context Structures *******************************/
|
90
|
+
/* NOTE: If your architecture does not define either u_intXX_t types or
|
91
|
+
* uintXX_t (from inttypes.h), you may need to define things by hand
|
92
|
+
* for your system:
|
93
|
+
*/
|
94
|
+
#ifndef SHA2_USE_INTTYPES_H
|
95
|
+
# ifdef HAVE_U_INT8_T
|
96
|
+
typedef u_int8_t uint8_t; /* 1-byte (8-bits) */
|
97
|
+
typedef u_int32_t uint32_t; /* 4-bytes (32-bits) */
|
98
|
+
typedef u_int64_t uint64_t; /* 8-bytes (64-bits) */
|
99
|
+
# else
|
100
|
+
typedef unsigned char uint8_t; /* 1-byte (8-bits) */
|
101
|
+
typedef unsigned int uint32_t; /* 4-bytes (32-bits) */
|
102
|
+
typedef unsigned long long uint64_t; /* 8-bytes (64-bits) */
|
103
|
+
# endif
|
104
|
+
#endif
|
105
|
+
|
106
|
+
/*
|
107
|
+
* Most BSD systems already define u_intXX_t types, as does Linux.
|
108
|
+
* Some systems, however, like Compaq's Tru64 Unix instead can use
|
109
|
+
* uintXX_t types defined by very recent ANSI C standards and included
|
110
|
+
* in the file:
|
111
|
+
*
|
112
|
+
* #include <inttypes.h>
|
113
|
+
*
|
114
|
+
* If you choose to use <inttypes.h> then please define:
|
115
|
+
*
|
116
|
+
* #define SHA2_USE_INTTYPES_H
|
117
|
+
*
|
118
|
+
* Or on the command line during compile:
|
119
|
+
*
|
120
|
+
* cc -DSHA2_USE_INTTYPES_H ...
|
121
|
+
*/
|
122
|
+
typedef struct _SHA256_CTX {
|
123
|
+
uint32_t state[8];
|
124
|
+
uint64_t bitcount;
|
125
|
+
uint8_t buffer[SHA256_BLOCK_LENGTH];
|
126
|
+
} SHA256_CTX;
|
127
|
+
typedef struct _SHA512_CTX {
|
128
|
+
uint64_t state[8];
|
129
|
+
uint64_t bitcount[2];
|
130
|
+
uint8_t buffer[SHA512_BLOCK_LENGTH];
|
131
|
+
} SHA512_CTX;
|
132
|
+
|
133
|
+
typedef SHA512_CTX SHA384_CTX;
|
134
|
+
|
135
|
+
|
136
|
+
/*** SHA-256/384/512 Function Prototypes ******************************/
|
137
|
+
#ifdef RUBY
|
138
|
+
#define SHA256_Init rb_Digest_SHA256_Init
|
139
|
+
#define SHA256_Update rb_Digest_SHA256_Update
|
140
|
+
#define SHA256_Finish rb_Digest_SHA256_Finish
|
141
|
+
#define SHA256_Data rb_Digest_SHA256_Data
|
142
|
+
#define SHA256_End rb_Digest_SHA256_End
|
143
|
+
#define SHA256_Last rb_Digest_SHA256_Last
|
144
|
+
#define SHA256_Transform rb_Digest_SHA256_Transform
|
145
|
+
#define SHA256_Final(d, c) SHA256_Finish(c, d)
|
146
|
+
|
147
|
+
#define SHA384_Init rb_Digest_SHA384_Init
|
148
|
+
#define SHA384_Update rb_Digest_SHA384_Update
|
149
|
+
#define SHA384_Finish rb_Digest_SHA384_Finish
|
150
|
+
#define SHA384_Data rb_Digest_SHA384_Data
|
151
|
+
#define SHA384_End rb_Digest_SHA384_End
|
152
|
+
#define SHA384_Last rb_Digest_SHA384_Last
|
153
|
+
#define SHA384_Transform rb_Digest_SHA384_Transform
|
154
|
+
#define SHA384_Final(d, c) SHA384_Finish(c, d)
|
155
|
+
|
156
|
+
#define SHA512_Init rb_Digest_SHA512_Init
|
157
|
+
#define SHA512_Update rb_Digest_SHA512_Update
|
158
|
+
#define SHA512_Finish rb_Digest_SHA512_Finish
|
159
|
+
#define SHA512_Data rb_Digest_SHA512_Data
|
160
|
+
#define SHA512_End rb_Digest_SHA512_End
|
161
|
+
#define SHA512_Last rb_Digest_SHA512_Last
|
162
|
+
#define SHA512_Transform rb_Digest_SHA512_Transform
|
163
|
+
#define SHA512_Final(d, c) SHA512_Finish(c, d)
|
164
|
+
#endif /* RUBY */
|
165
|
+
|
166
|
+
#ifndef NOPROTO
|
167
|
+
|
168
|
+
int SHA256_Init(SHA256_CTX *);
|
169
|
+
void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
|
170
|
+
int SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
|
171
|
+
char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
|
172
|
+
char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
|
173
|
+
|
174
|
+
int SHA384_Init(SHA384_CTX*);
|
175
|
+
void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
|
176
|
+
int SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
|
177
|
+
char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
|
178
|
+
char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
|
179
|
+
|
180
|
+
int SHA512_Init(SHA512_CTX*);
|
181
|
+
void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
|
182
|
+
int SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
|
183
|
+
char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
|
184
|
+
char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
|
185
|
+
|
186
|
+
#else /* NOPROTO */
|
187
|
+
|
188
|
+
int SHA256_Init();
|
189
|
+
void SHA256_Update();
|
190
|
+
#ifdef RUBY
|
191
|
+
int SHA256_Finish();
|
192
|
+
#else
|
193
|
+
int SHA256_Final();
|
194
|
+
#endif /* RUBY */
|
195
|
+
char* SHA256_End();
|
196
|
+
char* SHA256_Data();
|
197
|
+
|
198
|
+
int SHA384_Init();
|
199
|
+
void SHA384_Update();
|
200
|
+
#ifdef RUBY
|
201
|
+
int SHA384_Finish();
|
202
|
+
#else
|
203
|
+
int SHA384_Final();
|
204
|
+
#endif /* RUBY */
|
205
|
+
char* SHA384_End();
|
206
|
+
char* SHA384_Data();
|
207
|
+
|
208
|
+
int SHA512_Init();
|
209
|
+
void SHA512_Update();
|
210
|
+
#ifdef RUBY
|
211
|
+
int SHA512_Finish();
|
212
|
+
#else
|
213
|
+
int SHA512_Final();
|
214
|
+
#endif /* RUBY */
|
215
|
+
char* SHA512_End();
|
216
|
+
char* SHA512_Data();
|
217
|
+
|
218
|
+
#endif /* NOPROTO */
|
219
|
+
|
220
|
+
#ifdef __cplusplus
|
221
|
+
}
|
222
|
+
#endif /* __cplusplus */
|
223
|
+
|
224
|
+
#endif /* __SHA2_H__ */
|
225
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#define COMMON_DIGEST_FOR_OPENSSL 1
|
2
|
+
#include <CommonCrypto/CommonDigest.h>
|
3
|
+
|
4
|
+
#define SHA256_BLOCK_LENGTH CC_SHA256_BLOCK_BYTES
|
5
|
+
#define SHA384_BLOCK_LENGTH CC_SHA384_BLOCK_BYTES
|
6
|
+
#define SHA512_BLOCK_LENGTH CC_SHA512_BLOCK_BYTES
|
7
|
+
|
8
|
+
#define SHA384_CTX CC_SHA512_CTX
|
9
|
+
|
10
|
+
static DEFINE_UPDATE_FUNC_FOR_UINT(SHA256)
|
11
|
+
static DEFINE_FINISH_FUNC_FROM_FINAL(SHA256)
|
12
|
+
static DEFINE_UPDATE_FUNC_FOR_UINT(SHA384)
|
13
|
+
static DEFINE_FINISH_FUNC_FROM_FINAL(SHA384)
|
14
|
+
static DEFINE_UPDATE_FUNC_FOR_UINT(SHA512)
|
15
|
+
static DEFINE_FINISH_FUNC_FROM_FINAL(SHA512)
|
16
|
+
|
17
|
+
|
18
|
+
#undef SHA256_Update
|
19
|
+
#undef SHA256_Finish
|
20
|
+
#define SHA256_Update rb_digest_SHA256_update
|
21
|
+
#define SHA256_Finish rb_digest_SHA256_finish
|
22
|
+
|
23
|
+
#undef SHA384_Update
|
24
|
+
#undef SHA384_Finish
|
25
|
+
#define SHA384_Update rb_digest_SHA384_update
|
26
|
+
#define SHA384_Finish rb_digest_SHA384_finish
|
27
|
+
|
28
|
+
#undef SHA512_Update
|
29
|
+
#undef SHA512_Finish
|
30
|
+
#define SHA512_Update rb_digest_SHA512_update
|
31
|
+
#define SHA512_Finish rb_digest_SHA512_finish
|
@@ -0,0 +1,55 @@
|
|
1
|
+
/* $RoughId: sha2init.c,v 1.3 2001/07/13 20:00:43 knu Exp $ */
|
2
|
+
/* $Id$ */
|
3
|
+
|
4
|
+
#include <ruby/ruby.h>
|
5
|
+
#include "../digest.h"
|
6
|
+
#if defined(SHA2_USE_COMMONDIGEST)
|
7
|
+
#include "sha2cc.h"
|
8
|
+
#else
|
9
|
+
#include "sha2.h"
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#define FOREACH_BITLEN(func) func(256) func(384) func(512)
|
13
|
+
|
14
|
+
#define DEFINE_ALGO_METADATA(bitlen) \
|
15
|
+
static const rb_digest_metadata_t sha##bitlen = { \
|
16
|
+
RUBY_DIGEST_API_VERSION, \
|
17
|
+
SHA##bitlen##_DIGEST_LENGTH, \
|
18
|
+
SHA##bitlen##_BLOCK_LENGTH, \
|
19
|
+
sizeof(SHA##bitlen##_CTX), \
|
20
|
+
(rb_digest_hash_init_func_t)SHA##bitlen##_Init, \
|
21
|
+
(rb_digest_hash_update_func_t)SHA##bitlen##_Update, \
|
22
|
+
(rb_digest_hash_finish_func_t)SHA##bitlen##_Finish, \
|
23
|
+
};
|
24
|
+
|
25
|
+
FOREACH_BITLEN(DEFINE_ALGO_METADATA)
|
26
|
+
|
27
|
+
/*
|
28
|
+
* Classes for calculating message digests using the SHA-256/384/512
|
29
|
+
* Secure Hash Algorithm(s) by NIST (the US' National Institute of
|
30
|
+
* Standards and Technology), described in FIPS PUB 180-2.
|
31
|
+
*/
|
32
|
+
void
|
33
|
+
Init_sha2(void)
|
34
|
+
{
|
35
|
+
VALUE mDigest, cDigest_Base;
|
36
|
+
ID id_metadata = rb_id_metadata();
|
37
|
+
|
38
|
+
#define DECLARE_ALGO_CLASS(bitlen) \
|
39
|
+
VALUE cDigest_SHA##bitlen;
|
40
|
+
|
41
|
+
FOREACH_BITLEN(DECLARE_ALGO_CLASS)
|
42
|
+
|
43
|
+
mDigest = rb_digest_namespace();
|
44
|
+
cDigest_Base = rb_path2class("Digest::Base");
|
45
|
+
|
46
|
+
#define DEFINE_ALGO_CLASS(bitlen) \
|
47
|
+
cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
|
48
|
+
\
|
49
|
+
rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
|
50
|
+
Data_Wrap_Struct(0, 0, 0, (void *)&sha##bitlen));
|
51
|
+
|
52
|
+
#undef RUBY_UNTYPED_DATA_WARNING
|
53
|
+
#define RUBY_UNTYPED_DATA_WARNING 0
|
54
|
+
FOREACH_BITLEN(DEFINE_ALGO_CLASS)
|
55
|
+
}
|
data/ext/digest/test.sh
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
#
|
3
|
+
# $RoughId: test.sh,v 1.5 2001/07/13 15:38:27 knu Exp $
|
4
|
+
# $Id$
|
5
|
+
|
6
|
+
RUBY=${RUBY:=ruby}
|
7
|
+
MAKE=${MAKE:=make}
|
8
|
+
CFLAGS=${CFLAGS:=-Wall}
|
9
|
+
|
10
|
+
${RUBY} extconf.rb --with-cflags="${CFLAGS}"
|
11
|
+
${MAKE} clean
|
12
|
+
${MAKE}
|
13
|
+
|
14
|
+
for algo in md5 rmd160 sha1 sha2; do
|
15
|
+
args=--with-cflags="${CFLAGS}"
|
16
|
+
|
17
|
+
if [ $WITH_BUNDLED_ENGINES ]; then
|
18
|
+
args="$args --with-bundled-$algo"
|
19
|
+
fi
|
20
|
+
|
21
|
+
(cd $algo &&
|
22
|
+
${RUBY} extconf.rb $args;
|
23
|
+
${MAKE} clean;
|
24
|
+
${MAKE})
|
25
|
+
ln -sf ../../$algo/$algo.so lib/digest/
|
26
|
+
done
|
27
|
+
|
28
|
+
${RUBY} -I. -I./lib ../../test/digest/test_digest.rb
|
29
|
+
|
30
|
+
rm lib/digest/*.so
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
module OpenSSL
|
3
|
+
def self.deprecated_warning_flag
|
4
|
+
unless flag = (@deprecated_warning_flag ||= nil)
|
5
|
+
if try_compile("", flag = "-Werror=deprecated-declarations")
|
6
|
+
$warnflags << " #{flag}"
|
7
|
+
else
|
8
|
+
flag = ""
|
9
|
+
end
|
10
|
+
@deprecated_warning_flag = flag
|
11
|
+
end
|
12
|
+
flag
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.check_func(func, header)
|
16
|
+
have_func(func, header, deprecated_warning_flag)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.check_func_or_macro(func, header)
|
20
|
+
check_func(func, header) or
|
21
|
+
have_macro(func, header) && $defs.push("-DHAVE_#{func.upcase}")
|
22
|
+
end
|
23
|
+
end
|
data/lib/digest.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'digest.so'
|
3
|
+
|
4
|
+
module Digest
|
5
|
+
# A mutex for Digest().
|
6
|
+
REQUIRE_MUTEX = Thread::Mutex.new
|
7
|
+
|
8
|
+
def self.const_missing(name) # :nodoc:
|
9
|
+
case name
|
10
|
+
when :SHA256, :SHA384, :SHA512
|
11
|
+
lib = 'digest/sha2.so'
|
12
|
+
else
|
13
|
+
lib = File.join('digest', name.to_s.downcase)
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require lib
|
18
|
+
rescue LoadError
|
19
|
+
raise LoadError, "library not found for class Digest::#{name} -- #{lib}", caller(1)
|
20
|
+
end
|
21
|
+
unless Digest.const_defined?(name)
|
22
|
+
raise NameError, "uninitialized constant Digest::#{name}", caller(1)
|
23
|
+
end
|
24
|
+
Digest.const_get(name)
|
25
|
+
end
|
26
|
+
|
27
|
+
class ::Digest::Class
|
28
|
+
# Creates a digest object and reads a given file, _name_.
|
29
|
+
# Optional arguments are passed to the constructor of the digest
|
30
|
+
# class.
|
31
|
+
#
|
32
|
+
# p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
|
33
|
+
# # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
|
34
|
+
def self.file(name, *args)
|
35
|
+
new(*args).file(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the base64 encoded hash value of a given _string_. The
|
39
|
+
# return value is properly padded with '=' and contains no line
|
40
|
+
# feeds.
|
41
|
+
def self.base64digest(str, *args)
|
42
|
+
[digest(str, *args)].pack('m0')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module Instance
|
47
|
+
# Updates the digest with the contents of a given file _name_ and
|
48
|
+
# returns self.
|
49
|
+
def file(name)
|
50
|
+
File.open(name, "rb") {|f|
|
51
|
+
buf = ""
|
52
|
+
while f.read(16384, buf)
|
53
|
+
update buf
|
54
|
+
end
|
55
|
+
}
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
# If none is given, returns the resulting hash value of the digest
|
60
|
+
# in a base64 encoded form, keeping the digest's state.
|
61
|
+
#
|
62
|
+
# If a +string+ is given, returns the hash value for the given
|
63
|
+
# +string+ in a base64 encoded form, resetting the digest to the
|
64
|
+
# initial state before and after the process.
|
65
|
+
#
|
66
|
+
# In either case, the return value is properly padded with '=' and
|
67
|
+
# contains no line feeds.
|
68
|
+
def base64digest(str = nil)
|
69
|
+
[str ? digest(str) : digest].pack('m0')
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns the resulting hash value and resets the digest to the
|
73
|
+
# initial state.
|
74
|
+
def base64digest!
|
75
|
+
[digest!].pack('m0')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# call-seq:
|
81
|
+
# Digest(name) -> digest_subclass
|
82
|
+
#
|
83
|
+
# Returns a Digest subclass by +name+ in a thread-safe manner even
|
84
|
+
# when on-demand loading is involved.
|
85
|
+
#
|
86
|
+
# require 'digest'
|
87
|
+
#
|
88
|
+
# Digest("MD5")
|
89
|
+
# # => Digest::MD5
|
90
|
+
#
|
91
|
+
# Digest(:SHA256)
|
92
|
+
# # => Digest::SHA256
|
93
|
+
#
|
94
|
+
# Digest(:Foo)
|
95
|
+
# # => LoadError: library not found for class Digest::Foo -- digest/foo
|
96
|
+
def Digest(name)
|
97
|
+
const = name.to_sym
|
98
|
+
Digest::REQUIRE_MUTEX.synchronize {
|
99
|
+
# Ignore autoload's because it is void when we have #const_missing
|
100
|
+
Digest.const_missing(const)
|
101
|
+
}
|
102
|
+
rescue LoadError
|
103
|
+
# Constants do not necessarily rely on digest/*.
|
104
|
+
if Digest.const_defined?(const)
|
105
|
+
Digest.const_get(const)
|
106
|
+
else
|
107
|
+
raise
|
108
|
+
end
|
109
|
+
end
|