-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
hmac.cr
35 lines (33 loc) · 1.24 KB
/
hmac.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require "./lib_crypto"
require "openssl/algorithm"
# Allows computing Hash-based Message Authentication Code (HMAC).
#
# It is a type of message authentication code (MAC)
# involving a hash function in combination with a key.
#
# HMAC can be used to verify the integrity of a message as well as the authenticity.
#
# See also [RFC2104](https://tools.ietf.org/html/rfc2104.html).
class OpenSSL::HMAC
# Returns the HMAC digest of *data* using the secret *key*.
#
# It may contain non-ASCII bytes, including NUL bytes.
#
# *algorithm* specifies which `OpenSSL::Algorithm` is to be used.
def self.digest(algorithm : OpenSSL::Algorithm, key, data) : Bytes
evp = algorithm.to_evp
key_slice = key.to_slice
data_slice = data.to_slice
buffer = Bytes.new(128)
LibCrypto.hmac(evp, key_slice, key_slice.size, data_slice, data_slice.size, buffer, out buffer_len)
buffer[0, buffer_len.to_i]
end
# Returns the HMAC digest of *data* using the secret *key*,
# formatted as a hexadecimal string. This is necessary to safely transfer
# the digest where binary messages are not allowed.
#
# See also `#digest`.
def self.hexdigest(algorithm : OpenSSL::Algorithm, key, data) : String
digest(algorithm, key, data).hexstring
end
end