Class: JOSE::JWS::ALG_ECDSA

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#digestObject

Returns the value of attribute digest


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

def digest
  @digest
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_ecdsa.rb', line 5

def self.from_map(fields)
  case fields['alg']
  when 'ES256'
    return new(OpenSSL::Digest::SHA256), fields.delete('alg')
  when 'ES384'
    return new(OpenSSL::Digest::SHA384), fields.delete('alg')
  when 'ES512'
    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_ecdsa.rb', line 33

def generate_key(fields)
  crv, alg = if digest == OpenSSL::Digest::SHA256
    ['P-256', 'ES256']
  elsif digest == OpenSSL::Digest::SHA384
    ['P-384', 'ES384']
  elsif digest == OpenSSL::Digest::SHA512
    ['P-521', 'ES512']
  else
    raise ArgumentError, "unhandled ECDSA digest type: #{digest.inspect}"
  end
  return JOSE::JWS::ALG.generate_key([:ec, crv], alg)
end

#sign(jwk, message) ⇒ Object


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

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

#to_map(fields) ⇒ Object


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

def to_map(fields)
  alg = if digest == OpenSSL::Digest::SHA256
    'ES256'
  elsif digest == OpenSSL::Digest::SHA384
    'ES384'
  elsif digest == OpenSSL::Digest::SHA512
    'ES512'
  else
    raise ArgumentError, "unhandled ECDSA digest type: #{digest.inspect}"
  end
  return fields.put('alg', alg)
end

#verify(jwk, message, signature) ⇒ Object


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

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