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
Defined Under Namespace
Classes: State
Constant Summary collapse
- EMPTY_PERSONAL =
("\0" * PERSONALBYTES).freeze
- EMPTY_SALT =
("\0" * SALTBYTES).freeze
Class Method Summary collapse
-
.digest(message, options) ⇒ String
Calculate a Blake2b digest.
- .new(opts = {}) ⇒ Object
Instance Method Summary collapse
-
#digest ⇒ String
Finalize digest calculation, return cached digest if any.
-
#initialize(opts = {}) ⇒ RbNaCl::Hash::Blake2b
constructor
Create a new Blake2b hash object.
-
#reset ⇒ Object
Initialize state for Blake2b hash calculation, this will be called automatically from #update if needed.
-
#update(message) ⇒ Object
(also: #<<)
Reentrant version of Blake2b digest calculation method.
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
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rbnacl/hash/blake2b.rb', line 127 def initialize(opts = {}) @key = opts[:key] @key_size = opts[:key_size] @digest_size = opts[:digest_size] @personal = opts[:personal] @salt = opts[:salt] @incycle = false @instate = nil end |
Class Method Details
.digest(message, options) ⇒ String
Calculate a Blake2b digest
60 61 62 63 64 65 66 67 |
# File 'lib/rbnacl/hash/blake2b.rb', line 60 def self.digest(, ) opts = validate_opts() digest = Util.zeros(opts[:digest_size]) generichash_blake2b(digest, opts[:digest_size], , .bytesize, opts[:key], opts[:key_size], opts[:salt], opts[:personal]) || raise(CryptoError, "Hashing failed!") digest end |
.new(opts = {}) ⇒ Object
109 110 111 112 |
# File 'lib/rbnacl/hash/blake2b.rb', line 109 def self.new(opts = {}) opts = validate_opts(opts) super end |
Instance Method Details
#digest ⇒ String
Finalize digest calculation, return cached digest if any
162 163 164 165 166 167 168 169 170 |
# File 'lib/rbnacl/hash/blake2b.rb', line 162 def digest raise(CryptoError, "No message to hash yet!") unless @incycle return @digest if @digest @digest = Util.zeros(@digest_size) self.class.generichash_blake2b_final(@instate.pointer, @digest, @digest_size) || raise(CryptoError, "Hash finalization failed!") @digest end |
#reset ⇒ Object
Initialize state for Blake2b hash calculation, this will be called automatically from #update if needed
140 141 142 143 144 145 146 147 |
# File 'lib/rbnacl/hash/blake2b.rb', line 140 def reset @instate.release if @instate @instate = State.new self.class.generichash_blake2b_init(@instate.pointer, @key, @key_size, @digest_size, @salt, @personal) || raise(CryptoError, "Hash init failed!") @incycle = true @digest = nil end |
#update(message) ⇒ Object Also known as: <<
Reentrant version of Blake2b digest calculation method
152 153 154 155 156 |
# File 'lib/rbnacl/hash/blake2b.rb', line 152 def update() reset unless @incycle self.class.generichash_blake2b_update(@instate.pointer, , .bytesize) || raise(CryptoError, "Hashing failed!") end |