debian-mirror-gitlab/spec/lib/gitlab/crypto_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2019-02-15 15:39:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::CryptoHelper do
2019-02-15 15:39:39 +05:30
describe '.sha256' do
it 'generates SHA256 digest Base46 encoded' do
digest = described_class.sha256('some-value')
expect(digest).to match %r{\A[A-Za-z0-9+/=]+\z}
expect(digest).to eq digest.strip
end
end
describe '.aes256_gcm_encrypt' do
it 'is Base64 encoded string without new line character' do
encrypted = described_class.aes256_gcm_encrypt('some-value')
expect(encrypted).to match %r{\A[A-Za-z0-9+/=]+\z}
expect(encrypted).not_to include "\n"
end
2021-03-11 19:13:27 +05:30
it 'encrypts using static iv' do
expect(Encryptor).to receive(:encrypt).with(described_class::AES256_GCM_OPTIONS.merge(value: 'some-value', iv: described_class::AES256_GCM_IV_STATIC)).and_return('hashed_value')
described_class.aes256_gcm_encrypt('some-value')
end
2019-02-15 15:39:39 +05:30
2021-04-29 21:17:54 +05:30
context 'with provided iv' do
let(:iv) { create_nonce }
it 'encrypts using provided iv' do
expect(Encryptor).to receive(:encrypt).with(described_class::AES256_GCM_OPTIONS.merge(value: 'some-value', iv: iv)).and_return('hashed_value')
described_class.aes256_gcm_encrypt('some-value', nonce: iv)
end
2021-03-11 19:13:27 +05:30
end
2021-04-29 21:17:54 +05:30
end
2021-03-11 19:13:27 +05:30
2021-04-29 21:17:54 +05:30
describe '.aes256_gcm_decrypt' do
2021-03-11 19:13:27 +05:30
context 'when token was encrypted using static nonce' do
let(:encrypted) { described_class.aes256_gcm_encrypt('some-value', nonce: described_class::AES256_GCM_IV_STATIC) }
it 'correctly decrypts encrypted string' do
decrypted = described_class.aes256_gcm_decrypt(encrypted)
expect(decrypted).to eq 'some-value'
end
it 'decrypts a value when it ends with a new line character' do
decrypted = described_class.aes256_gcm_decrypt(encrypted + "\n")
2019-02-15 15:39:39 +05:30
2021-03-11 19:13:27 +05:30
expect(decrypted).to eq 'some-value'
end
2019-02-15 15:39:39 +05:30
end
2021-03-11 19:13:27 +05:30
context 'when token was encrypted using random nonce' do
let(:value) { 'random-value' }
2021-04-29 21:17:54 +05:30
let(:iv) { create_nonce }
let(:encrypted) { described_class.aes256_gcm_encrypt(value, nonce: iv) }
2019-02-15 15:39:39 +05:30
2021-03-11 19:13:27 +05:30
it 'correctly decrypts encrypted string' do
2021-04-29 21:17:54 +05:30
decrypted = described_class.aes256_gcm_decrypt(encrypted, nonce: iv)
2021-03-11 19:13:27 +05:30
expect(decrypted).to eq value
end
2019-02-15 15:39:39 +05:30
end
end
2021-03-11 19:13:27 +05:30
def create_nonce
2021-04-29 21:17:54 +05:30
::Digest::SHA256.hexdigest('my-value').bytes.take(TokenAuthenticatableStrategies::EncryptionHelper::NONCE_SIZE).pack('c*')
2021-03-11 19:13:27 +05:30
end
2019-02-15 15:39:39 +05:30
end