Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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: sha2init.c 26745 2010-02-24 00:31:37Z nobu $ */
3
4 #include "digest.h"
5 #if defined(SHA2_USE_OPENSSL)
6 #include "sha2ossl.h"
7 #else
8 #include "sha2.h"
9 #endif
10
11 #define FOREACH_BITLEN(func) func(256) func(384) func(512)
12
13 #define DEFINE_ALGO_METADATA(bitlen) \
14 static const rb_digest_metadata_t sha##bitlen = { \
15 RUBY_DIGEST_API_VERSION, \
16 SHA##bitlen##_DIGEST_LENGTH, \
17 SHA##bitlen##_BLOCK_LENGTH, \
18 sizeof(SHA##bitlen##_CTX), \
19 (rb_digest_hash_init_func_t)SHA##bitlen##_Init, \
20 (rb_digest_hash_update_func_t)SHA##bitlen##_Update, \
21 (rb_digest_hash_finish_func_t)SHA##bitlen##_Finish, \
22 };
23
24 FOREACH_BITLEN(DEFINE_ALGO_METADATA)
25
26 /*
27 * Classes for calculating message digests using the SHA-256/384/512
28 * Secure Hash Algorithm(s) by NIST (the US' National Institute of
29 * Standards and Technology), described in FIPS PUB 180-2.
30 */
31 void
32 Init_sha2()
33 {
34 VALUE mDigest, cDigest_Base;
35 ID id_metadata;
36
37 #define DECLARE_ALGO_CLASS(bitlen) \
38 VALUE cDigest_SHA##bitlen;
39
40 FOREACH_BITLEN(DECLARE_ALGO_CLASS)
41
42 rb_require("digest");
43
44 id_metadata = rb_intern("metadata");
45
46 mDigest = rb_path2class("Digest");
47 cDigest_Base = rb_path2class("Digest::Base");
48
49 #define DEFINE_ALGO_CLASS(bitlen) \
50 cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
51 \
52 rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
53 Data_Wrap_Struct(rb_cObject, 0, 0, (void *)&sha##bitlen));
54
55 FOREACH_BITLEN(DEFINE_ALGO_CLASS)
56 }
57