Skip to content

Commit b9083b0

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 f04b19f commit b9083b0

File tree

4 files changed

+20
-62
lines changed

4 files changed

+20
-62
lines changed

Gemfile.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
fastlane-plugin-wpmreleasetoolkit (0.9.5)
4+
fastlane-plugin-wpmreleasetoolkit (0.9.7)
55
activesupport (~> 4)
66
chroma (= 0.2.0)
77
diffy (~> 3.3)
@@ -13,6 +13,7 @@ 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/
@@ -113,6 +114,7 @@ GEM
113114
xcodeproj (>= 1.13.0, < 2.0.0)
114115
xcpretty (~> 0.3.0)
115116
xcpretty-travis-formatter (>= 0.0.3)
117+
ffi (1.13.1)
116118
gh_inspector (1.1.3)
117119
git (1.7.0)
118120
rchardet (~> 1.8)
@@ -192,6 +194,8 @@ GEM
192194
rake (12.3.3)
193195
rake-compiler (1.1.0)
194196
rake
197+
rbnacl (7.1.1)
198+
ffi
195199
rchardet (1.8.0)
196200
representable (3.0.4)
197201
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)