-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from cryptosphere/blake2b-keyed
Support for Blake2b in keyed mode
- Loading branch information
Showing
4 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Crypto | ||
module Hash | ||
# 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 | ||
class Blake2b | ||
# Create a new Blake2b hash object | ||
# | ||
# @param [Hash] opts Blake2b configuration | ||
# @option opts [String] :key for Blake2b keyed mode | ||
# @option opts [Integer] :digest_size size of output digest in bytes | ||
# | ||
# @raise [Crypto::LengthError] Invalid length specified for one or more options | ||
# | ||
# @return [Crypto::Hash::Blake2b] A Blake2b hasher object | ||
def initialize(opts = {}) | ||
@key = opts.fetch(:key, nil) | ||
@key_size = @key ? @key.bytesize : 0 | ||
raise LengthError, "key too long" if @key_size > NaCl::BLAKE2B_KEYBYTES | ||
|
||
@digest_size = opts.fetch(:digest_size, NaCl::BLAKE2B_OUTBYTES) | ||
raise LengthError, "invalid digest size" if @digest_size < 1 || @digest_size > NaCl::BLAKE2B_OUTBYTES | ||
end | ||
|
||
# Calculate a Blake2b hash | ||
# | ||
# @param [String] message Message to be hashed | ||
# @param [#to_sym] encoding Encoding of the returned hash | ||
# | ||
# @return [String] Blake2b digest of the string as raw bytes | ||
def hash(message, encoding = :raw) | ||
digest = Util.zeros(@digest_size) | ||
NaCl.crypto_hash_blake2b(digest, @digest_size, message, message.bytesize, @key, @key_size) || raise(CryptoError, "Hashing failed!") | ||
Encoder[encoding].encode(digest) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters