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

Remove base64 dependency and exclude newlines #20

Merged
merged 1 commit into from
Apr 4, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Follow [RFC 4648](https://www.ietf.org/rfc/rfc4648.txt) base 64 encoding, removing line-feeds from the encoded data.

## v1.0.1 (June 01, 2020)

- Fix missing documentation links
Expand Down
7 changes: 3 additions & 4 deletions lib/secret_keys/encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "securerandom"
require "openssl"
require "base64"

# Encyption helper for encrypting and decrypting values using AES-256-GCM and returning
# as Base64 encoded strings. The encrypted values also include a prefix that can be used
Expand Down Expand Up @@ -138,13 +137,13 @@ def inspect
# Receive a cipher object (initialized with key) and data
def encode_aes(params)
encoded = params.values.pack(ENCODING_FORMAT)
# encode base64 and get rid of trailing newline and unnecessary =
Base64.encode64(encoded).chomp.tr("=", "")
# encode base64 and get rid of unnecessary '=' padding
[encoded].pack("m0").tr("=", "")
end

# Passed in an aes encoded string and returns a cipher object
def decode_aes(str)
unpacked_data = Base64.decode64(str).unpack(ENCODING_FORMAT)
unpacked_data = str.unpack1("m").unpack(ENCODING_FORMAT)
# Splat the data array apart
# nonce, auth_tag, encrypted_data = unpacked_data
CipherParams.new(*unpacked_data)
Expand Down
8 changes: 8 additions & 0 deletions spec/secret_keys/encryptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
expect(encryptor.decrypt(encrypted)).to eq "stuff"
end

it "should encode data without linefeeds or padding" do
long_line = "Hello, world!" * 100
encrypted = encryptor.encrypt(long_line)
expect(encrypted).to_not include "="
expect(encrypted).to_not include "\n"
expect(encryptor.decrypt(encrypted)).to eq long_line
end

it "should never encrypt a value the same way twice" do
encrypted_1 = encryptor.encrypt("stuff")
encrypted_2 = encryptor.encrypt("stuff")
Expand Down