类 Digest::Base

这个抽象类为用 C 编写的消息摘要实现类提供了一个通用的接口。

用 C 编写 Digest 子类

Digest::Base 为用 C 编写的消息摘要类提供了一个通用的接口。这些类必须提供一个 rb_digest_metadata_t 类型的结构体

typedef int (*rb_digest_hash_init_func_t)(void *);
typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
typedef int (*rb_digest_hash_finish_func_t)(void *, unsigned char *);

typedef struct {
  int api_version;
  size_t digest_len;
  size_t block_len;
  size_t ctx_size;
  rb_digest_hash_init_func_t init_func;
  rb_digest_hash_update_func_t update_func;
  rb_digest_hash_finish_func_t finish_func;
} rb_digest_metadata_t;

这个结构体必须设置为名为 metadata 的实例变量(前面没有 +@+)。例如

 static const rb_digest_metadata_t sha1 = {
    RUBY_DIGEST_API_VERSION,
    SHA1_DIGEST_LENGTH,
    SHA1_BLOCK_LENGTH,
    sizeof(SHA1_CTX),
    (rb_digest_hash_init_func_t)SHA1_Init,
    (rb_digest_hash_update_func_t)SHA1_Update,
    (rb_digest_hash_finish_func_t)SHA1_Finish,
};

rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
            rb_digest_make_metadata(&sha1));