class OpenPGP::S2K

OpenPGP string-to-key (S2K) specifiers.

@see tools.ietf.org/html/rfc4880#section-3.7

Constants

DEFAULT

@see tools.ietf.org/html/rfc4880#section-3.7.1.3

Attributes

algorithm[RW]

@return [Integer]

passphrase[RW]

@return [String]

Public Class Methods

identifier() click to toggle source

@return [Integer]

# File lib/openpgp/s2k.rb, line 28
def self.identifier
  const_get(:IDENTIFIER)
end
new(passphrase = nil, options = {}, &block) click to toggle source

@param [String, to_s] passphrase @param [Hash{Symbol => Object}] options

# File lib/openpgp/s2k.rb, line 35
def initialize(passphrase = nil, options = {}, &block)
  @passphrase = passphrase.to_s
  options.each { |k, v| instance_variable_set("@#{k}", v) }

  block.call(self) if block_given?
end
parse(input) click to toggle source

@param [Buffer] input @return [S2K]

# File lib/openpgp/s2k.rb, line 16
def self.parse(input)
  case mode = input.read_byte
    when 0        then S2K::Simple.parse(input)       # Simple S2K
    when 1        then S2K::Salted.parse(input)       # Salted S2K
    when 3        then S2K::Iterated.parse(input)     # Iterated and Salted S2K
    when 100..110 then S2K.new(:data => input.read)   # Private/Experimental S2K
    else # TODO
  end
end

Public Instance Methods

digest() click to toggle source

@return [Class]

# File lib/openpgp/s2k.rb, line 85
def digest
  @digest ||= case algorithm
    when nil    then Digest::DEFAULT
    when Digest then algorithm
    when Symbol then Digest.for(algorithm)
    when String then Digest.for(algorithm)
    else Digest.for(algorithm.to_i)
  end
end
digest_input() click to toggle source

@return [String] @raise [NotImplementedError] unless implemented in subclass @abstract

# File lib/openpgp/s2k.rb, line 106
def digest_input
  raise NotImplementedError
end
digest_input_with_preload(length = 0) click to toggle source

@param [Integer] length @return [String]

# File lib/openpgp/s2k.rb, line 98
def digest_input_with_preload(length = 0)
  ("\0" * length) << digest_input
end
identifier() click to toggle source

@return [Integer]

# File lib/openpgp/s2k.rb, line 52
def identifier
  @identifier || self.class.identifier
end
to_hash() click to toggle source

@return [Hash]

# File lib/openpgp/s2k.rb, line 58
def to_hash
  {:mode => identifier, :algorithm => digest.to_i}
end
to_key(key_size = 16) click to toggle source

@return [Object]

# File lib/openpgp/s2k.rb, line 70
def to_key(key_size = 16)
  key = if digest.size >= key_size
    digest.digest(digest_input)
  else
    Buffer.write do |buffer|
      (key_size / digest.size.to_f).ceil.times do |i|
        buffer << digest.digest(digest_input_with_preload(i))
      end
    end
  end
  key[0, key_size]
end
to_s() click to toggle source

@return [String]

# File lib/openpgp/s2k.rb, line 64
def to_s
  Buffer.write { |buffer| write(buffer) }
end
write(buffer) click to toggle source

@param [Buffer] buffer @return [void]

# File lib/openpgp/s2k.rb, line 45
def write(buffer)
  buffer.write_byte(identifier)
  buffer.write_byte(digest.to_i)
end