Sha256: 4cb80682aed3007352e41374f1840cac1b92a9bbc71ab54ef11c9203ea309878

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'authmac/hmac_checker'

module Authmac
  describe HmacChecker do
    let(:checker) { HmacChecker.new("very secret key", "|", "sha1") }

    describe '#validate' do
      context 'for an empty hash' do
        let(:hash) { Hash.new }

        it 'succeeds with the correct hmac' do
          checker.validate(hash, hmacify('')).should be_true
        end

        it 'fails with an incorrect hmac' do
          checker.validate(hash, "wrong").should be_false
        end
      end

      context 'for a hash with a single parameter' do
        it 'succeeds with the correct hmac' do
          checker.validate({single: 'parameter'}, hmacify("parameter")).should be_true
        end

        it 'fails with incorrect hmac' do
          checker.validate({single: 'parameter'}, 'wrong').should be_false
        end
      end

      context 'for a hash with multiple parameters' do
        it 'succeeds with correct hmac' do
          checker.validate({first: 'parameter', second: 'another'},
                          hmacify('parameter|another')).should be_true
        end

        it 'sorts hash values based on their keys' do
          checker.validate({second: 'another', first: 'parameter'},
                          hmacify('parameter|another')).should be_true

        end
      end
    end

    describe '#calculate_hmac' do
      it 'generates hmac' do
        checker.sign(second: 'another', first: 'parameter').should == hmacify('parameter|another')
      end
    end

    def hmacify(string, method='sha1')
      digester = OpenSSL::Digest::Digest.new(method)
      OpenSSL::HMAC.hexdigest(digester, "very secret key", string)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authmac-1.0.0 spec/authmac/hmac_checker_spec.rb