Skip to content

Add RsaOaep256 algorithm #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Only a subset of these algorithms is implemented in this gem. Striked elements a
Key management:
* RSA1_5
* RSA-OAEP (default)
* ~~RSA-OAEP-256~~
* RSA-OAEP-256
* A128KW
* A192KW
* A256KW
Expand Down
1 change: 1 addition & 0 deletions lib/jwe/alg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'jwe/alg/a256_kw'
require 'jwe/alg/dir'
require 'jwe/alg/rsa_oaep'
require 'jwe/alg/rsa_oaep_256'
require 'jwe/alg/rsa15'

module JWE
Expand Down
22 changes: 22 additions & 0 deletions lib/jwe/alg/rsa_oaep_256.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module JWE
module Alg
# RSA-OAEP-256 key encryption algorithm.
class RsaOaep256
attr_accessor :key

def initialize(key)
self.key = key
end

def encrypt(cek)
key.encrypt(cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' })
end

def decrypt(encrypted_cek)
key.decrypt(encrypted_cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' })
end
end
end
end
15 changes: 15 additions & 0 deletions spec/jwe/alg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
end
end

describe JWE::Alg::RsaOaep256 do
let(:alg) { JWE::Alg::RsaOaep256.new(key) }

describe '#encrypt' do
it 'returns an encrypted string' do
expect(alg.encrypt('random key')).to_not eq 'random key'
end
end

it 'decrypts the encrypted key to the original key' do
ciphertext = alg.encrypt('random key')
expect(alg.decrypt(ciphertext)).to eq 'random key'
end
end

describe JWE::Alg::Rsa15 do
let(:alg) { JWE::Alg::Rsa15.new(key) }

Expand Down
Loading