Class: RbNaCl::Hash::Blake2b
- Inherits:
-
Object
- Object
- RbNaCl::Hash::Blake2b
- Extended by:
- Sodium
- Defined in:
- lib/rbnacl/hash/blake2b.rb
Overview
The Blake2b hash function
Blake2b is based on Blake, a SHA3 finalist which was snubbed in favor of Keccak, a much slower hash function but one sufficiently different from SHA2 to let the SHA3 judges panel sleep easy. Back in the real world, it'd be great if we can calculate hashes quickly if possible.
Blake2b provides for up to 64-bit digests and also supports a keyed mode similar to HMAC
Constant Summary collapse
- EMPTY_PERSONAL =
("\0" * PERSONALBYTES).freeze
- EMPTY_SALT =
("\0" * SALTBYTES).freeze
Instance Method Summary collapse
-
#digest(message) ⇒ String
Calculate a Blake2b digest.
-
#initialize(opts = {}) ⇒ RbNaCl::Hash::Blake2b
constructor
Create a new Blake2b hash object.
Methods included from Sodium
primitive, sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type
Constructor Details
#initialize(opts = {}) ⇒ RbNaCl::Hash::Blake2b
Create a new Blake2b hash object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rbnacl/hash/blake2b.rb', line 47 def initialize(opts = {}) @key = opts.fetch(:key, nil) if @key @key_size = @key.bytesize raise LengthError, "key too short" if @key_size < KEYBYTES_MIN raise LengthError, "key too long" if @key_size > KEYBYTES_MAX else @key_size = 0 end @digest_size = opts.fetch(:digest_size, BYTES_MAX) raise LengthError, "digest size too short" if @digest_size < BYTES_MIN raise LengthError, "digest size too long" if @digest_size > BYTES_MAX @personal = opts.fetch(:personal, EMPTY_PERSONAL) @personal = Util.zero_pad(PERSONALBYTES, @personal) @salt = opts.fetch(:salt, EMPTY_SALT) @salt = Util.zero_pad(SALTBYTES, @salt) end |
Instance Method Details
#digest(message) ⇒ String
Calculate a Blake2b digest
74 75 76 77 78 79 |
# File 'lib/rbnacl/hash/blake2b.rb', line 74 def digest() digest = Util.zeros(@digest_size) self.class.generichash_blake2b(digest, @digest_size, , .bytesize, @key, @key_size, @salt, @personal) || raise(CryptoError, "Hashing failed!") digest end |