2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 12:12:24 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
#define GetDigest(obj, ctx) do { \
|
2014-12-12 16:57:44 -05:00
|
|
|
TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_digest_type, (ctx)); \
|
2011-02-22 07:39:43 -05:00
|
|
|
if (!(ctx)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cDigest;
|
|
|
|
VALUE eDigestError;
|
|
|
|
|
2003-09-05 05:08:40 -04:00
|
|
|
static VALUE ossl_digest_alloc(VALUE klass);
|
|
|
|
|
2014-12-12 16:57:44 -05:00
|
|
|
static void
|
|
|
|
ossl_digest_free(void *ctx)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX_destroy(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_digest_type = {
|
|
|
|
"OpenSSL/Digest",
|
|
|
|
{
|
|
|
|
0, ossl_digest_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
|
|
|
const EVP_MD *
|
2017-09-03 08:35:27 -04:00
|
|
|
ossl_evp_get_digestbyname(VALUE obj)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2007-03-29 13:29:03 -04:00
|
|
|
const EVP_MD *md;
|
2011-06-12 22:37:35 -04:00
|
|
|
ASN1_OBJECT *oid = NULL;
|
2007-03-29 13:29:03 -04:00
|
|
|
|
2014-08-02 21:56:01 -04:00
|
|
|
if (RB_TYPE_P(obj, T_STRING)) {
|
2010-02-23 12:33:39 -05:00
|
|
|
const char *name = StringValueCStr(obj);
|
2010-02-23 10:51:01 -05:00
|
|
|
|
2011-06-13 00:09:04 -04:00
|
|
|
md = EVP_get_digestbyname(name);
|
|
|
|
if (!md) {
|
|
|
|
oid = OBJ_txt2obj(name, 0);
|
|
|
|
md = EVP_get_digestbyobj(oid);
|
|
|
|
ASN1_OBJECT_free(oid);
|
|
|
|
}
|
2011-06-12 22:37:35 -04:00
|
|
|
if(!md)
|
2016-05-23 07:40:07 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%"PRIsVALUE").", obj);
|
2010-02-23 12:33:39 -05:00
|
|
|
} else {
|
2007-03-29 13:29:03 -04:00
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
GetDigest(obj, ctx);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
md = EVP_MD_CTX_md(ctx);
|
2007-03-29 13:29:03 -04:00
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
return md;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2003-09-05 05:08:40 -04:00
|
|
|
VALUE
|
|
|
|
ossl_digest_new(const EVP_MD *md)
|
2010-04-22 04:04:13 -04:00
|
|
|
{
|
2003-09-05 05:08:40 -04:00
|
|
|
VALUE ret;
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
ret = ossl_digest_alloc(cDigest);
|
2016-11-30 09:41:46 -05:00
|
|
|
ctx = EVP_MD_CTX_new();
|
|
|
|
if (!ctx)
|
|
|
|
ossl_raise(eDigestError, "EVP_MD_CTX_new");
|
|
|
|
RTYPEDDATA_DATA(ret) = ctx;
|
|
|
|
|
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
|
|
|
ossl_raise(eDigestError, "Digest initialization failed");
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-09-05 05:08:40 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_digest_alloc(VALUE klass)
|
|
|
|
{
|
2016-11-30 09:41:46 -05:00
|
|
|
return TypedData_Wrap_Struct(klass, &ossl_digest_type, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE ossl_digest_update(VALUE, VALUE);
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-05-16 19:07:58 -04:00
|
|
|
* Digest.new(string [, data]) -> Digest
|
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* Creates a Digest instance based on _string_, which is either the ln
|
2011-05-16 19:07:58 -04:00
|
|
|
* (long name) or sn (short name) of a supported digest algorithm.
|
2016-08-29 01:47:09 -04:00
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* If _data_ (a String) is given, it is used as the initial input to the
|
2011-05-16 19:07:58 -04:00
|
|
|
* Digest instance, i.e.
|
2016-08-29 01:47:09 -04:00
|
|
|
*
|
2011-05-16 19:07:58 -04:00
|
|
|
* digest = OpenSSL::Digest.new('sha256', 'digestdata')
|
2016-08-29 01:47:09 -04:00
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* is equivalent to
|
2016-08-29 01:47:09 -04:00
|
|
|
*
|
2011-05-16 19:07:58 -04:00
|
|
|
* digest = OpenSSL::Digest.new('sha256')
|
|
|
|
* digest.update('digestdata')
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_digest_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
const EVP_MD *md;
|
|
|
|
VALUE type, data;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &type, &data);
|
2017-09-03 08:35:27 -04:00
|
|
|
md = ossl_evp_get_digestbyname(type);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!NIL_P(data)) StringValue(data);
|
2008-02-25 03:51:18 -05:00
|
|
|
|
2016-11-30 09:41:46 -05:00
|
|
|
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");
|
2011-07-21 22:21:26 -04:00
|
|
|
}
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2016-11-30 09:41:46 -05:00
|
|
|
if (!EVP_DigestInit_ex(ctx, md, NULL))
|
|
|
|
ossl_raise(eDigestError, "Digest initialization failed");
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!NIL_P(data)) return ossl_digest_update(self, data);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_digest_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx1, *ctx2;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_check_frozen(self);
|
|
|
|
if (self == other) return self;
|
|
|
|
|
2016-11-30 09:41:46 -05:00
|
|
|
TypedData_Get_Struct(self, EVP_MD_CTX, &ossl_digest_type, ctx1);
|
|
|
|
if (!ctx1) {
|
|
|
|
RTYPEDDATA_DATA(self) = ctx1 = EVP_MD_CTX_new();
|
|
|
|
if (!ctx1)
|
|
|
|
ossl_raise(eDigestError, "EVP_MD_CTX_new");
|
|
|
|
}
|
2017-09-03 08:35:27 -04:00
|
|
|
GetDigest(other, ctx2);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
if (!EVP_MD_CTX_copy(ctx1, ctx2)) {
|
|
|
|
ossl_raise(eDigestError, NULL);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* digest.reset -> self
|
|
|
|
*
|
2011-05-16 19:07:58 -04:00
|
|
|
* Resets the Digest in the sense that any Digest#update that has been
|
|
|
|
* performed is abandoned and the Digest is set to its initial state again.
|
|
|
|
*
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_digest_reset(VALUE self)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
2011-07-21 22:21:26 -04:00
|
|
|
if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_md(ctx), NULL) != 1) {
|
|
|
|
ossl_raise(eDigestError, "Digest initialization failed.");
|
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* digest.update(string) -> aString
|
|
|
|
*
|
2011-05-16 19:07:58 -04:00
|
|
|
* Not every message digest can be computed in one single pass. If a message
|
|
|
|
* digest is to be computed from several subsequent sources, then each may
|
|
|
|
* be passed individually to the Digest instance.
|
|
|
|
*
|
|
|
|
* === Example
|
2020-04-19 11:14:36 -04:00
|
|
|
* digest = OpenSSL::Digest.new('SHA256')
|
2011-05-16 19:07:58 -04:00
|
|
|
* digest.update('First input')
|
|
|
|
* digest << 'Second input' # equivalent to digest.update('Second input')
|
|
|
|
* result = digest.digest
|
|
|
|
*
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE
|
|
|
|
ossl_digest_update(VALUE self, VALUE data)
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
StringValue(data);
|
2004-12-15 01:35:55 -05:00
|
|
|
GetDigest(self, ctx);
|
2016-08-29 01:47:09 -04:00
|
|
|
|
|
|
|
if (!EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data)))
|
|
|
|
ossl_raise(eDigestError, "EVP_DigestUpdate");
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-02-25 03:51:18 -05:00
|
|
|
* digest.finish -> aString
|
2007-03-29 13:29:03 -04:00
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2008-02-25 03:51:18 -05:00
|
|
|
ossl_digest_finish(int argc, VALUE *argv, VALUE self)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
2008-02-25 03:51:18 -05:00
|
|
|
VALUE str;
|
2016-08-29 01:47:09 -04:00
|
|
|
int out_len;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_scan_args(argc, argv, "01", &str);
|
|
|
|
out_len = EVP_MD_CTX_size(ctx);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
if (NIL_P(str)) {
|
2016-08-29 01:47:09 -04:00
|
|
|
str = rb_str_new(NULL, out_len);
|
2008-02-25 03:51:18 -05:00
|
|
|
} else {
|
|
|
|
StringValue(str);
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_str_resize(str, out_len);
|
2008-02-25 03:51:18 -05:00
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), NULL))
|
|
|
|
ossl_raise(eDigestError, "EVP_DigestFinal_ex");
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
return str;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-02-25 03:51:18 -05:00
|
|
|
* digest.name -> string
|
2007-03-29 13:29:03 -04:00
|
|
|
*
|
2016-08-29 01:47:09 -04:00
|
|
|
* Returns the sn of this Digest algorithm.
|
2011-05-16 19:07:58 -04:00
|
|
|
*
|
|
|
|
* === Example
|
2020-04-19 11:14:36 -04:00
|
|
|
* digest = OpenSSL::Digest.new('SHA512')
|
2011-05-16 19:07:58 -04:00
|
|
|
* puts digest.name # => SHA512
|
|
|
|
*
|
2007-03-29 13:29:03 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2008-02-25 03:51:18 -05:00
|
|
|
ossl_digest_name(VALUE self)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
GetDigest(self, ctx);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
return rb_str_new2(EVP_MD_name(EVP_MD_CTX_md(ctx)));
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2007-03-29 13:29:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-05-16 19:07:58 -04:00
|
|
|
* digest.digest_length -> integer
|
|
|
|
*
|
|
|
|
* Returns the output size of the digest, i.e. the length in bytes of the
|
|
|
|
* final message digest result.
|
|
|
|
*
|
|
|
|
* === Example
|
2020-04-19 11:14:36 -04:00
|
|
|
* digest = OpenSSL::Digest.new('SHA1')
|
2011-05-16 19:07:58 -04:00
|
|
|
* puts digest.digest_length # => 20
|
2007-03-29 13:29:03 -04:00
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2008-02-25 03:51:18 -05:00
|
|
|
ossl_digest_size(VALUE self)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
return INT2NUM(EVP_MD_CTX_size(ctx));
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2011-05-16 19:07:58 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* digest.block_length -> integer
|
|
|
|
*
|
|
|
|
* Returns the block length of the digest algorithm, i.e. the length in bytes
|
2011-06-12 22:37:35 -04:00
|
|
|
* of an individual block. Most modern algorithms partition a message to be
|
|
|
|
* digested into a sequence of fix-sized blocks that are processed
|
|
|
|
* consecutively.
|
2011-05-16 19:07:58 -04:00
|
|
|
*
|
|
|
|
* === Example
|
2020-04-19 11:14:36 -04:00
|
|
|
* digest = OpenSSL::Digest.new('SHA1')
|
2011-05-16 19:07:58 -04:00
|
|
|
* puts digest.block_length # => 64
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2008-02-25 03:51:18 -05:00
|
|
|
ossl_digest_block_length(VALUE self)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
|
|
|
|
|
|
|
GetDigest(self, ctx);
|
|
|
|
|
2008-02-25 03:51:18 -05:00
|
|
|
return INT2NUM(EVP_MD_CTX_block_size(ctx));
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_digest(void)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2008-02-25 03:51:18 -05:00
|
|
|
rb_require("digest");
|
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
#if 0
|
2016-08-29 01:47:09 -04:00
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
2007-03-11 22:01:19 -04:00
|
|
|
#endif
|
|
|
|
|
2011-05-16 19:07:58 -04:00
|
|
|
/* Document-class: OpenSSL::Digest
|
|
|
|
*
|
|
|
|
* OpenSSL::Digest allows you to compute message digests (sometimes
|
|
|
|
* interchangeably called "hashes") of arbitrary data that are
|
|
|
|
* cryptographically secure, i.e. a Digest implements a secure one-way
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* One-way functions offer some useful properties. E.g. given two
|
|
|
|
* distinct inputs the probability that both yield the same output
|
|
|
|
* is highly unlikely. Combined with the fact that every message digest
|
|
|
|
* algorithm has a fixed-length output of just a few bytes, digests are
|
|
|
|
* often used to create unique identifiers for arbitrary data. A common
|
|
|
|
* example is the creation of a unique id for binary documents that are
|
|
|
|
* stored in a database.
|
|
|
|
*
|
|
|
|
* Another useful characteristic of one-way functions (and thus the name)
|
|
|
|
* is that given a digest there is no indication about the original
|
|
|
|
* data that produced it, i.e. the only way to identify the original input
|
|
|
|
* is to "brute-force" through every possible combination of inputs.
|
|
|
|
*
|
|
|
|
* These characteristics make one-way functions also ideal companions
|
|
|
|
* for public key signature algorithms: instead of signing an entire
|
|
|
|
* document, first a hash of the document is produced with a considerably
|
|
|
|
* faster message digest algorithm and only the few bytes of its output
|
|
|
|
* need to be signed using the slower public key algorithm. To validate
|
|
|
|
* the integrity of a signed document, it suffices to re-compute the hash
|
|
|
|
* and verify that it is equal to that in the signature.
|
|
|
|
*
|
2020-04-19 11:14:36 -04:00
|
|
|
* You can get a list of all digest algorithms supported on your system by
|
|
|
|
* running this command in your terminal:
|
2011-05-16 19:07:58 -04:00
|
|
|
*
|
2020-04-19 11:14:36 -04:00
|
|
|
* openssl list -digest-algorithms
|
2011-05-16 19:07:58 -04:00
|
|
|
*
|
2020-04-19 11:14:36 -04:00
|
|
|
* Among the OpenSSL 1.1.1 supported message digest algorithms are:
|
|
|
|
* * SHA224, SHA256, SHA384, SHA512, SHA512-224 and SHA512-256
|
|
|
|
* * SHA3-224, SHA3-256, SHA3-384 and SHA3-512
|
|
|
|
* * BLAKE2s256 and BLAKE2b512
|
|
|
|
*
|
|
|
|
* Each of these algorithms can be instantiated using the name:
|
|
|
|
*
|
|
|
|
* digest = OpenSSL::Digest.new('SHA256')
|
2011-05-16 19:07:58 -04:00
|
|
|
*
|
|
|
|
* "Breaking" a message digest algorithm means defying its one-way
|
|
|
|
* function characteristics, i.e. producing a collision or finding a way
|
|
|
|
* to get to the original data by means that are more efficient than
|
|
|
|
* brute-forcing etc. Most of the supported digest algorithms can be
|
|
|
|
* considered broken in this sense, even the very popular MD5 and SHA1
|
|
|
|
* algorithms. Should security be your highest concern, then you should
|
|
|
|
* probably rely on SHA224, SHA256, SHA384 or SHA512.
|
|
|
|
*
|
|
|
|
* === Hashing a file
|
|
|
|
*
|
2020-05-13 05:15:08 -04:00
|
|
|
* data = File.binread('document')
|
2020-04-19 11:14:36 -04:00
|
|
|
* sha256 = OpenSSL::Digest.new('SHA256')
|
2011-05-16 19:07:58 -04:00
|
|
|
* digest = sha256.digest(data)
|
|
|
|
*
|
|
|
|
* === Hashing several pieces of data at once
|
|
|
|
*
|
2020-05-13 05:15:08 -04:00
|
|
|
* data1 = File.binread('file1')
|
|
|
|
* data2 = File.binread('file2')
|
|
|
|
* data3 = File.binread('file3')
|
2020-04-19 11:14:36 -04:00
|
|
|
* sha256 = OpenSSL::Digest.new('SHA256')
|
2011-05-16 19:07:58 -04:00
|
|
|
* sha256 << data1
|
|
|
|
* sha256 << data2
|
|
|
|
* sha256 << data3
|
|
|
|
* digest = sha256.digest
|
2011-08-24 20:29:02 -04:00
|
|
|
*
|
|
|
|
* === Reuse a Digest instance
|
|
|
|
*
|
2020-05-13 05:15:08 -04:00
|
|
|
* data1 = File.binread('file1')
|
2020-04-19 11:14:36 -04:00
|
|
|
* sha256 = OpenSSL::Digest.new('SHA256')
|
2011-08-24 20:29:02 -04:00
|
|
|
* digest1 = sha256.digest(data1)
|
2011-11-04 03:19:23 -04:00
|
|
|
*
|
2020-05-13 05:15:08 -04:00
|
|
|
* data2 = File.binread('file2')
|
2011-08-24 20:29:02 -04:00
|
|
|
* sha256.reset
|
|
|
|
* digest2 = sha256.digest(data2)
|
|
|
|
*
|
2011-05-16 19:07:58 -04:00
|
|
|
*/
|
2008-02-25 03:51:18 -05:00
|
|
|
cDigest = rb_define_class_under(mOSSL, "Digest", rb_path2class("Digest::Class"));
|
2011-05-16 19:07:58 -04:00
|
|
|
/* Document-class: OpenSSL::Digest::DigestError
|
|
|
|
*
|
|
|
|
* Generic Exception class that is raised if an error occurs during a
|
|
|
|
* Digest operation.
|
|
|
|
*/
|
2007-04-05 01:59:22 -04:00
|
|
|
eDigestError = rb_define_class_under(cDigest, "DigestError", eOSSLError);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_alloc_func(cDigest, ossl_digest_alloc);
|
2008-02-25 03:51:18 -05:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
|
2017-09-03 08:35:27 -04:00
|
|
|
rb_define_method(cDigest, "initialize_copy", ossl_digest_copy, 1);
|
2008-02-25 03:51:18 -05:00
|
|
|
rb_define_method(cDigest, "reset", ossl_digest_reset, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cDigest, "update", ossl_digest_update, 1);
|
|
|
|
rb_define_alias(cDigest, "<<", "update");
|
2008-02-25 03:51:18 -05:00
|
|
|
rb_define_private_method(cDigest, "finish", ossl_digest_finish, -1);
|
|
|
|
rb_define_method(cDigest, "digest_length", ossl_digest_size, 0);
|
|
|
|
rb_define_method(cDigest, "block_length", ossl_digest_block_length, 0);
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cDigest, "name", ossl_digest_name, 0);
|
|
|
|
}
|