class method
OpenSSL::Digest.new
(::String name, ?::String data) -> ::OpenSSL::Digest
Digest.new(string [, data]) → Digest
ext/openssl/ossl_digest.c
View on GitHub ↗
static VALUE
ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
{
EVP_MD_CTX *ctx;
const EVP_MD *md;
VALUE type, data, md_holder;
rb_scan_args(argc, argv, "11", &type, &data);
md = ossl_evp_md_fetch(type, &md_holder);
if (!NIL_P(data)) StringValue(data);
TypedData_Get_Struct(self, EVP_MD_CTX, &ossl_digest_type, ctx);
if (!ctx) {
RTYPEDDATA_DATA(self) = ctx = EVP_MD_CTX_new();
if (!ctx)
ossl_raise(eDigestError, "EVP_MD_CTX_new");
}
if (!EVP_DigestInit_ex(ctx, md, NULL))
ossl_raise(eDigestError, "Digest initialization failed");
rb_ivar_set(self, id_md_holder, md_holder);
if (!NIL_P(data)) return ossl_digest_update(self, data);
return self;
}
Creates a Digest instance based on string, which is either the ln (long name) or sn (short name) of a supported digest algorithm. A list of supported algorithms can be obtained by calling OpenSSL::Digest.digests.
If data (a String) is given, it is used as the initial input to the Digest instance, i.e.
Example
digest = OpenSSL::Digest.new('sha256', 'digestdata')
is equivalent to
Example
digest = OpenSSL::Digest.new('sha256') digest.update('digestdata')