Class: JOSE::JWK::KTY_oct

Inherits:
Struct
  • Object
show all
Defined in:
lib/jose/jwk/kty_oct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#octObject

Returns the value of attribute oct

Returns:

  • (Object)

    the current value of oct


1
2
3
# File 'lib/jose/jwk/kty_oct.rb', line 1

def oct
  @oct
end

Class Method Details

.from_map(fields) ⇒ Object

JOSE::JWK callbacks


5
6
7
8
9
10
11
# File 'lib/jose/jwk/kty_oct.rb', line 5

def self.from_map(fields)
  if fields['kty'] == 'oct' and fields['k'].is_a?(String)
    return JOSE::JWK::KTY_oct.new(JOSE.urlsafe_decode64(fields['k'])), fields.except('kty', 'k')
  else
    raise ArgumentError, "invalid 'oct' JWK"
  end
end

.from_oct(binary) ⇒ Object

API functions


117
118
119
120
121
122
123
124
# File 'lib/jose/jwk/kty_oct.rb', line 117

def self.from_oct(binary)
  case binary
  when String
    return JOSE::JWK::KTY_oct.new(binary), JOSE::Map[]
  else
    raise ArgumentError, "'binary' must be a String"
  end
end

.generate_key(size) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jose/jwk/kty_oct.rb', line 70

def self.generate_key(size)
  if size.is_a?(Array) and size.length == 2 and size[0] == :oct
    size = size[1]
  end
  case size
  when Integer
    return from_oct(SecureRandom.random_bytes(size))
  else
    raise ArgumentError, "'size' must be an Integer"
  end
end

Instance Method Details

#block_encryptor(fields) ⇒ Object

JOSE::JWK::KTY callbacks


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jose/jwk/kty_oct.rb', line 31

def block_encryptor(fields)
  if fields and fields['use'] == 'enc' and not fields['alg'].nil? and not fields['enc'].nil?
    jwe = JOSE::Map[
      'alg' => fields['alg'],
      'enc' => fields['enc']
    ]
    if not fields['p2c'].nil?
      jwe = jwe.put('p2c', fields['p2c'])
    end
    if not fields['p2s'].nil?
      jwe = jwe.put('p2s', fields['p2s'])
    end
    return jwe
  else
    enc = case (oct.bytesize * 8)
    when 128
      'A128GCM'
    when 192
      'A192GCM'
    when 256
      'A256GCM'
    when 384
      'A192CBC-HS384'
    when 512
      'A256CBC-HS512'
    else
      raise ArgumentError, "oct of size #{oct.bytesize * 8} has no default block encryptor"
    end
    return JOSE::Map[
      'alg' => 'dir',
      'enc' => enc
    ]
  end
end

#derive_keyObject


66
67
68
# File 'lib/jose/jwk/kty_oct.rb', line 66

def derive_key
  return oct
end

#generate_key(fields) ⇒ Object


82
83
84
85
# File 'lib/jose/jwk/kty_oct.rb', line 82

def generate_key(fields)
  kty, other_fields = JOSE::JWK::KTY_oct.generate_key(oct.bytesize)
  return kty, fields.delete('kid').merge(other_fields)
end

#key_encryptor(fields, key) ⇒ Object


87
88
89
# File 'lib/jose/jwk/kty_oct.rb', line 87

def key_encryptor(fields, key)
  return JOSE::JWK::KTY.key_encryptor(self, fields, key)
end

#sign(message, digest_type) ⇒ Object


91
92
93
# File 'lib/jose/jwk/kty_oct.rb', line 91

def sign(message, digest_type)
  return OpenSSL::HMAC.digest(digest_type.new, oct, message)
end

#signer(fields = nil) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jose/jwk/kty_oct.rb', line 95

def signer(fields = nil)
  if fields and fields['use'] == 'sig' and not fields['alg'].nil?
    return JOSE::Map['alg' => fields['alg']]
  else
    bitsize = (oct.bytesize * 8)
    alg = if bitsize < 384
      'HS256'
    elsif bitsize < 512
      'HS384'
    else
      'HS512'
    end
    return JOSE::Map['alg' => alg]
  end
end

#to_keyObject


13
14
15
# File 'lib/jose/jwk/kty_oct.rb', line 13

def to_key
  return oct
end

#to_map(fields) ⇒ Object


17
18
19
# File 'lib/jose/jwk/kty_oct.rb', line 17

def to_map(fields)
  return fields.put('k', JOSE.urlsafe_encode64(oct)).put('kty', 'oct')
end

#to_octObject


126
127
128
# File 'lib/jose/jwk/kty_oct.rb', line 126

def to_oct
  return oct
end

#to_public_map(fields) ⇒ Object


21
22
23
# File 'lib/jose/jwk/kty_oct.rb', line 21

def to_public_map(fields)
  return to_map(fields)
end

#to_thumbprint_map(fields) ⇒ Object


25
26
27
# File 'lib/jose/jwk/kty_oct.rb', line 25

def to_thumbprint_map(fields)
  return to_public_map(fields).slice('k', 'kty')
end

#verify(message, digest_type, signature) ⇒ Object


111
112
113
# File 'lib/jose/jwk/kty_oct.rb', line 111

def verify(message, digest_type, signature)
  return JOSE::JWA.constant_time_compare(signature, sign(message, digest_type))
end