產生和派生亂數金鑰的程式。程式
方法
屬性
[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