Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

sha2init.c
Go to the documentation of this file.
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_OPENSSL)
7 #include "sha2ossl.h"
8 #elif defined(SHA2_USE_COMMONDIGEST)
9 #include "sha2cc.h"
10 #else
11 #include "sha2.h"
12 #endif
13
14 #define FOREACH_BITLEN(func) func(256) func(384) func(512)
15
16 #define DEFINE_ALGO_METADATA(bitlen) \
17 static const rb_digest_metadata_t sha##bitlen = { \
18 RUBY_DIGEST_API_VERSION, \
19 SHA##bitlen##_DIGEST_LENGTH, \
20 SHA##bitlen##_BLOCK_LENGTH, \
21 sizeof(SHA##bitlen##_CTX), \
22 (rb_digest_hash_init_func_t)SHA##bitlen##_Init, \
23 (rb_digest_hash_update_func_t)SHA##bitlen##_Update, \
24 (rb_digest_hash_finish_func_t)SHA##bitlen##_Finish, \
25 };
26
27 FOREACH_BITLEN(DEFINE_ALGO_METADATA)
28
29 /*
30 * Classes for calculating message digests using the SHA-256/384/512
31 * Secure Hash Algorithm(s) by NIST (the US' National Institute of
32 * Standards and Technology), described in FIPS PUB 180-2.
33 */
34 void
35 Init_sha2(void)
36 {
37 VALUE mDigest, cDigest_Base;
38 ID id_metadata = rb_id_metadata();
39
40 #define DECLARE_ALGO_CLASS(bitlen) \
41 VALUE cDigest_SHA##bitlen;
42
43 FOREACH_BITLEN(DECLARE_ALGO_CLASS)
44
45 mDigest = rb_digest_namespace();
46 cDigest_Base = rb_path2class("Digest::Base");
47
48 #define DEFINE_ALGO_CLASS(bitlen) \
49 cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
50 \
51 rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
52 Data_Wrap_Struct(0, 0, 0, (void *)&sha##bitlen));
53
54 #undef RUBY_UNTYPED_DATA_WARNING
55 #define RUBY_UNTYPED_DATA_WARNING 0
56 FOREACH_BITLEN(DEFINE_ALGO_CLASS)
57 }
DEFINE_ALGO_METADATA
#define DEFINE_ALGO_METADATA(bitlen)
Definition: sha2init.c:16
ID
unsigned long ID
Definition: ruby.h:103
DEFINE_ALGO_CLASS
#define DEFINE_ALGO_CLASS(bitlen)
VALUE
unsigned long VALUE
Definition: ruby.h:102
sha2ossl.h
sha2cc.h
ruby.h
FOREACH_BITLEN
#define FOREACH_BITLEN(func)
Definition: sha2init.c:14
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:268
sha2.h
Init_sha2
void Init_sha2(void)
Definition: sha2init.c:35
DECLARE_ALGO_CLASS
#define DECLARE_ALGO_CLASS(bitlen)