class Sodium::Digest::Blake2b

Overview

Hash data using Blake2b.

Compatible with the Crystal OpenSSL::Digest interface.

Usage:

digest = Blake2b.new
digest.update data
digest.update data
digest.hexfinal => String

Included Modules

Defined in:

sodium/digest/blake2b.cr

Constant Summary

KEY_SIZE = LibSodium.crypto_generichash_blake2b_keybytes.to_i

32

KEY_SIZE_MAX = LibSodium.crypto_generichash_blake2b_keybytes_max.to_i

64

KEY_SIZE_MIN = LibSodium.crypto_generichash_blake2b_keybytes_min.to_i

16

Log = ::Log.for(self)
OUT_SIZE = LibSodium.crypto_generichash_blake2b_bytes.to_i32

32

OUT_SIZE_MAX = LibSodium.crypto_generichash_blake2b_bytes_max.to_i32

64

OUT_SIZE_MIN = LibSodium.crypto_generichash_blake2b_bytes_min.to_i32

16

PERSONAL_SIZE = LibSodium.crypto_generichash_blake2b_personalbytes.to_i

16

SALT_SIZE = LibSodium.crypto_generichash_blake2b_saltbytes.to_i

16

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Sodium::Wipe

close close, finalize finalize

Constructor Detail

def self.new(digest_size : Int32 = OUT_SIZE, key : Bytes? | SecureBuffer? = nil, salt : Bytes? = nil, personal : Bytes? = nil) #

Create a new Blake2b Digest.

digest_size is selectable. Use 32 for Blake2b256 (libsodium default), 64 for Blake2b512 or any value between OUT_SIZE_MIN and OUT_SIZE_MAX. Many libsodium bindings only support [256] or [256 and 512] bit output.

key, salt, and personal are all optional. Many other libsodium bindings don't support them. Check the other implementation(s) you need to interoperate with before using.


[View source]

Class Method Detail

def self.base64digest(data) : String #

Returns the base64-encoded hash of data.

require "digest/sha1"

Digest::SHA1.base64digest("foo") # => "C+7Hteo/D9vJXQ3UfzxbwnXaijM="

[View source]
def self.base64digest(& : self -> _) : String #

Yields a context object with an #update(data : String | Bytes) method available. Returns the resulting digest in base64 representation afterwards.

require "digest/sha1"

Digest::SHA1.base64digest do |ctx|
  ctx.update "f"
  ctx.update "oo"
end
# => "C+7Hteo/D9vJXQ3UfzxbwnXaijM="

[View source]
def self.digest(data) #

Returns the hash of data. data must respond to #to_slice.


[View source]
def self.digest(& : self -> ) : Bytes #

Yields an instance of self which can receive calls to #update(data : String | Bytes) and returns the finalized digest afterwards.

require "digest/md5"

digest = Digest::MD5.digest do |ctx|
  ctx.update "f"
  ctx.update "oo"
end
digest.to_slice.hexstring # => "acbd18db4cc2f85cedef654fccc4a4d8"

[View source]
def self.hexdigest(data) : String #

Returns the hexadecimal representation of the hash of data.

require "digest/md5"

Digest::MD5.hexdigest("foo") # => "acbd18db4cc2f85cedef654fccc4a4d8"

[View source]
def self.hexdigest(& : self -> ) : String #

Yields a context object with an #update(data : String | Bytes) method available. Returns the resulting digest in hexadecimal representation afterwards.

require "digest/md5"

Digest::MD5.hexdigest("foo") # => "acbd18db4cc2f85cedef654fccc4a4d8"
Digest::MD5.hexdigest do |ctx|
  ctx.update "f"
  ctx.update "oo"
end
# => "acbd18db4cc2f85cedef654fccc4a4d8"

[View source]

Instance Method Detail

def clone #

[View source]
def digest_size : Int32 #
Description copied from class Digest::Base

Returns the digest output size in bytes.


[View source]
def final_impl(dst : Bytes) : Nil #
Description copied from class Digest::Base

Stores the output digest of #digest_size bytes in dst.


[View source]
def hexfinal(dst : Bytes) : Nil #

[View source]
def hexfinal : String #

[View source]
def key_size : Int32 #

[View source]
def reset_impl : Nil #
Description copied from class Digest::Base

Resets the object to it's initial state.


[View source]
def update_impl(data : Bytes) : Nil #
Description copied from class Digest::Base

Hashes data incrementally.


[View source]