aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/util/secret.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-11-04 11:30:16 -0800
committerelijah <elijah@riseup.net>2012-11-04 11:30:16 -0800
commit08b03669c262fd7ea67c7e2e5e5448a98db4ceef (patch)
tree6d08a0ae0fe6e7f8baf49fa58c52e7c0a23911cd /lib/leap_cli/util/secret.rb
parentb561a3eadd43218a59650b6255f8d12266b38884 (diff)
downloadleap_cli-08b03669c262fd7ea67c7e2e5e5448a98db4ceef.tar.gz
leap_cli-08b03669c262fd7ea67c7e2e5e5448a98db4ceef.tar.bz2
added automatic secret generation in secrets.json
Diffstat (limited to 'lib/leap_cli/util/secret.rb')
-rw-r--r--lib/leap_cli/util/secret.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/leap_cli/util/secret.rb b/lib/leap_cli/util/secret.rb
new file mode 100644
index 0000000..4833caa
--- /dev/null
+++ b/lib/leap_cli/util/secret.rb
@@ -0,0 +1,37 @@
+#
+# A simple alphanumeric secret generator, with no ambiguous characters.
+#
+# It also includes symbols that are treated as word characters by most
+# terminals (so you can still double click to select the entire secret).
+#
+# Uses OpenSSL random number generator instead of Ruby's rand function
+#
+
+require 'openssl'
+
+module LeapCli; module Util
+
+ class Secret
+
+ CHARS = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + "_-&@%~=+".split(//u) - "io01lO".split(//u)
+
+ def self.generate(length = 10)
+ seed
+ OpenSSL::Random.random_bytes(length).bytes.to_a.collect { |byte|
+ CHARS[ byte % CHARS.length ]
+ }.join
+ end
+
+ def self.seed
+ @pid ||= 0
+ pid = $$
+ if @pid != pid
+ now = Time.now
+ OpenSSL::Random.seed( [now.to_i, now.nsec, @pid, pid].join )
+ @pid = pid
+ end
+ end
+
+ end
+
+end; end