Sha256: 8b4f3e05e894f7a7a05339c51d10ec7c920b2014076471581be13b038efef0f2

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

module Keyring
  class Key
    attr_reader :id, :signing_key, :encryption_key

    def initialize(id, key, key_size)
      @id = Integer(id)
      @key_size = key_size
      @encryption_key, @signing_key = parse_key(key)
    end

    def to_s
      "#<Keyring::Key id=#{id.inspect}>"
    end
    alias_method :inspect, :to_s

    private def parse_key(key)
      expected_key_size = @key_size * 2
      secret = decode_key(key, expected_key_size)

      raise InvalidSecret, "Secret must be #{expected_key_size} bytes, instead got #{secret.bytesize}" unless secret.bytesize == expected_key_size

      signing_key = secret[0...@key_size]
      encryption_key = secret[@key_size..-1]

      [encryption_key, signing_key]
    end

    private def decode_key(key, key_size)
      if key.bytesize == key_size
        key
      else
        begin
          Base64.strict_decode64(key)
        rescue ArgumentError
          Base64.decode64(key)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attr_keyring-0.5.2 lib/keyring/key.rb
attr_keyring-0.5.1 lib/keyring/key.rb