用于生成和派生随机密钥的工具。
方法
Attributes
| [R] | hash_digest_class |
类公共方法
new(hash_digest_class: ActiveRecord::Encryption.config.hash_digest_class) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/key_generator.rb, line 11 def initialize(hash_digest_class: ActiveRecord::Encryption.config.hash_digest_class) @hash_digest_class = hash_digest_class end
实例公共方法
derive_key_from(password, length: key_length) 链接
从给定的密码派生密钥。密钥的字节大小为 :length (默认为配置的 Cipher 的长度)
生成的密钥将使用 ActiveRecord::Encryption.key_derivation_salt 的值进行加盐处理
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/key_generator.rb, line 38 def derive_key_from(password, length: key_length) ActiveSupport::KeyGenerator.new(password, hash_digest_class: hash_digest_class) .generate_key(key_derivation_salt, length) end
generate_random_hex_key(length: key_length) 链接
返回十六进制格式的随机密钥。密钥的字节大小为 :length (默认为配置的 Cipher 的长度)
十六进制格式便于将密钥表示为可打印文本。为了最大限度地利用字符空间,建议包含不可打印字符。十六进制格式确保生成的密钥可以用纯文本表示
要将其转换回具有所需长度的原始字符串
[ value ].pack("H*")
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/key_generator.rb, line 30 def generate_random_hex_key(length: key_length) generate_random_key(length: length).unpack("H*")[0] end
generate_random_key(length: key_length) 链接
返回一个随机密钥。密钥的字节大小为 :length (默认为配置的 Cipher 的长度)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/encryption/key_generator.rb, line 16 def generate_random_key(length: key_length) SecureRandom.random_bytes(length) end