Class: ExoBasic::HMACKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/exobasic/encrypt/hmac_keys.rb

Constant Summary collapse

SIZE =
20

Class Method Summary collapse

Class Method Details

.gen_key(size = nil) ⇒ Object


15
16
17
18
19
# File 'lib/exobasic/encrypt/hmac_keys.rb', line 15

def self.gen_key(size=nil)
  s = size.nil? ? HMACKeys::SIZE : s

  SecureRandom.hex(s)
end

.sign_message(key, data) ⇒ Object


21
22
23
24
25
26
# File 'lib/exobasic/encrypt/hmac_keys.rb', line 21

def self.sign_message(key, data)
  signature        = OpenSSL::HMAC.hexdigest('SHA256', key, data)
  signature_base64 = Base64.encode64(signature).gsub("\n", '')

  signature_base64
end

.verify_message(key, signature_base64, data) ⇒ Object


28
29
30
31
32
# File 'lib/exobasic/encrypt/hmac_keys.rb', line 28

def self.verify_message(key, signature_base64, data)
  # Hash the signatures a second time (to protect against timing attacks) and compare them
  Digest::SHA256.base64digest(HMACKeys.sign_message(key, data)) ==
  Digest::SHA256.base64digest(signature_base64)
end