Sha256: ddc5ebafc03716cd7f68387b6923d12205d9789570d8c4996eb76ad0b8a9b54e

Contents?: true

Size: 864 Bytes

Versions: 9

Compression:

Stored size: 864 Bytes

Contents

# frozen_string_literal: true

module LedgerSync
  module Util
    class Signer
      HMAC_SHA1_DIGEST   = OpenSSL::Digest.new('sha1')
      HMAC_SHA256_DIGEST = OpenSSL::Digest.new('sha256')

      attr_reader :str

      def initialize(str:)
        @str = str
      end

      def hmac_sha1(escape: false, key:)
        ret = Base64.encode64(OpenSSL::HMAC.digest(HMAC_SHA1_DIGEST, key, str)).strip
        ret = self.class.escape(str: ret) if escape
        ret
      end

      def hmac_sha256(escape: false, key:)
        ret = Base64.encode64(OpenSSL::HMAC.digest(HMAC_SHA256_DIGEST, key, str)).strip
        ret = self.class.escape(str: ret) if escape
        ret
      end

      def self.escape(str:)
        CGI.escape(str).gsub(/\+/, '%20')
      end

      def self.unescape(str:)
        CGI.unescape(str.gsub(/%20/, '+'))
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ledger_sync-1.4.4 lib/ledger_sync/util/signer.rb
ledger_sync-1.4.2 lib/ledger_sync/util/signer.rb
ledger_sync-1.4.1 lib/ledger_sync/util/signer.rb
ledger_sync-1.4.0 lib/ledger_sync/util/signer.rb
ledger_sync-1.3.5 lib/ledger_sync/util/signer.rb
ledger_sync-1.3.4 lib/ledger_sync/util/signer.rb
ledger_sync-1.3.3 lib/ledger_sync/util/signer.rb
ledger_sync-1.3.2 lib/ledger_sync/util/signer.rb
ledger_sync-1.3.1 lib/ledger_sync/util/signer.rb