Rails实现字段加密存储(rail是什么意思)干货满满

随心笔谈11个月前发布 admin
89 0


class EncryptedStringSerializer
def self.load(value)
value.to_s.blank? ? ” : decrypt(value)
end

def self.dump(value)
encrypt(value || ”)
end

private

def self.encrypt(value)
encryptor.encrypt_and_sign(value)
end

def self.decrypt(value)
encryptor.decrypt_and_verify(value)
end

def self.encryptor
@encryptor ||=ActiveSupport::MessageEncryptor.new(Settings.message_encryptor_key)
end
end

class UserAddress < ActiveRecord::Base
serialize :phone, EncryptedStringSerializer
serialize :first_name, EncryptedStringSerializer
serialize :last_name, EncryptedStringSerializer
serialize :country, EncryptedStringSerializer
serialize :state, EncryptedStringSerializer
serialize :city, EncryptedStringSerializer
serialize :address1, EncryptedStringSerializer
serialize :address2, EncryptedStringSerializer
serialize :zipcode, EncryptedStringSerializer
end

© 版权声明

相关文章