summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormh <mh@immerda.ch>2011-08-05 23:27:52 +0200
committermh <mh@immerda.ch>2011-08-05 23:27:52 +0200
commit02d0ab7cb6f9490e8846c0cb703a9efdb9e6518f (patch)
treec793aed5113a72e774087d6286ac62ed0b958c75
parent1d5f20953d6a4bf6de8dab1d915b4d252eb4a790 (diff)
salt should not really containt special characters
-rw-r--r--lib/trocla/util.rb16
-rw-r--r--spec/trocla/util_spec.rb33
2 files changed, 34 insertions, 15 deletions
diff --git a/lib/trocla/util.rb b/lib/trocla/util.rb
index 468206d..5bca8f1 100644
--- a/lib/trocla/util.rb
+++ b/lib/trocla/util.rb
@@ -4,11 +4,21 @@ class Trocla
def random_str(length=12)
(1..length).collect{|a| chars[rand(chars.size)] }.join.to_s
end
-
+
+ def salt(length=8)
+ (1..length).collect{|a| normal_chars[rand(normal_chars.size)] }.join.to_s
+ end
+
private
def chars
- @chars ||= (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a) + "+*%/()@&=?![]{}-_.,;:<>".split(//)
+ @chars ||= normal_chars + special_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
end
end
-end \ No newline at end of file
+end
diff --git a/spec/trocla/util_spec.rb b/spec/trocla/util_spec.rb
index 4e02d97..879b244 100644
--- a/spec/trocla/util_spec.rb
+++ b/spec/trocla/util_spec.rb
@@ -1,19 +1,28 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Trocla::Util" do
- describe "random_str" do
- it "should be random" do
- Trocla::Util.random_str.should_not eql(Trocla::Util.random_str)
- end
+
+ { :random_str => 12, :salt => 8 }.each do |m,length|
+ describe m do
+ it "should be random" do
+ Trocla::Util.send(m).should_not eql(Trocla::Util.send(m))
+ end
- it "should default to length 12" do
- Trocla::Util.random_str.length.should == 12
- end
+ it "should default to length #{length}" do
+ Trocla::Util.send(m).length.should == length
+ end
- it "should be possible to change length" do
- Trocla::Util.random_str(8).length.should == 8
- Trocla::Util.random_str(32).length.should == 32
- Trocla::Util.random_str(1).length.should == 1
+ it "should be possible to change length" do
+ Trocla::Util.send(m,8).length.should == 8
+ Trocla::Util.send(m,32).length.should == 32
+ Trocla::Util.send(m,1).length.should == 1
+ end
+ end
+ end
+
+ describe :salt do
+ it "should only contain characters and numbers" do
+ Trocla::Util.salt =~ /^[a-z0-9]+$/i
end
end
-end \ No newline at end of file
+end