Skip to content
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

Use Sodium for secret encryption and decryption #128

Open
wants to merge 2 commits into
base: trunk
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
3 changes: 2 additions & 1 deletion .circleci/.brewfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pkg-config
libxml2
imagemagick@6
imagemagick@6
libsodium
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fastlane-plugin-wpmreleasetoolkit (0.9.5)
fastlane-plugin-wpmreleasetoolkit (0.9.7)
activesupport (~> 4)
chroma (= 0.2.0)
diffy (~> 3.3)
Expand All @@ -13,6 +13,7 @@ PATH
progress_bar (~> 1.3)
rake (~> 12.3)
rake-compiler (~> 1.0)
rbnacl (~> 7)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -113,6 +114,7 @@ GEM
xcodeproj (>= 1.13.0, < 2.0.0)
xcpretty (~> 0.3.0)
xcpretty-travis-formatter (>= 0.0.3)
ffi (1.13.1)
gh_inspector (1.1.3)
git (1.7.0)
rchardet (~> 1.8)
Expand Down Expand Up @@ -192,6 +194,8 @@ GEM
rake (12.3.3)
rake-compiler (1.1.0)
rake
rbnacl (7.1.1)
ffi
rchardet (1.8.0)
representable (3.0.4)
declarative (< 0.1.0)
Expand Down
1 change: 1 addition & 0 deletions fastlane-plugin-wpmreleasetoolkit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
spec.add_dependency('parallel', '~> 1.14')
spec.add_dependency('chroma', '0.2.0')
spec.add_dependency('activesupport', '~> 4')
spec.add_dependency('rbnacl', '~> 7')

spec.add_development_dependency('pry', '~> 0.12.2')
spec.add_development_dependency('bundler', '>= 1.17')
Expand Down
38 changes: 5 additions & 33 deletions lib/fastlane/plugin/wpmreleasetoolkit/helper/encryption_helper.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
require 'openssl'

module Fastlane
module Helper
class EncryptionHelper
module OperationType
ENCRYPT = 1
DECRYPT = 2
end

def self.cipher(op_type)
cipher = OpenSSL::Cipher::AES256.new :CBC

cipher.encrypt if op_type == OperationType::ENCRYPT
cipher.decrypt if op_type == OperationType::DECRYPT

cipher
end

def self.encrypt(plain_text, key)
# Ensure consistent encoding
plain_text.force_encoding(Encoding::UTF_8)

cipher = cipher(OperationType::ENCRYPT)
cipher.key = key

encrypted = cipher.update(plain_text)
encrypted << cipher.final

encrypted
box = RbNaCl::SimpleBox.from_secret_key(key)
box.encrypt(plain_text)
end

def self.decrypt(encrypted, key)
cipher = cipher(OperationType::DECRYPT)
cipher.key = key

decrypted = cipher.update(encrypted)
decrypted << cipher.final

# Ensure consistent encoding
decrypted.force_encoding(Encoding::UTF_8)

decrypted
box = RbNaCl::SimpleBox.from_secret_key(key)
box.decrypt(encrypted)
end

def self.generate_key
cipher(OperationType::ENCRYPT).random_key
RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
end
end
end
Expand Down
37 changes: 9 additions & 28 deletions spec/encryption_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
require 'spec_helper.rb'
require 'securerandom'

describe Fastlane::Helper::EncryptionHelper do
let(:cipher) { double('cipher') }

before(:each) do
allow(OpenSSL::Cipher::AES256).to receive(:new).with(:CBC).and_return(cipher)
it 'can encrypt and decrypt data' do
string = SecureRandom.hex
key = Fastlane::Helper::EncryptionHelper.generate_key
encrypted = Fastlane::Helper::EncryptionHelper.encrypt(string, key)
decrypted = Fastlane::Helper::EncryptionHelper.decrypt(encrypted, key)
expect(string).to eq decrypted
end

it 'encrypts the input' do
expect(cipher).to receive(:encrypt)
expect(cipher).to receive(:key=).with('key')

expect(cipher).to receive(:update).with('plain text').and_return('encrypted')
expect(cipher).to receive(:final).and_return('!')

expect(Fastlane::Helper::EncryptionHelper.encrypt('plain text', 'key')).to eq('encrypted!')
end

it 'decrypts the input' do
expect(cipher).to receive(:decrypt)
expect(cipher).to receive(:key=).with('key')

expect(cipher).to receive(:update).with('encrypted').and_return('plain text')
expect(cipher).to receive(:final).and_return('!')

expect(Fastlane::Helper::EncryptionHelper.decrypt('encrypted', 'key')).to eq('plain text!')
end

it 'generates a random key' do
expect(cipher).to receive(:encrypt)
expect(cipher).to receive(:random_key).and_return('random key')

expect(Fastlane::Helper::EncryptionHelper.generate_key).to eq('random key')
it 'generates a random key that is 32 bytes long' do
expect(Fastlane::Helper::EncryptionHelper.generate_key.length).to eq(32)
end
end