-
Notifications
You must be signed in to change notification settings - Fork 82
/
sha256.rb
44 lines (37 loc) · 1.42 KB
/
sha256.rb
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
36
37
38
39
40
41
42
43
44
# encoding: binary
# frozen_string_literal: true
module RbNaCl
module HMAC
# Computes an authenticator as HMAC-SHA-256
#
# The authenticator can be used at a later time to verify the provenance of
# the message by recomputing the HMAC over the message and then comparing it to
# the provided authenticator. The class provides methods for generating
# signatures and also has a constant-time implementation for checking them.
#
# This is a secret key authenticator, i.e. anyone who can verify signatures
# can also create them.
#
# @see http://nacl.cr.yp.to/auth.html
class SHA256 < Auth
extend Sodium
sodium_type :auth
sodium_primitive :hmacsha256
sodium_type_primitive_constant :BYTES
sodium_type_primitive_constant :KEYBYTES
sodium_function :auth_hmacsha256,
:crypto_auth_hmacsha256,
%i[pointer pointer ulong_long pointer]
sodium_function :auth_hmacsha256_verify,
:crypto_auth_hmacsha256_verify,
%i[pointer pointer ulong_long pointer]
private
def compute_authenticator(authenticator, message)
self.class.auth_hmacsha256(authenticator, message, message.bytesize, key)
end
def verify_message(authenticator, message)
self.class.auth_hmacsha256_verify(authenticator, message, message.bytesize, key)
end
end
end
end