summaryrefslogtreecommitdiff
path: root/lib/trocla
diff options
context:
space:
mode:
authorasq <asq@asq.art.pl>2014-04-03 17:15:37 +0200
committerasq <asq@asq.art.pl>2014-04-03 17:22:18 +0200
commit4b0cd484e68a806e60a32a6d611333d41ec845aa (patch)
treebf9264582a9b30689a4b68edca4ebad96dbf5988 /lib/trocla
parent9fef5b8d944946ca9ea2f37e30780d3dba1d775e (diff)
add option to generate shell-safe passwords
basically excludes characters that might be dangerous if used in shell. many passwords generated by trocla may end up in some sort of bash scripts (initscripts, sourced shell variables, etc) which may yeld problems with default trocla random generator. this can be now changed either in troclarc (with "shellsafe: true") or on (ie. "trocla create foo plain '{ length: 32, shellsafe: true}'").
Diffstat (limited to 'lib/trocla')
-rw-r--r--lib/trocla/util.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/trocla/util.rb b/lib/trocla/util.rb
index 2b1c6c6..ff7e3ce 100644
--- a/lib/trocla/util.rb
+++ b/lib/trocla/util.rb
@@ -2,8 +2,12 @@ require 'securerandom'
class Trocla
class Util
class << self
- def random_str(length=12)
- (1..length).collect{|a| chars[SecureRandom.random_number(chars.size)] }.join.to_s
+ def random_str(length=12,shellsafe=:undef)
+ if shellsafe
+ (1..length).collect{|a| safechars[SecureRandom.random_number(safechars.size)] }.join.to_s
+ else
+ (1..length).collect{|a| chars[SecureRandom.random_number(chars.size)] }.join.to_s
+ end
end
def salt(length=8)
@@ -14,12 +18,18 @@ class Trocla
def chars
@chars ||= normal_chars + special_chars
end
+ def safechars
+ @chars ||= normal_chars + shellsafe_chars
+ end
def normal_chars
@normal_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
end
def special_chars
@special_chars ||= "+*%/()@&=?![]{}-_.,;:".split(//)
end
+ def shellsafe_chars
+ @shellsafe_chars ||= "+%/@=?_.,:".split(//)
+ end
end
end
end