Class: SHA3::Digest

Inherits:
Digest::Class
  • Object
show all
Defined in:
ext/sha3/_sha3.c

Overview

SHA3::Digest allows you to compute message digests (interchangeably called “hashes”) of arbitrary data that are cryptographically secure using SHA3 (Keccak) algorithm.

Usage

require 'sha3'

Basics

# Instantiate a new SHA3::Digest class with 256 bit length
s = SHA3::Digest.new(:sha256)
# => #<SHA3::Digest: c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470>

# Update hash state, and compute new value
s.update "Compute Me"
# => #<SHA3::Digest: c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470>

# << is an .update() alias
s << "Me too"
# => #<SHA3::Digest: e26f539eee3a05c52eb1f9439652d23343adea9764f011da232d24cd6d19924a>

# Print digest bytes string
puts s.digest

# Print digest hex string
puts s.hexdigest

Hashing a file

# Compute the hash value for given file, and return the result as hex
s = SHA3::Digest.new(224).file("my_awesome_file.bin").hexdigest

Bit operation

# Compute hash of "011"
SHA3::Digest.compute(:sha224, "\xC0", 3).unpack("H*")
# => ["2b695a6fd92a2b3f3ce9cfca617d22c9bb52815dd59a9719b01bad25"]

Notes

::Digest::Class call sequence ->
| .alloc() ->
| .new() ->
| .update() ->
| .digest or .hexdigest or .inspect -> (Instance.digest or .hexdigest()) ->
--| .alloc() ->
  | .copy() ->
  | .finish() ->