diff options
author | elijah <elijah@riseup.net> | 2013-04-02 15:43:15 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2013-04-02 15:43:15 -0700 |
commit | f77ee68d61646d27ea2c4098d14808b31f6d9a86 (patch) | |
tree | ac9e2327e0931df8b77120478ecf57c50f870460 /lib/leap_cli/util/secret.rb | |
parent | 86339c23f4a51fc9e4b9753f75366b8e7c0cc848 (diff) |
added support for hex_secrets
Diffstat (limited to 'lib/leap_cli/util/secret.rb')
-rw-r--r-- | lib/leap_cli/util/secret.rb | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/leap_cli/util/secret.rb b/lib/leap_cli/util/secret.rb index 691065f..47a050e 100644 --- a/lib/leap_cli/util/secret.rb +++ b/lib/leap_cli/util/secret.rb @@ -1,20 +1,23 @@ # -# A simple alphanumeric secret generator, with no ambiguous characters. -# -# Only alphanumerics are allowed, in order to make these passwords work -# for REST url calls and to allow you to easily copy and paste them. +# A simple secret generator # # 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) - + HEX = (0..9).to_a + ('a'..'f').to_a + + # + # generate a secret with with no ambiguous characters. + # + # +length+ is in chars + # + # Only alphanumerics are allowed, in order to make these passwords work + # for REST url calls and to allow you to easily copy and paste them. + # def self.generate(length = 16) seed OpenSSL::Random.random_bytes(length).bytes.to_a.collect { |byte| @@ -22,6 +25,20 @@ module LeapCli; module Util }.join end + # + # generates a hex secret, instead of an alphanumeric on. + # + # length is in bits + # + def self.generate_hex(length = 128) + seed + OpenSSL::Random.random_bytes(length/4).bytes.to_a.collect { |byte| + HEX[ byte % HEX.length ] + }.join + end + + private + def self.seed @pid ||= 0 pid = $$ @@ -33,5 +50,4 @@ module LeapCli; module Util end end - end; end |