Class: JOSE::JWS::ALG_HMAC

Inherits:
Struct
  • Object
show all
Defined in:
lib/jose/jws/alg_hmac.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hmacObject

Returns the value of attribute hmac

Returns:

  • (Object)

    the current value of hmac



1
2
3
# File 'lib/jose/jws/alg_hmac.rb', line 1

def hmac
  @hmac
end

Class Method Details

.from_map(fields) ⇒ Object

JOSE::JWS callbacks



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jose/jws/alg_hmac.rb', line 5

def self.from_map(fields)
  case fields['alg']
  when 'HS256'
    return new(OpenSSL::Digest::SHA256), fields.delete('alg')
  when 'HS384'
    return new(OpenSSL::Digest::SHA384), fields.delete('alg')
  when 'HS512'
    return new(OpenSSL::Digest::SHA512), fields.delete('alg')
  else
    raise ArgumentError, "invalid 'alg' for JWS: #{fields['alg'].inspect}"
  end
end

Instance Method Details

#sign(jwk, message) ⇒ Object

JOSE::JWS::ALG callbacks



33
34
35
# File 'lib/jose/jws/alg_hmac.rb', line 33

def sign(jwk, message)
  return jwk.kty.sign(message, hmac)
end

#to_map(fields) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jose/jws/alg_hmac.rb', line 18

def to_map(fields)
  alg = if hmac == OpenSSL::Digest::SHA256
    'HS256'
  elsif hmac == OpenSSL::Digest::SHA384
    'HS384'
  elsif hmac == OpenSSL::Digest::SHA512
    'HS512'
  else
    raise ArgumentError, "unhandled HMAC digest type: #{hmac.inspect}"
  end
  return fields.put('alg', alg)
end

#verify(jwk, message, signature) ⇒ Object



37
38
39
# File 'lib/jose/jws/alg_hmac.rb', line 37

def verify(jwk, message, signature)
  return jwk.kty.verify(message, hmac, signature)
end