Module: RbNaCl::Hash

Defined in:
lib/rbnacl/hash.rb,
lib/rbnacl/hash/sha256.rb,
lib/rbnacl/hash/sha512.rb,
lib/rbnacl/hash/blake2b.rb

Overview

Cryptographic hash functions

Cryptographic hash functions take a variable length message and compute a fixed length string, the message digest. Even a small change in the input data should produce a large change in the digest, and it is ‘very difficult’ to create two messages with the same digest.

A cryptographic hash can be used for checking the integrity of data, but there is no secret involved in the hashing, so anyone can create the hash of a given message.

RbNaCl provides the SHA-256,SHA-512 as well as the Blake2b hash functions.

Defined Under Namespace

Modules: SHA256, SHA512 Classes: Blake2b

Class Method Summary collapse

Class Method Details

.blake2b(data, options = {}) ⇒ String

Returns the Blake2b hash of the given data

There’s no streaming done, just pass in the data and be done with it. This method returns a 64-byte hash by default.

Parameters:

  • data (String)

    The data, as a collection of bytes

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • digest_size (Fixnum)

    Size in bytes (1-64, default 64)

  • key (String)

    64-byte (or less) key for keyed mode

  • salt (String)

    Provide a salt to support randomised hashing. This is mixed into the parameters block to start the hashing.

  • personal (Personal)

    Provide personalisation string to allow pinning a hash for a particular purpose. This is mixed into the parameters block to start the hashing

Returns:

  • (String)

    The Blake2b hash digest as raw bytes

Raises:

  • (CryptoError)

    If the hashing fails for some reason.


65
66
67
# File 'lib/rbnacl/hash.rb', line 65

def self.blake2b(data, options = {})
  Blake2b.digest(data, options)
end

.sha256(data) ⇒ String

Returns the SHA-256 hash of the given data

There’s no streaming done, just pass in the data and be done with it.

Parameters:

  • data (#to_str)

    The data, as a collection of bytes

Returns:

  • (String)

    The SHA-256 hash digest as raw bytes

Raises:

  • (CryptoError)

    If the hashing fails for some reason.


27
28
29
30
31
32
# File 'lib/rbnacl/hash.rb', line 27

def self.sha256(data)
  data   = data.to_str
  digest = Util.zeros(SHA256::BYTES)
  SHA256.hash_sha256(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
  digest
end

.sha512(data) ⇒ String

Returns the SHA-512 hash of the given data

There’s no streaming done, just pass in the data and be done with it.

Parameters:

  • data (#to_str)

    The data, as a collection of bytes

Returns:

  • (String)

    The SHA-512 hash digest as raw bytes

Raises:

  • (CryptoError)

    If the hashing fails for some reason.


43
44
45
46
47
# File 'lib/rbnacl/hash.rb', line 43

def self.sha512(data)
  digest = Util.zeros(SHA512::BYTES)
  SHA512.hash_sha512(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
  digest
end