Sha256: 3cbdd5fd2e766b2bd9907f49c128222114de57c190f87b5518fc10454390b62e

Contents?: true

Size: 1.17 KB

Versions: 31

Compression:

Stored size: 1.17 KB

Contents

require 'digest/sha1'

module ASIR
  class Coder
    # !SLIDE
    # Sign Coder
    #
    # Sign payload during encode, check signature during decode.
    #
    # Signature is the digest of secret + payload.
    #
    # Encode payload as Hash containing the digest function name, signature and payload.
    # Decode and validate Hash containing the digest function name, signature and payload.
    #
    class Sign < self
      attr_accessor :secret, :function

      def _encode obj
        payload = obj.to_s
        { :function  => function,
          :signature => ::Digest.const_get(function).
                          new.hexdigest(secret + payload),
          :payload   => payload }
      end

      def _decode obj
        raise SignatureError, "expected Hash, given #{obj.class}" unless Hash === obj
        payload = obj[:payload]
        raise SignatureError, "signature invalid" unless obj == _encode(payload)
        payload
      end

      # !SLIDE
      # Sign Coder Support

      # Signature Error.
      class SignatureError < Error; end

      def initialize_before_opts
        @function = :SHA1
      end
      # !SLIDE END
    end 
    # !SLIDE END
  end # class
end # class


Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
asir-1.2.11 lib/asir/coder/sign.rb
asir-1.2.10 lib/asir/coder/sign.rb
asir-1.2.9 lib/asir/coder/sign.rb
asir-1.2.8 lib/asir/coder/sign.rb
asir-1.2.7 lib/asir/coder/sign.rb
asir-1.2.6 lib/asir/coder/sign.rb
asir-1.2.5 lib/asir/coder/sign.rb
asir-1.2.3 lib/asir/coder/sign.rb
asir-1.2.2 lib/asir/coder/sign.rb
asir-1.2.1 lib/asir/coder/sign.rb
asir-1.2.0 lib/asir/coder/sign.rb
asir-1.1.12 lib/asir/coder/sign.rb
asir-1.1.11 lib/asir/coder/sign.rb
asir-1.1.10 lib/asir/coder/sign.rb
asir-1.1.9 lib/asir/coder/sign.rb
asir-1.1.8 lib/asir/coder/sign.rb
asir-1.1.7 lib/asir/coder/sign.rb
asir-1.1.6 lib/asir/coder/sign.rb
asir-1.1.5 lib/asir/coder/sign.rb
asir-1.1.4 lib/asir/coder/sign.rb