Sha256: 556dbfbc41b3cbc0521cff435f8ac7c2d80a61d20264621e8a4acfb0d8b9005a

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

##
# SHA-256 Digester 基于 SHA-256 算法的摘要逻辑关注点。

module Unidom::Common::Concerns::Sha256Digester

  extend ActiveSupport::Concern

  included do |includer|

    ##
    # 对明文 message 进行 SHA-256 摘要, pepper 是用于增加混乱的内容。如:
    # class SomeModel
    #   include Unidom::Common::Concerns::Sha256Digester
    #   def some_method(param_1)
    #     digest param_1
    #     # 或者
    #     digest param_1, pepper: 'my_pepper'
    #   end
    # end
    def digest(message, pepper: nil)
      self.class.digest message, pepper: pepper
    end

    ##
    # 对明文 message 进行 SHA-256 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
    # class SomeModel
    #   include Unidom::Common::Concerns::Sha256Digester
    #   def some_method(param_1)
    #     hex_digest param_1
    #     # 或者
    #     hex_digest param_1, pepper: 'my_pepper'
    #   end
    # end
    def hex_digest(message, pepper: nil)
      self.class.hex_digest message, pepper: pepper
    end

  end

  module ClassMethods

    def digest(message, pepper: nil)
      message.present? ? Digest::SHA256.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
    end

    def hex_digest(message, pepper: nil)
      message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unidom-common-1.10 app/models/unidom/common/concerns/sha256_digester.rb