class OpenSSL::Digest - Documentation for Ruby 4.0
Description: class OpenSSL::Digest: Digest allows you to compute message digests (sometimes interchangeably called "hashes") of arbitrary data that are cryptographically secure, i. a Dig
Keywords: ruby,class,OpenSSL::Digest

🔍

No recent searches

# 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.

You can get a list of all digest algorithms supported on your system by running this command in your terminal:

    openssl list -digest-algorithms

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')

“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

    data = File.binread('document')
    sha256 = OpenSSL::Digest.new('SHA256')
    digest = sha256.digest(data)

### Hashing several pieces of data at once

    data1 = File.binread('file1')
    data2 = File.binread('file2')
    data3 = File.binread('file3')
    sha256 = OpenSSL::Digest.new('SHA256')
    sha256 << data1
    sha256 << data2
    sha256 << data3
    digest = sha256.digest

### Reuse a `Digest` instance

    data1 = File.binread('file1')
    sha256 = OpenSSL::Digest.new('SHA256')
    digest1 = sha256.digest(data1)

    data2 = File.binread('file2')
    sha256.reset
    digest2 = sha256.digest(data2)

### Public Class Methods

digest (name, data)
Source

    # File ext/openssl/lib/openssl/digest.rb, line 25
    def self.digest(name, data)
      super(data, name)
    end

Return the hash value computed with name `Digest`. name is either the long name or short name of a supported digest algorithm.

### Example

    OpenSSL::Digest.digest("SHA256", "abc")

Calls superclass method
OpenSSL::Digest.digests → array[string...]
Source

    static VALUE
    ossl_s_digests(VALUE self)
    {
        VALUE ary;

        ary = rb_ary_new();
        OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH,
                               add_digest_name_to_ary,
                               (void*)ary);

        return ary;
    }

Returns the names of all available digests in an array.

new(string [, data]) → Digest
Source

    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.

    digest = OpenSSL::Digest.new('sha256', 'digestdata')

is equivalent to

    digest = OpenSSL::Digest.new('sha256')
    digest.update('digestdata')

### Public Instance Methods

<<
Alias for: update
block_length → integer
Source

    static VALUE
    ossl_digest_block_length(VALUE self)
    {
        EVP_MD_CTX *ctx;

        GetDigest(self, ctx);

        return INT2NUM(EVP_MD_CTX_block_size(ctx));
    }

Returns the block length of the digest algorithm, i.e. the length in bytes of an individual block. Most modern algorithms partition a message to be digested into a sequence of fix-sized blocks that are processed consecutively.

### Example

    digest = OpenSSL::Digest.new('SHA1')
    puts digest.block_length # => 64

digest_length → integer
Source

    static VALUE
    ossl_digest_size(VALUE self)
    {
        EVP_MD_CTX *ctx;

        GetDigest(self, ctx);

        return INT2NUM(EVP_MD_CTX_size(ctx));
    }

Returns the output size of the digest, i.e. the length in bytes of the final message digest result.

### Example

    digest = OpenSSL::Digest.new('SHA1')
    puts digest.digest_length # => 20

name → string
Source

    static VALUE
    ossl_digest_name(VALUE self)
    {
        EVP_MD_CTX *ctx;

        GetDigest(self, ctx);

        return rb_str_new_cstr(EVP_MD_name(EVP_MD_CTX_get0_md(ctx)));
    }

Returns the short name of this `Digest` algorithm which may differ slightly from the original name provided.

### Example

    digest = OpenSSL::Digest.new('SHA512')
    puts digest.name # => SHA512

reset → self
Source

    static VALUE
    ossl_digest_reset(VALUE self)
    {
        EVP_MD_CTX *ctx;

        GetDigest(self, ctx);
        if (EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL) != 1) {
            ossl_raise(eDigestError, "Digest initialization failed.");
        }

        return self;
    }

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.

update(string) → aString
Source

    static VALUE
    ossl_digest_update(VALUE self, VALUE data)
    {
        EVP_MD_CTX *ctx;

        StringValue(data);
        GetDigest(self, ctx);

        if (!EVP_DigestUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data)))
            ossl_raise(eDigestError, "EVP_DigestUpdate");

        return self;
    }

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

    digest = OpenSSL::Digest.new('SHA256')
    digest.update('First input')
    digest << 'Second input' # equivalent to digest.update('Second input')
    result = digest.digest

Also aliased as: <<

### Private Instance Methods

finish → aString
Source

    static VALUE
    ossl_digest_finish(VALUE self)
    {
        EVP_MD_CTX *ctx;
        VALUE str;

        GetDigest(self, ctx);
        str = rb_str_new(NULL, EVP_MD_CTX_size(ctx));
        if (!EVP_DigestFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), NULL))
            ossl_raise(eDigestError, "EVP_DigestFinal_ex");

        return str;
    }