1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/digest: Merge from trunk; apply all changes since the

initial import, except for the removal of compatibility stub
  libraries (md5.rb and sha1.rb).


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2006-10-10 05:01:33 +00:00
parent 6d83e40c32
commit 9b2fa26be4
33 changed files with 298 additions and 751 deletions

View file

@ -1,3 +1,9 @@
Tue Oct 10 13:49:53 2006 Akinori MUSHA <knu@iDaemons.org>
* ext/digest: Merge from trunk; apply all changes since the
initial import, except for the removal of compatibility stub
libraries (md5.rb and sha1.rb).
Mon Oct 9 23:46:29 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/parsedate.rb: documentation patch from Konrad Meyer

View file

@ -23,12 +23,8 @@
typedef unsigned int uint32_t;
# if SIZEOF_LONG == 8
typedef unsigned long uint64_t;
# elif defined(__GNUC__)
typedef unsigned long long uint64_t;
# elif defined(_MSC_VER)
typedef unsigned _int64 uint64_t;
# elif defined(__BORLANDC__)
typedef unsigned __int64 uint64_t;
# elif SIZEOF_LONG_LONG == 8
typedef unsigned LONG_LONG uint64_t;
# else
# define NO_UINT64_T
# endif

View file

@ -1,2 +1,8 @@
digest.o: digest.c digest.h $(hdrdir)/ruby.h $(topdir)/config.h \
$(hdrdir)/defines.h $(hdrdir)/intern.h
install-so: install-h
site-install-so: install-h
install-h:
$(INSTALL_DATA) $(srcdir)/digest.h $(RUBYARCHDIR)

View file

