class Security::Password

Attributes

attributes[R]
keychain[R]
password[R]

Public Class Methods

new(keychain, attributes, password) click to toggle source
# File lib/security/password.rb, line 12
def initialize(keychain, attributes, password)
  @keychain = Keychain.new(keychain)
  @attributes = attributes
  @password = password
end

Private Class Methods

decode_hex_blob(string) click to toggle source
# File lib/security/password.rb, line 57
def decode_hex_blob(string)
  [string].pack('H*').force_encoding('UTF-8')
end
flags_for_options(options = {}) click to toggle source
# File lib/security/password.rb, line 45
def flags_for_options(options = {})
  flags = options.dup
  flags[:a] ||= flags.delete(:account)
  flags[:c] ||= flags.delete(:creator)
  flags[:C] ||= flags.delete(:type)
  flags[:D] ||= flags.delete(:kind)
  flags[:G] ||= flags.delete(:value)
  flags[:j] ||= flags.delete(:comment)

  flags.delete_if { |_k, v| v.nil? }.collect { |k, v| "-#{k} #{v.shellescape}".strip }.join(' ')
end
password_from_output(output) click to toggle source
# File lib/security/password.rb, line 21
def password_from_output(output)
  return nil if output.match?(/^security: /)

  keychain = nil
  attributes = {}
  password = nil
  output.split(/\n/).each do |line|
    case line
    when /^keychain: "(.+)"/
      keychain = Regexp.last_match(1)
    when /"(\w{4})".+="(.+)"/
      attributes[Regexp.last_match(1)] = Regexp.last_match(2)
    when /"(\w{4})"<blob>=0x([[:xdigit:]]+)/
      attributes[Regexp.last_match(1)] = decode_hex_blob(Regexp.last_match(2))
    when /^password: "(.+)"/
      password = Regexp.last_match(1)
    when /^password: 0x([[:xdigit:]]+)/
      password = decode_hex_blob(Regexp.last_match(1))
    end
  end

  new(keychain, attributes, password)
end