Class: Bitca::Pwdgen

Inherits:
Object
  • Object
show all
Defined in:
lib/bitca/pwdgen.rb

Instance Method Summary collapse

Constructor Details

#initialize(rootpath = '.', pwdsize = 40) ⇒ Pwdgen

Returns a new instance of Pwdgen.



3
4
5
6
7
# File 'lib/bitca/pwdgen.rb', line 3

def initialize(rootpath = '.', pwdsize = 40)
  @pwdsize = pwdsize
  @rootpath = rootpath
  @pwdpath = "#{rootpath}/pwd"
end

Instance Method Details

#genpwd(cn) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/bitca/pwdgen.rb', line 28

def genpwd(cn)
  chars = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a) + %w(i I o O 1 l 0)
  pwd = (1..@pwdsize).collect{|a| chars[rand(chars.size)] }.join

  f = File.new("#{@pwdpath}/#{cn}.pwd", "w")
  f.puts pwd
  f.close

  pwd
end

#getpwd(cn) ⇒ Object



39
40
41
42
# File 'lib/bitca/pwdgen.rb', line 39

def getpwd(cn)
  return false unless pwd_exists?(cn)
  File.read("#{@pwdpath}/#{cn}.pwd").gsub /\n/, ''
end

#initObject



9
10
11
# File 'lib/bitca/pwdgen.rb', line 9

def init
  Dir.mkdir(@pwdpath, 0700) unless File.directory?(@pwdpath)
end

#listObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/bitca/pwdgen.rb', line 13

def list
  items = []

  Dir.foreach(@pwdpath) do |f|
    next unless f =~ /\.pwd$/
    items << f.gsub(/\.pwd$/, '')
  end

  items
end

#pwd_exists?(cn) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/bitca/pwdgen.rb', line 24

def pwd_exists?(cn)
  File.exist?("#{@pwdpath}/#{cn}.pwd")
end

#remove(cn) ⇒ Object



44
45
46
47
# File 'lib/bitca/pwdgen.rb', line 44

def remove(cn)
  return false unless pwd_exists?(cn)
  File.delete("#{@pwdpath}/#{cn}.pwd")
end