Commit c4f81e4

mo <mo.khan@gmail.com>
2017-12-30 18:56:16
add spec for oaep.
1 parent b994b14
Changed files (2)
lib
xml
kit
spec
lib/xml/kit/crypto/oaep_cipher.rb
@@ -13,8 +13,18 @@ module Xml
           ALGORITHMS[algorithm]
         end
 
+        def encrypt(plain_text)
+          @key.public_encrypt(plain_text, padding)
+        end
+
         def decrypt(cipher_text)
-          @key.private_decrypt(cipher_text, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
+          @key.private_decrypt(cipher_text, padding)
+        end
+
+        private
+
+        def padding
+          OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
         end
       end
     end
spec/xml/crypto/oaep_cipher_spec.rb
@@ -0,0 +1,23 @@
+RSpec.describe ::Xml::Kit::Crypto::OaepCipher do
+  let(:key_pair) { ::Xml::Kit::KeyPair.generate(use: :encryption) }
+  let(:private_key) { key_pair.private_key }
+
+  describe "#encrypt" do
+    let(:uuid) { SecureRandom.uuid }
+    subject { described_class.new("", private_key) }
+
+    it 'encrypts the plain text' do
+      expect(subject.decrypt(subject.encrypt(uuid))).to eql(uuid)
+    end
+  end
+
+  describe "#decrypt" do
+    let(:uuid) { SecureRandom.uuid }
+    subject { described_class.new("", private_key) }
+
+    it 'decrypts the cipher text' do
+      cipher_text = private_key.public_encrypt(uuid, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
+      expect(subject.decrypt(cipher_text)).to eql(uuid)
+    end
+  end
+end