跳到內容 跳到搜尋

這是一個加密屬性的雜湊包裝器。它是由 Key (公開標籤)和 Message (標頭)使用的。

由於屬性會在訊息中序列化,為了儲存效率,盡量縮短它们的鍵長非常重要。它會定義常見屬性的存取器,它們會盡量縮短這些鍵長,同時公開可讀的名稱。

message.headers.encrypted_data_key # instead of message.headers[:k]

參閱 Properties::DEFAULT_PROPERTIESKeyMessage

方法
#
A
N
T
V

常量

ALLOWED_VALUE_CLASSES = [String, ActiveRecord::Encryption::Message, Numeric, Integer, Float, BigDecimal, TrueClass, FalseClass, Symbol, NilClass]
 
DEFAULT_PROPERTIES = {encrypted_data_key: "k", encrypted_data_key_id: "i", compressed: "c", iv: "iv", auth_tag: "at", encoding: "e"}
 

對每個輸入,它產生一個存取器,公開完整名稱

類別公開方法

new(initial_properties = {})

# File activerecord/lib/active_record/encryption/properties.rb, line 42
def initialize(initial_properties = {})
  @data = {}
  add(initial_properties)
end

實體公開方法

[]=(key, value)

設定給定鍵的值

如果值存在,它會引發 EncryptedContentIntegrity

# File activerecord/lib/active_record/encryption/properties.rb, line 50
def []=(key, value)
  raise Errors::EncryptedContentIntegrity, "Properties can't be overridden: #{key}" if key?(key)
  validate_value_type(value)
  data[key] = value
end

add(other_properties)

# File activerecord/lib/active_record/encryption/properties.rb, line 62
def add(other_properties)
  other_properties.each do |key, value|
    self[key.to_sym] = value
  end
end

to_h()

# File activerecord/lib/active_record/encryption/properties.rb, line 68
def to_h
  data
end

validate_value_type(value)

# File activerecord/lib/active_record/encryption/properties.rb, line 56
def validate_value_type(value)
  unless ALLOWED_VALUE_CLASSES.include?(value.class) || ALLOWED_VALUE_CLASSES.any? { |klass| value.is_a?(klass) }
    raise ActiveRecord::Encryption::Errors::ForbiddenClass, "Can't store a #{value.class}, only properties of type #{ALLOWED_VALUE_CLASSES.inspect} are allowed"
  end
end