跳至内容 跳至搜索

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