@ -6,49 +6,30 @@
created at: Fri May 25 08:57:27 JST 2001
Copyright (C) 1995-2001 Yukihiro Matsumoto
Copyright (C) 2001 Akinori MUSHA
Copyright (C) 2001-2006 Akinori MUSHA
$RoughId: digest.c,v 1.16 2001/07/13 15:38:27 knu Exp $
$Id$
************************************************/
/*
* This module provides an interface to the following hash algorithms:
*
* - the MD5 Message-Digest Algorithm by the RSA Data Security,
* Inc., described in RFC 1321
*
* - the SHA-1 Secure Hash Algorithm by NIST (the US' National
* Institute of Standards and Technology), described in FIPS PUB
* 180-1.
*
* - the SHA-256/384/512 Secure Hash Algorithm by NIST (the US'
* National Institute of Standards and Technology), described in
* FIPS PUB 180-2.
*
* - the RIPEMD-160 cryptographic hash function, designed by Hans
* Dobbertin, Antoon Bosselaers, and Bart Preneel.
*/
#include "digest.h"
static VALUE mDigest, cDigest_Base;
static ID id_metadata;
static ID id_metadata, id_new, id_update, id_digest;
/*
* Digest::Base
*/
static algo_t *
get_digest_base_metadata(klass)
VALUE klass;
get_digest_base_metadata(VALUE klass)
{
VALUE obj;
algo_t *algo;
if (rb_cvar_defined(klass, id_metadata) == Qfalse) {
rb_notimplement();
return NULL;
}
obj = rb_cvar_get(klass, id_metadata);
@ -58,10 +39,95 @@ get_digest_base_metadata(klass)
return algo;
}
static VALUE rb_digest_base_alloc _((VALUE));
static VALUE
rb_digest_base_alloc(klass)
VALUE klass;
hexdigest_str_new(VALUE str_digest)
{
char *digest = RSTRING_PTR(str_digest);
size_t digest_len = RSTRING_LEN(str_digest);
int i;
VALUE str;
char *p;
static const char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'
};
if (LONG_MAX / 2 < digest_len) {
rb_raise(rb_eRuntimeError, "digest string too long");
}
str = rb_str_new(0, digest_len * 2);
for (i = 0, p = RSTRING_PTR(str); i < digest_len; i++) {
unsigned char byte = digest[i];
p[i + i] = hex[byte >> 4];
p[i + i + 1] = hex[byte & 0x0f];
}
return str;
}
static VALUE
bubblebabble_str_new(VALUE str_digest)
{
char *digest = RSTRING_PTR(str_digest);
size_t digest_len = RSTRING_LEN(str_digest);
VALUE str;
char *p;
int i, j, seed = 1;
static const char vowels[] = {
'a', 'e', 'i', 'o', 'u', 'y'
};
static const char consonants[] = {
'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n',
'p', 'r', 's', 't', 'v', 'z', 'x'
};
if ((LONG_MAX - 2) / 3 < (digest_len | 1)) {
rb_raise(rb_eRuntimeError, "digest string too long");
}
str = rb_str_new(0, (digest_len | 1) * 3 + 2);
p = RSTRING_PTR(str);
i = j = 0;
p[j++] = 'x';
for (;;) {
unsigned char byte1, byte2;
if (i >= digest_len) {
p[j++] = vowels[seed % 6];
p[j++] = consonants[16];
p[j++] = vowels[seed / 6];
break;
}
byte1 = digest[i++];
p[j++] = vowels[(((byte1 >> 6) & 3) + seed) % 6];
p[j++] = consonants[(byte1 >> 2) & 15];
p[j++] = vowels[((byte1 & 3) + (seed / 6)) % 6];
if (i >= digest_len) {
break;
}
byte2 = digest[i++];
p[j++] = consonants[(byte2 >> 4) & 15];
p[j++] = '-';
p[j++] = consonants[byte2 & 15];
seed = (seed * 5 + byte1 * 7 + byte2) % 36;
}
p[j] = 'x';
return str;
}
static VALUE
rb_digest_base_alloc(VALUE klass)
{
algo_t *algo;
VALUE obj;
@ -73,7 +139,11 @@ rb_digest_base_alloc(klass)
algo = get_digest_base_metadata(klass);
/* XXX: An uninitialized buffer leads ALGO_Equal() to fail */
if (algo == NULL) {
return Data_Wrap_Struct(klass, 0, free, 0);
}
/* XXX: An uninitialized buffer may lead ALGO_Equal() to fail */
pctx = xcalloc(algo->ctx_size, 1);
algo->init_func(pctx);
@ -83,66 +153,44 @@ rb_digest_base_alloc(klass)
}
static VALUE
rb_digest_base_s_digest(klass, str)
VALUE klass;
VALUE str;
rb_digest_base_s_digest(VALUE klass, VALUE str)
{
algo_t *algo;
algo_t *algo = get_digest_base_metadata(klass);
void *pctx;
size_t len;
unsigned char *digest;
volatile VALUE obj = rb_digest_base_alloc(klass);
volatile VALUE obj;
algo = get_digest_base_metadata(klass);
if (algo == NULL) {
VALUE obj = rb_funcall(klass, id_new, 0);
rb_funcall(obj, id_update, 1, str);
return rb_funcall(obj, id_digest, 0);
}
obj = rb_digest_base_alloc(klass);
Data_Get_Struct(obj, void, pctx);
StringValue(str);
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
len = algo->digest_len;
str = rb_str_new(0, algo->digest_len);
algo->finish_func(pctx, RSTRING(str)->ptr);
digest = xmalloc(len);
algo->final_func(digest, pctx);
obj = rb_str_new(digest, len);
free(digest);
return obj;
return str;
}
static VALUE
rb_digest_base_s_hexdigest(klass, str)
VALUE klass;
VALUE str;
rb_digest_base_s_hexdigest(VALUE klass, VALUE str)
{
algo_t *algo;
void *pctx;
size_t len;
unsigned char *hexdigest;
volatile VALUE obj = rb_digest_base_alloc(klass);
algo = get_digest_base_metadata(klass);
Data_Get_Struct(obj, void, pctx);
StringValue(str);
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
len = algo->digest_len * 2;
hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
algo->end_func(pctx, hexdigest);
obj = rb_str_new(hexdigest, len);
free(hexdigest);
return obj;
return hexdigest_str_new(rb_funcall(klass, id_digest, 1, str));
}
static VALUE
rb_digest_base_copy(copy, obj)
VALUE copy, obj;
rb_digest_base_s_bubblebabble(VALUE klass, VALUE str)
{
return bubblebabble_str_new(rb_funcall(klass, id_digest, 1, str));
}
static VALUE
rb_digest_base_copy(VALUE copy, VALUE obj)
{
algo_t *algo;
void *pctx1, *pctx2;
@ -150,6 +198,11 @@ rb_digest_base_copy(copy, obj)
if (copy == obj) return copy;
rb_check_frozen(copy);
algo = get_digest_base_metadata(rb_obj_class(copy));
if (algo == NULL)
rb_notimplement();
/* get_digest_base_metadata() may return a NULL */
if (algo != get_digest_base_metadata(rb_obj_class(obj))) {
rb_raise(rb_eTypeError, "wrong argument class");
}
@ -161,26 +214,51 @@ rb_digest_base_copy(copy, obj)
}
static VALUE
rb_digest_base_update(self, str)
VALUE self, str;
rb_digest_base_update(VALUE self, VALUE str)
{
algo_t *algo;
void *pctx;
StringValue(str);
algo = get_digest_base_metadata(rb_obj_class(self));
if (algo == NULL) {
/* subclasses must define update() */
rb_notimplement();
}
Data_Get_Struct(self, void, pctx);
StringValue(str);
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
return self;
}
static VALUE
rb_digest_base_init(argc, argv, self)
int argc;
VALUE* argv;
VALUE self;
rb_digest_base_lshift(VALUE self, VALUE str)
{
algo_t *algo;
void *pctx;
algo = get_digest_base_metadata(rb_obj_class(self));
if (algo == NULL) {
/* subclasses just need to define update(), not << */
rb_funcall(self, id_update, 1, str);
return self;
}
Data_Get_Struct(self, void, pctx);
StringValue(str);
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
return self;
}
static VALUE
rb_digest_base_init(int argc, VALUE *argv, VALUE self)
{
VALUE arg;
@ -192,70 +270,71 @@ rb_digest_base_init(argc, argv, self)
}
static VALUE
rb_digest_base_digest(self)
VALUE self;
rb_digest_base_digest(VALUE self)
{
algo_t *algo;
void *pctx1, *pctx2;
unsigned char *digest;
size_t len;
size_t ctx_size;
VALUE str;
algo = get_digest_base_metadata(rb_obj_class(self));
if (algo == NULL)
rb_notimplement();
Data_Get_Struct(self, void, pctx1);
len = algo->ctx_size;
ctx_size = algo->ctx_size;
pctx2 = xmalloc(ctx_size);
memcpy(pctx2, pctx1, ctx_size);
pctx2 = xmalloc(len);
memcpy(pctx2, pctx1, len);
len = algo->digest_len;
digest = xmalloc(len);
algo->final_func(digest, pctx2);
str = rb_str_new(digest, len);
free(digest);
str = rb_str_new(0, algo->digest_len);
algo->finish_func(pctx2, RSTRING(str)->ptr);
free(pctx2);
return str;
}
static VALUE
rb_digest_base_hexdigest(self)
VALUE self;
rb_digest_base_hexdigest(VALUE self)
{
return hexdigest_str_new(rb_funcall(self, id_digest, 0));
}
static VALUE
rb_digest_base_bubblebabble(VALUE self)
{
return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
}
static VALUE
rb_digest_base_inspect(VALUE self)
{
algo_t *algo;
void *pctx1, *pctx2;
unsigned char *hexdigest;
size_t len;
VALUE str;
VALUE klass, str;
size_t digest_len = 32; /* no need to be just the right size */
char *cname;
algo = get_digest_base_metadata(rb_obj_class(self));
Data_Get_Struct(self, void, pctx1);
klass = rb_obj_class(self);
algo = get_digest_base_metadata(klass);
len = algo->ctx_size;
if (algo != NULL)
digest_len = algo->digest_len;
pctx2 = xmalloc(len);
memcpy(pctx2, pctx1, len);
len = algo->digest_len * 2;
hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
algo->end_func(pctx2, hexdigest);
str = rb_str_new(hexdigest, len);
free(hexdigest);
free(pctx2);
cname = rb_obj_classname(self);
/* #<Digest::Alg: xxxxx...xxxx> */
str = rb_str_buf_new(2 + strlen(cname) + 2 + digest_len * 2 + 1);
rb_str_buf_cat2(str, "#<");
rb_str_buf_cat2(str, cname);
rb_str_buf_cat2(str, ": ");
rb_str_buf_append(str, rb_digest_base_hexdigest(self));
rb_str_buf_cat2(str, ">");
return str;
}
static VALUE
rb_digest_base_equal(self, other)
VALUE self, other;
rb_digest_base_equal(VALUE self, VALUE other)
{
algo_t *algo;
VALUE klass;
@ -289,11 +368,25 @@ rb_digest_base_equal(self, other)
}
/*
* Init
* This module provides an interface to the following hash algorithms:
*
* - the MD5 Message-Digest Algorithm by the RSA Data Security,
* Inc., described in RFC 1321
*
* - the SHA-1 Secure Hash Algorithm by NIST (the US' National
* Institute of Standards and Technology), described in FIPS PUB
* 180-1.
*
* - the SHA-256/384/512 Secure Hash Algorithm by NIST (the US'
* National Institute of Standards and Technology), described in
* FIPS PUB 180-2.
*
* - the RIPEMD-160 cryptographic hash function, designed by Hans
* Dobbertin, Antoon Bosselaers, and Bart Preneel.
*/
void
Init_digest()
Init_digest(void)
{
mDigest = rb_define_module("Digest");
@ -302,15 +395,21 @@ Init_digest()
rb_define_alloc_func(cDigest_Base, rb_digest_base_alloc);
rb_define_singleton_method(cDigest_Base, "digest", rb_digest_base_s_digest, 1);
rb_define_singleton_method(cDigest_Base, "hexdigest", rb_digest_base_s_hexdigest, 1);
rb_define_singleton_method(cDigest_Base, "bubblebabble", rb_digest_base_s_bubblebabble, 1);
rb_define_method(cDigest_Base, "initialize", rb_digest_base_init, -1);
rb_define_method(cDigest_Base, "initialize_copy", rb_digest_base_copy, 1);
rb_define_method(cDigest_Base, "update", rb_digest_base_update, 1);
rb_define_method(cDigest_Base, "<<", rb_digest_base_update, 1);
rb_define_method(cDigest_Base, "<<", rb_digest_base_lshift, 1);
rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
rb_define_method(cDigest_Base, "hexdigest", rb_digest_base_hexdigest, 0);
rb_define_method(cDigest_Base, "bubblebabble", rb_digest_base_bubblebabble, 0);
rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
rb_define_method(cDigest_Base, "inspect", rb_digest_base_inspect, 0);
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);
id_metadata = rb_intern("metadata");
id_new = rb_intern("new");
id_update = rb_intern("update");
id_digest = rb_intern("digest");
}

