Ruby  2.5.0dev(2017-10-22revision60238)
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 
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;
39 
40 #define DECLARE_ALGO_CLASS(bitlen) \
41  VALUE cDigest_SHA##bitlen;
42 
44 
45  rb_require("digest");
46 
47  id_metadata = rb_intern_const("metadata");
48 
49  mDigest = rb_path2class("Digest");
50  cDigest_Base = rb_path2class("Digest::Base");
51 
52 #define DEFINE_ALGO_CLASS(bitlen) \
53  cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
54 \
55  rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
56  Data_Wrap_Struct(0, 0, 0, (void *)&sha##bitlen));
57 
58 #undef RUBY_UNTYPED_DATA_WARNING
59 #define RUBY_UNTYPED_DATA_WARNING 0
61 }
#define DEFINE_ALGO_METADATA(bitlen)
Definition: sha2init.c:16
VALUE rb_path2class(const char *)
Definition: variable.c:432
VALUE rb_require(const char *)
Definition: load.c:1061
#define DEFINE_ALGO_CLASS(bitlen)
#define DECLARE_ALGO_CLASS(bitlen)
unsigned long ID
Definition: ruby.h:86
unsigned long VALUE
Definition: ruby.h:85
#define rb_intern_const(str)
Definition: ruby.h:1777
void Init_sha2(void)
Definition: sha2init.c:35
#define FOREACH_BITLEN(func)
Definition: sha2init.c:14