Class: OpenPGP::S2K

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

Overview

OpenPGP string-to-key (S2K) specifiers.

See Also:

Direct Known Subclasses

S2K::Salted, S2K::Simple

Defined Under Namespace

Classes: Iterated, Salted, Simple

Constant Summary

DEFAULT =
Iterated

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

A new instance of S2K

Parameters:

  • (String, #to_s) passphrase (defaults to: nil)
  • (Hash{Symbol => Object}) options (defaults to: {})


35
36
37
38
39
40
# 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

Instance Attribute Details

- (Integer) algorithm

Returns:

  • (Integer)


11
12
13
# File 'lib/openpgp/s2k.rb', line 11

def algorithm
  @algorithm
end

- (String) passphrase

Returns:

  • (String)


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

def passphrase
  @passphrase
end

Class Method Details

+ (Integer) identifier

Returns:

  • (Integer)


28
29
30
# File 'lib/openpgp/s2k.rb', line 28

def self.identifier
  const_get(:IDENTIFIER)
end

+ (S2K) parse(input)

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

- (Class) digest

Returns:

  • (Class)


85
86
87
88
89
90
91
92
93
# 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

- (String) digest_input

This method is abstract.

Returns:

  • (String)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



106
107
108
# File 'lib/openpgp/s2k.rb', line 106

def digest_input
  raise NotImplementedError
end

- (String) digest_input_with_preload(length = 0)

Parameters:

  • (Integer) length (defaults to: 0)

Returns:

  • (String)


98
99
100
# File 'lib/openpgp/s2k.rb', line 98

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

- (Integer) identifier

Returns:

  • (Integer)


52
53
54
# File 'lib/openpgp/s2k.rb', line 52

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

- (Hash) to_hash

Returns:

  • (Hash)


58
59
60
# File 'lib/openpgp/s2k.rb', line 58

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

- (Object) to_key(key_size = 16)

Returns:

  • (Object)


70
71
72
73
74
75
76
77
78
79
80
81
# 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

- (String) to_s

Returns:

  • (String)


64
65
66
# File 'lib/openpgp/s2k.rb', line 64

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

- write(buffer)

This method returns an undefined value.

Parameters:



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

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