Class: ExoBasic::HMACKeys
- Inherits:
-
Object
- Object
- ExoBasic::HMACKeys
- Defined in:
- lib/exobasic/encrypt/hmac_keys.rb
Constant Summary collapse
- SIZE =
20
Class Method Summary collapse
- .gen_key(size = nil) ⇒ Object
- .sign_message(key, data) ⇒ Object
- .verify_message(key, signature_base64, data) ⇒ Object
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.(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.(key, signature_base64, data) # Hash the signatures a second time (to protect against timing attacks) and compare them Digest::SHA256.base64digest(HMACKeys.(key, data)) == Digest::SHA256.base64digest(signature_base64) end |