Skip to content

Commit 7bd4d49

Browse files
committed
Use Sodium for secret encryption and decryption
Fixes an openSSL warning: ``` openssl aes-256-cbc -md sha256 -d -in .circleci/.firebase.secrets.json.enc -out .circleci/.firebase.secrets.json -k “${FIREBASE_SECRETS_ENCRYPTION_KEY}” *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. ``` Also gets us out of manual crypto. This is a breaking change and should be carefully merged to avoid breaking projects.
1 parent 1105ba5 commit 7bd4d49

File tree

4 files changed

+22
-64
lines changed

4 files changed

+22
-64
lines changed

Gemfile.lock

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
fastlane-plugin-wpmreleasetoolkit (0.9.2)
4+
fastlane-plugin-wpmreleasetoolkit (0.9.5)
55
activesupport (~> 4)
66
chroma (= 0.2.0)
77
diffy (~> 3.3)
@@ -13,12 +13,13 @@ PATH
1313
progress_bar (~> 1.3)
1414
rake (~> 12.3)
1515
rake-compiler (~> 1.0)
16+
rbnacl (~> 7)
1617

1718
GEM
1819
remote: https://rubygems.org/
1920
specs:
2021
CFPropertyList (3.0.1)
21-
activesupport (4.2.11.1)
22+
activesupport (4.2.11.3)
2223
i18n (~> 0.7)
2324
minitest (~> 5.1)
2425
thread_safe (~> 0.3, >= 0.3.4)
@@ -96,6 +97,7 @@ GEM
9697
xcodeproj (>= 1.8.1, < 2.0.0)
9798
xcpretty (~> 0.3.0)
9899
xcpretty-travis-formatter (>= 0.0.3)
100+
ffi (1.13.0)
99101
gh_inspector (1.1.3)
100102
git (1.7.0)
101103
rchardet (~> 1.8)
@@ -142,7 +144,7 @@ GEM
142144
mime-types-data (3.2019.0904)
143145
mini_magick (4.9.5)
144146
mini_portile2 (2.4.0)
145-
minitest (5.14.0)
147+
minitest (5.14.1)
146148
multi_json (1.13.1)
147149
multi_xml (0.6.0)
148150
multipart-post (2.0.0)
@@ -172,6 +174,8 @@ GEM
172174
rake (12.3.3)
173175
rake-compiler (1.1.0)
174176
rake
177+
rbnacl (7.1.1)
178+
ffi
175179
rchardet (1.8.0)
176180
representable (3.0.4)
177181
declarative (< 0.1.0)

fastlane-plugin-wpmreleasetoolkit.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
4444
spec.add_dependency('parallel', '~> 1.14')
4545
spec.add_dependency('chroma', '0.2.0')
4646
spec.add_dependency('activesupport', '~> 4')
47+
spec.add_dependency('rbnacl', '~> 7')
4748

4849
spec.add_development_dependency('pry', '~> 0.12.2')
4950
spec.add_development_dependency('bundler', '>= 1.17')

lib/fastlane/plugin/wpmreleasetoolkit/helper/encryption_helper.rb

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,22 @@
1-
require 'openssl'
2-
31
module Fastlane
42
module Helper
53
class EncryptionHelper
6-
module OperationType
7-
ENCRYPT = 1
8-
DECRYPT = 2
9-
end
10-
11-
def self.cipher(op_type)
12-
cipher = OpenSSL::Cipher::AES256.new :CBC
13-
14-
cipher.encrypt if op_type == OperationType::ENCRYPT
15-
cipher.decrypt if op_type == OperationType::DECRYPT
16-
17-
cipher
18-
end
194

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

24-
cipher = cipher(OperationType::ENCRYPT)
25-
cipher.key = key
26-
27-
encrypted = cipher.update(plain_text)
28-
encrypted << cipher.final
29-
30-
encrypted
9+
box = RbNaCl::SimpleBox.from_secret_key(key)
10+
box.encrypt(plain_text)
3111
end
3212

3313
def self.decrypt(encrypted, key)
34-
cipher = cipher(OperationType::DECRYPT)
35-
cipher.key = key
36-
37-
decrypted = cipher.update(encrypted)
38-
decrypted << cipher.final
39-
40-
# Ensure consistent encoding
41-
decrypted.force_encoding(Encoding::UTF_8)
42-
43-
decrypted
14+
box = RbNaCl::SimpleBox.from_secret_key(key)
15+
box.decrypt(encrypted)
4416
end
4517

4618
def self.generate_key
47-
cipher(OperationType::ENCRYPT).random_key
19+
RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
4820
end
4921
end
5022
end

spec/encryption_helper_spec.rb

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
require 'spec_helper.rb'
2+
require 'securerandom'
23

34
describe Fastlane::Helper::EncryptionHelper do
4-
let(:cipher) { double('cipher') }
55

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

10-
it 'encrypts the input' do
11-
expect(cipher).to receive(:encrypt)
12-
expect(cipher).to receive(:key=).with('key')
13-
14-
expect(cipher).to receive(:update).with('plain text').and_return('encrypted')
15-
expect(cipher).to receive(:final).and_return('!')
16-
17-
expect(Fastlane::Helper::EncryptionHelper.encrypt('plain text', 'key')).to eq('encrypted!')
18-
end
19-
20-
it 'decrypts the input' do
21-
expect(cipher).to receive(:decrypt)
22-
expect(cipher).to receive(:key=).with('key')
23-
24-
expect(cipher).to receive(:update).with('encrypted').and_return('plain text')
25-
expect(cipher).to receive(:final).and_return('!')
26-
27-
expect(Fastlane::Helper::EncryptionHelper.decrypt('encrypted', 'key')).to eq('plain text!')
28-
end
29-
30-
it 'generates a random key' do
31-
expect(cipher).to receive(:encrypt)
32-
expect(cipher).to receive(:random_key).and_return('random key')
33-
34-
expect(Fastlane::Helper::EncryptionHelper.generate_key).to eq('random key')
14+
it 'generates a random key that is 32 bytes long' do
15+
expect(Fastlane::Helper::EncryptionHelper.generate_key.length).to eq(32)
3516
end
3617
end

0 commit comments

Comments
 (0)