@@ -31,6 +31,7 @@ class ExhaustedSpaceError < StandardError; end
31
31
:min_length => 3 ,
32
32
# This should be pretty universal for identifier rules
33
33
:char_set => Rex ::Text ::AlphaNumeric +"_" ,
34
+ :first_char_set => Rex ::Text ::LowerAlpha
34
35
}
35
36
36
37
# @param opts [Hash] Options, see {DefaultOpts} for default values
@@ -46,8 +47,8 @@ def initialize(opts={})
46
47
raise ArgumentError , "Invalid length options"
47
48
end
48
49
49
- # This is really just the maximum number of shortest names. Since
50
- # this will still be a pretty big number most of the time, don't
50
+ # This is really just the maximum number of shortest names. This
51
+ # will still be a pretty big number most of the time, so don't
51
52
# bother calculating the real one, which will potentially be
52
53
# expensive, since we're talking about a 36-digit decimal number to
53
54
# represent the total possibilities for the range of 10- to
@@ -108,9 +109,17 @@ def store(name, value)
108
109
end
109
110
110
111
# Create a random string that satisfies most languages' requirements
111
- # for identifiers.
112
+ # for identifiers. In particular, with a default configuration, the
113
+ # first character will always be lowercase alpha (unless modified by a
114
+ # block), and the whole thing will contain only a-zA-Z0-9_ characters.
112
115
#
113
- # Note that the first character will always be lowercase alpha.
116
+ # If called with a block, the block will be given the identifier before
117
+ # uniqueness checks. The block's return value will be the new
118
+ # identifier. Note that the block may be called multiple times if it
119
+ # returns a non-unique value.
120
+ #
121
+ # @note Calling this method with a block that returns only values that
122
+ # this generator already contains will result in an infinite loop.
114
123
#
115
124
# @example
116
125
# rig = Rex::RandomIdentifierGenerator.new
@@ -139,7 +148,7 @@ def generate(len=nil)
139
148
# fact that you'd have to call generate at least 26*62 times (in the
140
149
# case of 2-character names) to hit it with the default :char_set.
141
150
loop do
142
- ident = Rex ::Text . rand_text_alpha_lower ( 1 )
151
+ ident = Rex ::Text . rand_base ( 1 , "" , @opts [ :first_char_set ] )
143
152
ident << Rex ::Text . rand_base ( len -1 , "" , @opts [ :char_set ] )
144
153
if block_given?
145
154
ident = yield ident
0 commit comments