Ruby: ext/digest/sha2/sha2init.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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_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) \
15static 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 */
32void
33Init_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 rb_digest_make_metadata(&sha##bitlen));
51
52 FOREACH_BITLEN(DEFINE_ALGO_CLASS)
53}
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:287
ruby.h
sha2.h
sha2cc.h
DEFINE_ALGO_METADATA
#define DEFINE_ALGO_METADATA(bitlen)
Definition: sha2init.c:14
Init_sha2
void Init_sha2(void)
Definition: sha2init.c:33
FOREACH_BITLEN
#define FOREACH_BITLEN(func)
Definition: sha2init.c:12
DEFINE_ALGO_CLASS
#define DEFINE_ALGO_CLASS(bitlen)
DECLARE_ALGO_CLASS
#define DECLARE_ALGO_CLASS(bitlen)
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39