跳至内容 跳至搜索

这是加密属性的哈希的包装器。它由 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