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

#generate_key(fields) ⇒ Object

JOSE::JWS::ALG callbacks



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jose/jws/alg_hmac.rb', line 33

def generate_key(fields)
  bytesize, alg = if hmac == OpenSSL::Digest::SHA256
    [32, 'HS256']
  elsif hmac == OpenSSL::Digest::SHA384
    [48, 'HS384']
  elsif hmac == OpenSSL::Digest::SHA512
    [64, 'HS512']
  else
    raise ArgumentError, "unhandled HMAC digest type: #{hmac.inspect}"
  end
  return JOSE::JWS::ALG.generate_key([:oct, bytesize], alg)
end

#sign(jwk, message) ⇒ Object



46
47
48
# File 'lib/jose/jws/alg_hmac.rb', line 46

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



50
51
52
# File 'lib/jose/jws/alg_hmac.rb', line 50

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