Class: OpenPGP::S2K

Inherits:
Object
  • Object
show all
Defined in:
lib/openpgp/s2k.rb

Overview

OpenPGP string-to-key (S2K) specifiers.

Direct Known Subclasses

Salted, Simple

Defined Under Namespace

Classes: Iterated, Salted, Simple

Constant Summary collapse

DEFAULT =
Iterated

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(passphrase = nil, options = {}, &block) ⇒ S2K

Returns a new instance of S2K.



24
25
26
27
28
29
# File 'lib/openpgp/s2k.rb', line 24

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

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



8
9
10
# File 'lib/openpgp/s2k.rb', line 8

def algorithm
  @algorithm
end

#passphraseObject

Returns the value of attribute passphrase.



7
8
9
# File 'lib/openpgp/s2k.rb', line 7

def passphrase
  @passphrase
end

Class Method Details

.identifierObject



20
21
22
# File 'lib/openpgp/s2k.rb', line 20

def self.identifier
  const_get(:IDENTIFIER)
end

.parse(input) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/openpgp/s2k.rb', line 10

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

Instance Method Details

#digestObject



61
62
63
64
65
66
67
68
69
# File 'lib/openpgp/s2k.rb', line 61

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_inputObject

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/openpgp/s2k.rb', line 75

def digest_input
  raise NotImplementedError
end

#digest_input_with_preload(length = 0) ⇒ Object



71
72
73
# File 'lib/openpgp/s2k.rb', line 71

def digest_input_with_preload(length = 0)
  ("\0" * length) << digest_input
end

#identifierObject



36
37
38
# File 'lib/openpgp/s2k.rb', line 36

def identifier
  @identifier || self.class.identifier
end

#to_hashObject



40
41
42
# File 'lib/openpgp/s2k.rb', line 40

def to_hash
  {:mode => identifier, :algorithm => digest.to_i}
end

#to_key(key_size = 16) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openpgp/s2k.rb', line 48

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_sObject



44
45
46
# File 'lib/openpgp/s2k.rb', line 44

def to_s
  Buffer.write { |buffer| write(buffer) }
end

#write(buffer) ⇒ Object



31
32
33
34
# File 'lib/openpgp/s2k.rb', line 31

def write(buffer)
  buffer.write_byte(identifier)
  buffer.write_byte(digest.to_i)
end