2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module CryptoHelper
|
|
|
|
extend self
|
|
|
|
|
|
|
|
AES256_GCM_OPTIONS = {
|
|
|
|
algorithm: 'aes-256-gcm',
|
2021-03-11 19:13:27 +05:30
|
|
|
key: Settings.attr_encrypted_db_key_base_32
|
2018-11-18 11:00:15 +05:30
|
|
|
}.freeze
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
AES256_GCM_IV_STATIC = Settings.attr_encrypted_db_key_base_12
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def sha256(value)
|
|
|
|
salt = Settings.attr_encrypted_db_key_base_truncated
|
|
|
|
::Digest::SHA256.base64digest("#{value}#{salt}")
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def aes256_gcm_encrypt(value, nonce: AES256_GCM_IV_STATIC)
|
|
|
|
encrypted_token = Encryptor.encrypt(AES256_GCM_OPTIONS.merge(value: value, iv: nonce))
|
|
|
|
Base64.strict_encode64(encrypted_token)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def aes256_gcm_decrypt(value, nonce: AES256_GCM_IV_STATIC)
|
2018-11-18 11:00:15 +05:30
|
|
|
return unless value
|
|
|
|
|
|
|
|
encrypted_token = Base64.decode64(value)
|
2021-04-29 21:17:54 +05:30
|
|
|
Encryptor.decrypt(AES256_GCM_OPTIONS.merge(value: encrypted_token, iv: nonce))
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|