Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implementing setting password length through ENV variable. Adding ali…

…as -l for length.
  • Loading branch information...
commit 416db8aea099de0a2460831f3df9c53d14802375 1 parent 802235f
@polarblau authored
Showing with 11 additions and 4 deletions.
  1. +1 −1  lib/passwordsafe/cli.rb
  2. +10 −3 lib/passwordsafe/keyring.rb
View
2  lib/passwordsafe/cli.rb
@@ -20,7 +20,7 @@ def add name, password
end
desc "generate NAME", "Generate a new PASSWORD and add it to the keyring with NAME"
- method_options :length => 8
+ method_options %w(length -l) => :numeric
def generate name
begin
password = get_keyring.generate(name, options[:length])
View
13 lib/passwordsafe/keyring.rb
@@ -24,15 +24,22 @@ def add name, password
@ring.store(name, password)
@safe.write_safe @ring
end
-
+
def change name, password
raise KeyMissingException, "#{name} does not exist in this safe.", caller unless @ring.has_key?(name)
@ring[name] = password
@safe.write_safe @ring
end
- def generate name, length = 8
+ def generate name, length = nil
raise KeyExistsException, "Key already exists in keyring, if you'd like to add it remove the existing key", caller if @ring.has_key?(name)
+ length = if length
+ length
+ elsif ENV.has_key?('PW_LENGTH')
+ ENV['PW_LENGTH'].to_i
+ else
+ 8
+ end
password = generate_password(length)
@ring.store(name, password)
@safe.write_safe @ring
@@ -61,7 +68,7 @@ def list
def load_from_safe
@safe.read_safe
end
-
+
def generate_password(len)
Array.new(len/2) { rand(256) }.pack('C*').unpack('H*').first
end
Please sign in to comment.
Something went wrong with that request. Please try again.