Skip to content

Commit 85175aa

Browse files
committed
Reduced JWE module complexity
1 parent 46bb3f8 commit 85175aa

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

lib/jwe.rb

+29-16
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,31 @@ class InvalidData < RuntimeError; end
2222

2323
class << self
2424
def encrypt(payload, key, alg: 'RSA-OAEP', enc: 'A128GCM', **more_headers)
25-
header = { alg: alg, enc: enc }.merge(more_headers)
26-
header.delete(:zip) if header[:zip] == ''
25+
header = generate_header(alg, enc, more_headers)
2726
check_params(header, key)
2827

29-
cipher = Enc.for(enc).new
30-
cipher.cek = key if alg == 'dir'
28+
payload = apply_zip(header, payload, :compress)
3129

32-
payload = Zip.for(header[:zip]).new.compress(payload) if header[:zip]
30+
cipher = Enc.for(enc)
31+
cipher.cek = key if alg == 'dir'
3332

34-
ciphertext = cipher.encrypt(payload, Base64.jwe_encode(header.to_json))
35-
encrypted_cek = Alg.for(alg).new(key).encrypt(cipher.cek)
33+
json_hdr = header.to_json
34+
ciphertext = cipher.encrypt(payload, Base64.jwe_encode(json_hdr))
3635

37-
Serialization::Compact.encode(header.to_json, encrypted_cek, cipher.iv, ciphertext, cipher.tag)
36+
generate_serialization(json_hdr, Alg.encrypt_cek(alg, key, cipher.cek), ciphertext, cipher)
3837
end
3938

4039
def decrypt(payload, key)
4140
header, enc_key, iv, ciphertext, tag = Serialization::Compact.decode(payload)
4241
header = JSON.parse(header)
4342
check_params(header, key)
4443

45-
cek = Alg.for(header['alg']).new(key).decrypt(enc_key)
46-
cipher = Enc.for(header['enc']).new(cek, iv)
47-
cipher.tag = tag
44+
cek = Alg.decrypt_cek(header['alg'], key, enc_key)
45+
cipher = Enc.for(header['enc'], cek, iv, tag)
4846

4947
plaintext = cipher.decrypt(ciphertext, payload.split('.').first)
5048

51-
if header['zip']
52-
Zip.for(header['zip']).new.decompress(plaintext)
53-
else
54-
plaintext
55-
end
49+
apply_zip(header, plaintext, :decompress)
5650
end
5751

5852
def check_params(header, key)
@@ -82,5 +76,24 @@ def param_to_class_name(param)
8276
klass = param.gsub(/[-\+]/, '_').downcase.sub(/^[a-z\d]*/) { $&.capitalize }
8377
klass.gsub(/_([a-z\d]*)/i) { Regexp.last_match(1).capitalize }
8478
end
79+
80+
def apply_zip(header, data, direction)
81+
zip = header[:zip] || header['zip']
82+
if zip
83+
Zip.for(zip).new.send(direction, data)
84+
else
85+
data
86+
end
87+
end
88+
89+
def generate_header(alg, enc, more)
90+
header = { alg: alg, enc: enc }.merge(more)
91+
header.delete(:zip) if header[:zip] == ''
92+
header
93+
end
94+
95+
def generate_serialization(hdr, cek, content, cipher)
96+
Serialization::Compact.encode(hdr, cek, cipher.iv, content, cipher.tag)
97+
end
8598
end
8699
end

lib/jwe/alg.rb

+8
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@ def self.for(alg)
1313
rescue NameError
1414
raise NotImplementedError.new("Unsupported alg type: #{alg}")
1515
end
16+
17+
def self.encrypt_cek(alg, key, cek)
18+
self.for(alg).new(key).encrypt(cek)
19+
end
20+
21+
def self.decrypt_cek(alg, key, encrypted_cek)
22+
self.for(alg).new(key).decrypt(encrypted_cek)
23+
end
1624
end
1725
end

lib/jwe/enc.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
module JWE
99
# Content encryption algorithms namespace
1010
module Enc
11-
def self.for(enc)
12-
const_get(JWE.param_to_class_name(enc))
11+
def self.for(enc, cek = nil, iv = nil, tag = nil)
12+
klass = const_get(JWE.param_to_class_name(enc))
13+
inst = klass.new(cek, iv)
14+
inst.tag = tag if tag
15+
inst
1316
rescue NameError
1417
raise NotImplementedError.new("Unsupported enc type: #{enc}")
1518
end

spec/jwe/enc_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
describe JWE::Enc do
99
describe '.for' do
10-
it 'returns a class for the specified enc' do
11-
expect(JWE::Enc.for('A128GCM')).to eq JWE::Enc::A128gcm
10+
it 'returns an instance for the specified enc' do
11+
expect(JWE::Enc.for('A128GCM')).to be_a JWE::Enc::A128gcm
1212
end
1313

1414
it 'raises an error for a not-implemented enc' do

0 commit comments

Comments
 (0)