View file

@ -15,18 +15,16 @@
#include "ruby.h"
typedef void (*hash_init_func_t) _((void *));
typedef void (*hash_update_func_t) _((void *, unsigned char *, size_t));
typedef void (*hash_end_func_t) _((void *, unsigned char *));
typedef void (*hash_final_func_t) _((unsigned char *, void *));
typedef int (*hash_equal_func_t) _((void *, void *));
typedef void (*hash_init_func_t)(void *);
typedef void (*hash_update_func_t)(void *, unsigned char *, size_t);
typedef void (*hash_finish_func_t)(void *, unsigned char *);
typedef int (*hash_equal_func_t)(void *, void *);
typedef struct {
size_t digest_len;
size_t ctx_size;
hash_init_func_t init_func;
hash_update_func_t update_func;
hash_end_func_t end_func;
hash_final_func_t final_func;
hash_finish_func_t finish_func;
hash_equal_func_t equal_func;
} algo_t;

View file

@ -24,4 +24,6 @@ have_header("inttypes.h")
have_header("unistd.h")
$preload = %w[digest]
create_makefile("digest/md5")

View file

@ -41,6 +41,12 @@
1999-05-03 lpd Original version.
*/
/*
This code was modified for use in Ruby.
- Akinori MUSHA <knu@idaemons.org>
*/
/*$OrigId: md5c.c,v 1.2 2001/03/26 08:57:14 matz Exp $ */
/*$RoughId: md5.c,v 1.2 2001/07/13 19:48:41 knu Exp $ */
/*$Id$ */
@ -391,7 +397,7 @@ MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
}
void
MD5_Final(uint8_t *digest, MD5_CTX *pms)
MD5_Finish(MD5_CTX *pms, uint8_t *digest)
{
static const uint8_t pad[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -413,18 +419,6 @@ MD5_Final(uint8_t *digest, MD5_CTX *pms)
digest[i] = (uint8_t)(pms->state[i >> 2] >> ((i & 3) << 3));
}
void
MD5_End(MD5_CTX *pctx, uint8_t *hexdigest)
{
unsigned char digest[16];
size_t i;
MD5_Final(digest, pctx);
for (i = 0; i < 16; i++)
sprintf(hexdigest + i * 2, "%02x", digest[i]);
}
int MD5_Equal(MD5_CTX* pctx1, MD5_CTX* pctx2) {
return memcmp(pctx1->count, pctx2->count, sizeof(pctx1->count)) == 0
&& memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0

View file

@ -63,17 +63,16 @@ typedef struct md5_state_s {
} MD5_CTX;
#ifdef RUBY
/* avoid name clash */
#define MD5_Init rb_Digest_MD5_Init
#define MD5_Update rb_Digest_MD5_Update
#define MD5_Final rb_Digest_MD5_Final
#define MD5_End rb_Digest_MD5_End
#define MD5_Finish rb_Digest_MD5_Finish
#define MD5_Equal rb_Digest_MD5_Equal
#endif
void MD5_Init _((MD5_CTX *pms));
void MD5_Update _((MD5_CTX *pms, const uint8_t *data, size_t nbytes));
void MD5_Final _((uint8_t *digest, MD5_CTX *pms));
void MD5_End _((MD5_CTX *pctx, uint8_t *hexdigest));
void MD5_Finish _((MD5_CTX *pms, uint8_t *digest));
int MD5_Equal _((MD5_CTX *pctx1, MD5_CTX *pctx2));
#define MD5_BLOCK_LENGTH 64

View file

@ -13,8 +13,7 @@ static algo_t md5 = {
sizeof(MD5_CTX),
(hash_init_func_t)MD5_Init,
(hash_update_func_t)MD5_Update,
(hash_end_func_t)MD5_End,
(hash_final_func_t)MD5_Final,
(hash_finish_func_t)MD5_Finish,
(hash_equal_func_t)MD5_Equal,
};
@ -23,7 +22,7 @@ Init_md5()
{
VALUE mDigest, cDigest_Base, cDigest_MD5;
rb_require("digest.so");
rb_require("digest");
mDigest = rb_path2class("Digest");
cDigest_Base = rb_path2class("Digest::Base");

View file

@ -6,15 +6,9 @@
#include <string.h>
void
MD5_End(MD5_CTX *pctx, unsigned char *hexdigest)
MD5_Finish(MD5_CTX *pctx, unsigned char *digest)
{
unsigned char digest[16];
size_t i;
MD5_Final(digest, pctx);
for (i = 0; i < 16; i++)
sprintf(hexdigest + i * 2, "%02x", digest[i]);
}
int

View file

@ -6,7 +6,7 @@
#include <stddef.h>
#include <openssl/md5.h>
void MD5_End(MD5_CTX *pctx, unsigned char *hexdigest);
void MD5_Finish(MD5_CTX *pctx, unsigned char *digest);
int MD5_Equal(MD5_CTX *pctx1, MD5_CTX *pctx2);
#endif

View file

@ -1,7 +1,5 @@
rmd160.o: rmd160.c rmd160.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
rmd160hl.o: rmd160hl.c rmd160.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
rmd160init.o: rmd160init.c $(srcdir)/../digest.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h \
rmd160.h $(srcdir)/../defs.h

View file

@ -14,7 +14,7 @@ if !with_config("bundled-rmd160") &&
have_library("crypto") && have_header("openssl/ripemd.h")
$objs << "rmd160ossl.#{$OBJEXT}"
else
$objs << "rmd160.#{$OBJEXT}" << "rmd160hl.#{$OBJEXT}"
$objs << "rmd160.#{$OBJEXT}"
end
have_header("sys/cdefs.h")
@ -23,4 +23,6 @@ have_header("inttypes.h")
have_header("unistd.h")
$preload = %w[digest]
create_makefile("digest/rmd160")

View file

@ -409,7 +409,7 @@ RMD160_Update(RMD160_CTX *context, const uint8_t *data, size_t nbytes)
/********************************************************************/
void
RMD160_Final(uint8_t digest[20], RMD160_CTX *context)
RMD160_Finish(RMD160_CTX *context, uint8_t digest[20])
{
uint32_t i;
uint32_t X[16];

View file

@ -39,26 +39,16 @@ typedef struct {
#define RMD160_Init rb_Digest_RMD160_Init
#define RMD160_Transform rb_Digest_RMD160_Transform
#define RMD160_Update rb_Digest_RMD160_Update
#define RMD160_Final rb_Digest_RMD160_Final
#define RMD160_Finish rb_Digest_RMD160_Finish
#define RMD160_Equal rb_Digest_RMD160_Equal
#ifndef _KERNEL
#define RMD160_End rb_Digest_RMD160_End
#define RMD160_File rb_Digest_RMD160_File
#define RMD160_Data rb_Digest_RMD160_Data
#endif /* _KERNEL */
#endif
__BEGIN_DECLS
void RMD160_Init _((RMD160_CTX *));
void RMD160_Transform _((uint32_t[5], const uint32_t[16]));
void RMD160_Update _((RMD160_CTX *, const uint8_t *, size_t));
void RMD160_Final _((uint8_t[20], RMD160_CTX *));
void RMD160_Finish _((RMD160_CTX *, uint8_t[20]));
int RMD160_Equal _((RMD160_CTX *, RMD160_CTX *));
#ifndef _KERNEL
char *RMD160_End _((RMD160_CTX *, char *));
char *RMD160_File _((char *, char *));
char *RMD160_Data _((const uint8_t *, size_t, char *));
#endif /* _KERNEL */
__END_DECLS
#define RMD160_BLOCK_LENGTH 64

View file

@ -1,96 +0,0 @@
/* $NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */
/* $RoughId: rmd160hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
/* $Id$ */
/* rmd160hl.c
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* from OpenBSD: rmd160hl.c,v 1.2 1999/08/17 09:13:12 millert Exp $
*/
#include "rmd160.h"
#ifndef lint
/* __RCSID("$NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); */
#endif /* not lint */
/* #include "namespace.h" */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
char *
RMD160_End(RMD160_CTX *ctx, char *buf)
{
size_t i;
char *p = buf;
uint8_t digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
RMD160_Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[(uint32_t)digest[i] >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
}
char *
RMD160_File(char *filename, char *buf)
{
uint8_t buffer[BUFSIZ];
RMD160_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
RMD160_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
RMD160_Update(&ctx, buffer, (size_t)num);
oerrno = errno;
close(fd);
errno = oerrno;
return(num < 0 ? 0 : RMD160_End(&ctx, buf));
}
char *
RMD160_Data(const uint8_t *data, size_t len, char *buf)
{
RMD160_CTX ctx;
_DIAGASSERT(data != NULL);
/* XXX: buf may be NULL ? */
RMD160_Init(&ctx);
RMD160_Update(&ctx, data, len);
return(RMD160_End(&ctx, buf));
}

View file

@ -13,8 +13,7 @@ static algo_t rmd160 = {
sizeof(RMD160_CTX),
(hash_init_func_t)RMD160_Init,
(hash_update_func_t)RMD160_Update,
(hash_end_func_t)RMD160_End,
(hash_final_func_t)RMD160_Final,
(hash_finish_func_t)RMD160_Finish,
(hash_equal_func_t)RMD160_Equal,
};
@ -24,7 +23,7 @@ Init_rmd160()
VALUE mDigest, cDigest_Base, cDigest_RMD160;
ID id_metadata;
rb_require("digest.so");
rb_require("digest");
mDigest = rb_path2class("Digest");
cDigest_Base = rb_path2class("Digest::Base");

View file

@ -5,31 +5,8 @@
#include <assert.h>
#include <stdlib.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
char *
RMD160_End(RMD160_CTX *ctx, char *buf)
{
size_t i;
char *p = buf;
uint8_t digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
RMD160_Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[(uint32_t)digest[i] >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
void RMD160_Finish(RMD160_CTX *ctx, char *buf) {
RIPEMD160_Final(buf, ctx);
}
int RMD160_Equal(RMD160_CTX* pctx1, RMD160_CTX* pctx2) {

View file

@ -10,12 +10,11 @@
#define RMD160_Init RIPEMD160_Init
#define RMD160_Update RIPEMD160_Update
#define RMD160_Final RIPEMD160_Final
#define RMD160_BLOCK_LENGTH RIPEMD160_CBLOCK
#define RMD160_DIGEST_LENGTH RIPEMD160_DIGEST_LENGTH
char *RMD160_End(RMD160_CTX *ctx, char *buf);
void RMD160_Finish(RMD160_CTX *ctx, char *buf);
int RMD160_Equal(RMD160_CTX *pctx1, RMD160_CTX *pctx2);
#endif

View file

@ -1,7 +1,5 @@
sha1.o: sha1.c sha1.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
sha1hl.o: sha1hl.c sha1.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
sha1init.o: sha1init.c $(srcdir)/../digest.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h \
sha1.h $(srcdir)/../defs.h

View file

@ -14,7 +14,7 @@ if !with_config("bundled-sha1") &&
have_library("crypto") && have_header("openssl/sha.h")
$objs << "sha1ossl.#{$OBJEXT}"
else
$objs << "sha1.#{$OBJEXT}" << "sha1hl.#{$OBJEXT}"
$objs << "sha1.#{$OBJEXT}"
end
have_header("sys/cdefs.h")
@ -23,4 +23,6 @@ have_header("inttypes.h")
have_header("unistd.h")
$preload = %w[digest]
create_makefile("digest/sha1")

View file

@ -129,9 +129,7 @@ do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e, CHAR64LON
/*
* Hash a single 512-bit block. This is the core of the algorithm.
*/
void SHA1_Transform(state, buffer)
uint32_t state[5];
const uint8_t buffer[64];
void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
{
uint32_t a, b, c, d, e;
CHAR64LONG16 *block;
@ -201,8 +199,7 @@ void SHA1_Transform(state, buffer)
/*
* SHA1_Init - Initialize new context
*/
void SHA1_Init(context)
SHA1_CTX *context;
void SHA1_Init(SHA1_CTX *context)
{
_DIAGASSERT(context != 0);
@ -220,10 +217,7 @@ void SHA1_Init(context)
/*
* Run your data through this.
*/
void SHA1_Update(context, data, len)
SHA1_CTX *context;
const uint8_t *data;
size_t len;
void SHA1_Update(SHA1_CTX *context, const uint8_t *data, size_t len)
{
uint32_t i, j;
@ -250,9 +244,7 @@ void SHA1_Update(context, data, len)
/*
* Add padding and return the message digest.
*/
void SHA1_Final(digest, context)
uint8_t digest[20];
SHA1_CTX* context;
void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
{
size_t i;
uint8_t finalcount[8];

View file

@ -20,28 +20,19 @@ typedef struct {
} SHA1_CTX;
#ifdef RUBY
/* avoid name clash */
#define SHA1_Transform rb_Digest_SHA1_Transform
#define SHA1_Init rb_Digest_SHA1_Init
#define SHA1_Update rb_Digest_SHA1_Update
#define SHA1_Final rb_Digest_SHA1_Final
#define SHA1_Finish rb_Digest_SHA1_Finish
#define SHA1_Equal rb_Digest_SHA1_Equal
#ifndef _KERNEL
#define SHA1_End rb_Digest_SHA1_End
#define SHA1_File rb_Digest_SHA1_File
#define SHA1_Data rb_Digest_SHA1_Data
#endif /* _KERNEL */
#endif
void SHA1_Transform _((uint32_t state[5], const uint8_t buffer[64]));
void SHA1_Init _((SHA1_CTX *context));
void SHA1_Update _((SHA1_CTX *context, const uint8_t *data, size_t len));
void SHA1_Final _((uint8_t digest[20], SHA1_CTX *context));
void SHA1_Finish _((SHA1_CTX *context, uint8_t digest[20]));
int SHA1_Equal _((SHA1_CTX *pctx1, SHA1_CTX *pctx2));
#ifndef _KERNEL
char *SHA1_End _((SHA1_CTX *, char *));
char *SHA1_File _((char *, char *));
char *SHA1_Data _((const uint8_t *, size_t, char *));
#endif /* _KERNEL */
#define SHA1_BLOCK_LENGTH 64
#define SHA1_DIGEST_LENGTH 20

View file

@ -1,102 +0,0 @@
/* $NetBSD: sha1hl.c,v 1.2 2001/03/10 15:55:14 tron Exp $ */
/* $RoughId: sha1hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
/* $Id$ */
/* sha1hl.c
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/
/* #include "namespace.h" */
#include "sha1.h"
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
/* __RCSID("$NetBSD: sha1hl.c,v 1.2 2001/03/10 15:55:14 tron Exp $"); */
#endif /* LIBC_SCCS and not lint */
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
/* ARGSUSED */
char *
SHA1_End(ctx, buf)
SHA1_CTX *ctx;
char *buf;
{
int i;
char *p = buf;
uint8_t digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
SHA1_Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[((uint32_t)digest[i]) >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
}
char *
SHA1_File (filename, buf)
char *filename;
char *buf;
{
uint8_t buffer[BUFSIZ];
SHA1_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA1_Init(&ctx);
if ((fd = open(filename,O_RDONLY)) < 0)
return(0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA1_Update(&ctx, buffer, (size_t)num);
oerrno = errno;
close(fd);
errno = oerrno;
return(num < 0 ? 0 : SHA1_End(&ctx, buf));
}
char *
SHA1_Data (data, len, buf)
const uint8_t *data;
size_t len;
char *buf;
{
SHA1_CTX ctx;
_DIAGASSERT(data != NULL);
/* XXX: buf may be NULL ? */
SHA1_Init(&ctx);
SHA1_Update(&ctx, data, len);
return(SHA1_End(&ctx, buf));
}

View file

@ -13,8 +13,7 @@ static algo_t sha1 = {
sizeof(SHA1_CTX),
(hash_init_func_t)SHA1_Init,
(hash_update_func_t)SHA1_Update,
(hash_end_func_t)SHA1_End,
(hash_final_func_t)SHA1_Final,
(hash_finish_func_t)SHA1_Finish,
(hash_equal_func_t)SHA1_Equal,
};
@ -22,17 +21,14 @@ void
Init_sha1()
{
VALUE mDigest, cDigest_Base, cDigest_SHA1;
ID id_metadata;
rb_require("digest.so");
rb_require("digest");
mDigest = rb_path2class("Digest");
cDigest_Base = rb_path2class("Digest::Base");
cDigest_SHA1 = rb_define_class_under(mDigest, "SHA1", cDigest_Base);
id_metadata = rb_intern("metadata");
rb_cvar_set(cDigest_SHA1, id_metadata,
rb_cvar_set(cDigest_SHA1, rb_intern("metadata"),
Data_Wrap_Struct(rb_cObject, 0, 0, &sha1), Qtrue);
}

View file

@ -2,37 +2,17 @@
#include "defs.h"
#include "sha1ossl.h"
#include <assert.h>
#include <stdlib.h>
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
char *
SHA1_End(SHA1_CTX *ctx, char *buf)
void
SHA1_Finish(SHA1_CTX *ctx, char *buf)
{
int i;
char *p = buf;
uint8_t digest[20];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != NULL);
/* buf may be NULL */
if (p == NULL && (p = malloc(41)) == NULL)
return 0;
SHA1_Final(digest,ctx);
for (i = 0; i < 20; i++) {
p[i + i] = hex[((uint32_t)digest[i]) >> 4];
p[i + i + 1] = hex[digest[i] & 0x0f];
}
p[i + i] = '\0';
return(p);
SHA1_Final(buf, ctx);
}
int SHA1_Equal(SHA1_CTX* pctx1, SHA1_CTX* pctx2) {
int
SHA1_Equal(SHA1_CTX* pctx1, SHA1_CTX* pctx2)
{
return pctx1->num == pctx2->num
&& pctx1->h0 == pctx2->h0
&& pctx1->h1 == pctx2->h1

View file

@ -11,7 +11,7 @@
#define SHA1_BLOCK_LENGTH SHA_BLOCK_LENGTH
#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
char *SHA1_End(SHA1_CTX *ctx, char *buf);
void SHA1_Finish(SHA1_CTX *ctx, char *buf);
int SHA1_Equal(SHA1_CTX *pctx1, SHA1_CTX *pctx2);
#endif

View file

@ -1,7 +1,5 @@
sha2.o: sha2.c sha2.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
sha2hl.o: sha2hl.c sha2.h $(srcdir)/../defs.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h
sha2init.o: sha2init.c $(srcdir)/../digest.h $(hdrdir)/ruby.h \
$(topdir)/config.h $(hdrdir)/defines.h $(hdrdir)/intern.h \
sha2.h $(srcdir)/../defs.h

View file

@ -8,7 +8,6 @@ $INCFLAGS << " -I$(srcdir)/.."
$objs = [
"sha2.#{$OBJEXT}",
"sha2hl.#{$OBJEXT}",
"sha2init.#{$OBJEXT}",
]
@ -18,6 +17,8 @@ have_header("inttypes.h")
have_header("unistd.h")
$preload = %w[digest]
if have_type("uint64_t", "defs.h", $defs.join(' '))
create_makefile("digest/sha2")
end

View file

@ -515,7 +515,7 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
usedspace = freespace = 0;
}
void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
void SHA256_Finish(SHA256_CTX* context, sha2_byte digest[]) {
sha2_word32 *d = (sha2_word32*)digest;
unsigned int usedspace;
@ -852,7 +852,7 @@ void SHA512_Last(SHA512_CTX* context) {
SHA512_Transform(context, (const sha2_word64*)context->buffer);
}
void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
void SHA512_Finish(SHA512_CTX* context, sha2_byte digest[]) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */
@ -901,7 +901,7 @@ void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
SHA512_Update((SHA512_CTX*)context, data, len);
}
void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
void SHA384_Finish(SHA384_CTX* context, sha2_byte digest[]) {
sha2_word64 *d = (sha2_word64*)digest;
/* Sanity check: */

View file

@ -77,52 +77,34 @@ typedef SHA512_CTX SHA384_CTX;
#ifdef RUBY
#define SHA256_Init rb_Digest_SHA256_Init
#define SHA256_Update rb_Digest_SHA256_Update
#define SHA256_Final rb_Digest_SHA256_Final
#define SHA256_End rb_Digest_SHA256_End
#define SHA256_Data rb_Digest_SHA256_Data
#define SHA256_File rb_Digest_SHA256_File
#define SHA256_Finish rb_Digest_SHA256_Finish
#define SHA256_Equal rb_Digest_SHA256_Equal
#define SHA384_Init rb_Digest_SHA384_Init
#define SHA384_Update rb_Digest_SHA384_Update
#define SHA384_Final rb_Digest_SHA384_Final
#define SHA384_End rb_Digest_SHA384_End
#define SHA384_Data rb_Digest_SHA384_Data
#define SHA384_File rb_Digest_SHA384_File
#define SHA384_Finish rb_Digest_SHA384_Finish
#define SHA384_Equal rb_Digest_SHA384_Equal
#define SHA512_Init rb_Digest_SHA512_Init
#define SHA512_Update rb_Digest_SHA512_Update
#define SHA512_Final rb_Digest_SHA512_Final
#define SHA512_End rb_Digest_SHA512_End
#define SHA512_Data rb_Digest_SHA512_Data
#define SHA512_File rb_Digest_SHA512_File
#define SHA512_Finish rb_Digest_SHA512_Finish
#define SHA512_Equal rb_Digest_SHA512_Equal
#endif
/*** SHA-256/384/512 Function Prototypes ******************************/
void SHA256_Init _((SHA256_CTX *));
void SHA256_Update _((SHA256_CTX*, const uint8_t*, size_t));
void SHA256_Final _((uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*));
char* SHA256_End _((SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]));
char* SHA256_Data _((const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]));
char *SHA256_File _((char *, char *));
void SHA256_Finish _((SHA256_CTX*, uint8_t[SHA256_DIGEST_LENGTH]));
int SHA256_Equal _((SHA256_CTX*, SHA256_CTX*));
void SHA384_Init _((SHA384_CTX*));
void SHA384_Update _((SHA384_CTX*, const uint8_t*, size_t));
void SHA384_Final _((uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*));
char* SHA384_End _((SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]));
char* SHA384_Data _((const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]));
char *SHA384_File _((char *, char *));
void SHA384_Finish _((SHA384_CTX*, uint8_t[SHA384_DIGEST_LENGTH]));
int SHA384_Equal _((SHA384_CTX*, SHA384_CTX*));
void SHA512_Init _((SHA512_CTX*));
void SHA512_Update _((SHA512_CTX*, const uint8_t*, size_t));
void SHA512_Final _((uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*));
char* SHA512_End _((SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]));
char* SHA512_Data _((const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]));
char *SHA512_File _((char *, char *));
void SHA512_Finish _((SHA512_CTX*, uint8_t[SHA512_DIGEST_LENGTH]));
int SHA512_Equal _((SHA512_CTX*, SHA512_CTX*));
#ifdef __cplusplus

View file

@ -1,252 +0,0 @@
/* $NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $ */
/* $RoughId: sha2hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
/* $Id$ */
/*
* sha2hl.c
* This code includes some functions taken from sha2.c, hence the
* following licence reproduction.
*
* This code is not a verbatim copy, since some routines have been added,
* and some bugs have been fixed.
*
* Version 1.0.0beta1
*
* Written by Aaron D. Gifford <me@aarongifford.com>
*
* Copyright 2000 Aaron D. Gifford. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "sha2.h"
#ifndef lint
/* __RCSID("$NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $"); */
#endif /* not lint */
/* #include "namespace.h" */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT(cond) assert(cond)
#endif
/*
* Constant used by SHA256/384/512_End() functions for converting the
* digest to a readable hexadecimal character string:
*/
static const char sha2_hex_digits[] = "0123456789abcdef";
char *
SHA256_File(char *filename, char *buf)
{
uint8_t buffer[BUFSIZ * 20];
SHA256_CTX ctx;
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA256_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA256_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA256_End(&ctx, buf));
}
char *
SHA256_End(SHA256_CTX *ctx, char buffer[])
{
uint8_t digest[SHA256_DIGEST_LENGTH], *d = digest;
uint8_t *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA256_Final(digest, ctx);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) memset(ctx, 0, sizeof(SHA256_CTX));
}
(void) memset(digest, 0, SHA256_DIGEST_LENGTH);
return ret;
}
char *
SHA256_Data(const uint8_t * data, size_t len, char *digest)
{
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, data, len);
return SHA256_End(&ctx, digest);
}
char *
SHA384_File(char *filename, char *buf)
{
SHA384_CTX ctx;
uint8_t buffer[BUFSIZ * 20];
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA384_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA384_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA384_End(&ctx, buf));
}
char *
SHA384_End(SHA384_CTX * ctx, char buffer[])
{
uint8_t digest[SHA384_DIGEST_LENGTH], *d = digest;
uint8_t *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA384_Final(digest, ctx);
for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) memset(ctx, 0, sizeof(SHA384_CTX));
}
(void) memset(digest, 0, SHA384_DIGEST_LENGTH);
return ret;
}
char *
SHA384_Data(const uint8_t * data, size_t len, char *digest)
{
SHA384_CTX ctx;
SHA384_Init(&ctx);
SHA384_Update(&ctx, data, len);
return SHA384_End(&ctx, digest);
}
char *
SHA512_File(char *filename, char *buf)
{
SHA512_CTX ctx;
uint8_t buffer[BUFSIZ * 20];
int fd, num, oerrno;
_DIAGASSERT(filename != NULL);
/* XXX: buf may be NULL ? */
SHA512_Init(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0)
return (0);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
SHA512_Update(&ctx, buffer, (size_t) num);
oerrno = errno;
close(fd);
errno = oerrno;
return (num < 0 ? 0 : SHA512_End(&ctx, buf));
}
char *
SHA512_End(SHA512_CTX * ctx, char buffer[])
{
uint8_t digest[SHA512_DIGEST_LENGTH], *d = digest;
uint8_t *ret;
int i;
/* Sanity check: */
assert(ctx != NULL);
if ((ret = buffer) != NULL) {
SHA512_Final(digest, ctx);
for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
*buffer++ = sha2_hex_digits[*d & 0x0f];
d++;
}
*buffer = (char) 0;
} else {
(void) memset(ctx, 0, sizeof(SHA512_CTX));
}
(void) memset(digest, 0, SHA512_DIGEST_LENGTH);
return ret;
}
char *
SHA512_Data(const uint8_t * data, size_t len, char *digest)
{
SHA512_CTX ctx;
SHA512_Init(&ctx);
SHA512_Update(&ctx, data, len);
return SHA512_End(&ctx, digest);
}

View file

@ -12,8 +12,7 @@ static algo_t sha##bitlen = { \
sizeof(SHA##bitlen##_CTX), \
(hash_init_func_t)SHA##bitlen##_Init, \
(hash_update_func_t)SHA##bitlen##_Update, \
(hash_end_func_t)SHA##bitlen##_End, \
(hash_final_func_t)SHA##bitlen##_Final, \
(hash_finish_func_t)SHA##bitlen##_Finish, \
(hash_equal_func_t)SHA##bitlen##_Equal, \
};
@ -30,7 +29,7 @@ Init_sha2()
FOREACH_BITLEN(DECLARE_ALGO_CLASS)
rb_require("digest.so");
rb_require("digest");
id_metadata = rb_intern("metadata");