Class: Passwordy::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/passwordy/generator.rb

Class Method Summary collapse

Class Method Details

.generate_password(resource, master_password) ⇒ Object

Public: Generate a password for a given resource.

resource - The String name of the thing we want a

password for.

master_password - The String master password that is used to

generate any subsequent passwords.

Examples

generate_password('google.com', 'keyboard cat')
# => 'e94e5ce8c8ca9affb507ae9e152d4b44'

Returns a password that can be used for the given resource.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/passwordy/generator.rb', line 32

def self.generate_password(resource, master_password)
  salt_path = File.expand_path('~/.salt')
  write_salt(salt_path)

  salt = File.open(salt_path, 'r').read

  first_digest = Digest::SHA512.hexdigest([salt, resource].join('\n'))
  second_digest = Digest::SHA512.hexdigest(master_password)
  digested_parts = [first_digest, second_digest].join('\n')

  Digest::SHA512.hexdigest(digested_parts)[1..32]
end

.write_salt(path) ⇒ Object

Public: Write a salt file to a particular location.

path - The String path to save the salt.

Does not overwrite an existing file at the given location.



11
12
13
14
15
16
17
# File 'lib/passwordy/generator.rb', line 11

def self.write_salt(path)
  return if File.exists?(path)
  File.open(path, 'w') do |f|
    f.write(generate_salt)
    f.chmod(0400)
  end
end