Skip to content

abstract class Digest
inherits Reference

Digest is the base type of hashing algorithms like Digest::MD5, Digest::SHA1, Digest::SHA256, or Digest::SHA512.

A Digest instance holds the state of an ongoing hash calculation. It can receive new data to include in the hash via #update, #<<, or #file. Once all data is included, use #final to get the hash. This will mark the ongoing calculation as finished. A finished calculation can't receive new data.

A digest.dup.final call may be used to get an intermediate hash value.

Use #reset to reuse the Digest instance for a new calculation.

Direct known subclasses

Crystal::Digest::MD5 OpenSSL::Digest

Methods

#<<(data) : self

Reads the io's data and updates the digest with it.

View source

abstract #digest_size : Int32

Returns the digest output size in bytes.

View source

#file(file_name : Path | String) : self

Reads the file's content and updates the digest with it.

View source

#final(dst : Bytes) : Bytes

View source

#final : Bytes

Returns the final digest output.

This method can only be called once and raises FinalizedError on subsequent calls.

Note

.dup.final call may be used to get an intermediate hash value.

View source

abstract #final_impl(dst : Bytes) : Nil

Stores the output digest of #digest_size bytes in dst.

View source

#reset : self

View source

abstract #reset_impl : Nil

Resets the object to it's initial state.

View source

#update(data : Bytes) : self

View source

#update(io : IO) : self

Reads the io's data and updates the digest with it.

View source

#update(data) : self

View source

abstract #update_impl(data : Bytes) : Nil

Hashes data incrementally.

View source