@@ -32,15 +32,25 @@ public class Random {
32
32
33
33
// MARK: - Public Initializers
34
34
//
35
-
35
+ /// Create a `Random` instance
36
+ ///
37
+ /// - parameter charSet: The default `CharSet`
36
38
public init ( _ charSet: CharSet ) {
37
39
self . charSet = charSet
38
40
}
39
41
42
+ /// Create a `Random` instance with default `CharSet` set to `.charSet32`
43
+ ///
40
44
convenience public init ( ) {
41
45
self . init ( CharSet . charSet32)
42
46
}
43
47
48
+ /// Create a `Random` instance
49
+ ///
50
+ /// - paramter chars: String of characters for the `CharSet`
51
+ ///
52
+ /// - throws: `.invalidCharCount` if String length not a multiple of 2
53
+ /// - throws: `.charsNotUnique` if any character repeats
44
54
convenience public init ( _ chars: String ) throws {
45
55
let charSet = try CharSet ( chars)
46
56
self . init ( charSet)
@@ -51,11 +61,20 @@ public class Random {
51
61
public var chars : String {
52
62
return charSet. chars
53
63
}
54
-
64
+
65
+ /// Sets the default `CharSet` for generating random strings
66
+ ///
67
+ /// - paramter charSet: The `CharSet` to use
55
68
public func use( _ charSet: CharSet ) {
56
69
self . charSet = charSet
57
70
}
58
71
72
+ /// Sets the default `CharSet` to use
73
+ ///
74
+ /// - paramter chars: String of characters for the `CharSet`
75
+ ///
76
+ /// - throws: `.invalidCharCount` if String length not a multiple of 2
77
+ /// - throws: `.charsNotUnique` if any character repeats
59
78
public func use( _ chars: String ) throws {
60
79
let charSet = try CharSet ( chars)
61
80
self . charSet = charSet
@@ -90,6 +109,7 @@ public class Random {
90
109
91
110
/// Generates a medium ID
92
111
///
112
+ /// - parameter charSet: The `CharSet` to use
93
113
///
94
114
/// - return: A string with a one in a billion chance of repeat in a million strings.
95
115
public func mediumID( _ charSet: CharSet ) -> String {
@@ -99,10 +119,8 @@ public class Random {
99
119
100
120
/// Generates a large ID
101
121
///
102
-
103
122
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
104
123
public func largeID( ) -> String {
105
-
106
124
return largeID ( self . charSet)
107
125
}
108
126
@@ -150,7 +168,7 @@ public class Random {
150
168
return string ( bits: 256 , charSet: charSet, secRand: & secRand)
151
169
}
152
170
153
- /// Generates a random session ID .
171
+ /// Generates a random string .
154
172
///
155
173
/// - parameter bits: Minimum bits of entropy.
156
174
///
@@ -162,18 +180,27 @@ public class Random {
162
180
return string ( bits: bits, secRand: & secRand)
163
181
}
164
182
165
- /// Generates a random session ID .
183
+ /// Generates a random string .
166
184
///
167
- /// - return: A string suitable for a OWASP recommended session ID. The returned string's characters
168
- /// are from the character set in use.
169
- public func sessionID( ) -> String {
170
- var secRand = true
171
- return string ( bits: 128 , secRand: & secRand)
185
+ /// - parameter bits: Minimum bits of entropy.
186
+ /// - parameter secRand: If _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
187
+ /// generate the random bytes used to generate the random characters for the returned string;
188
+ /// otherwise use `arc4random_buf` to generate random bytes.
189
+ ///
190
+ /// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
191
+ /// for the character set in use. The entropy returned is the smallest such multiple larger
192
+ /// than `bits`.
193
+ ///
194
+ /// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
195
+ /// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
196
+ public func string( bits: Float , secRand: inout Bool ) -> String {
197
+ return string ( bits: bits, charSet: self . charSet, secRand: & secRand)
172
198
}
173
199
174
200
/// Generates a random string.
175
201
///
176
202
/// - parameter bits: Minimum bits of entropy.
203
+ /// - parameter charSet: The `CharSet` to use
177
204
/// - parameter secRand: If _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
178
205
/// generate the random bytes used to generate the random characters for the returned string;
179
206
/// otherwise use `arc4random_buf` to generate random bytes.
@@ -184,27 +211,40 @@ public class Random {
184
211
///
185
212
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
186
213
/// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
187
- public func string( bits: Float , secRand: inout Bool ) -> String {
214
+ public func string( bits: Float , charSet : CharSet , secRand: inout Bool ) -> String {
188
215
guard 0 < bits else { return " " }
189
-
190
216
// `Bytes.random` sets `secRand`
191
217
let bytes = Bytes . random ( bits, charSet, & secRand)
192
-
193
218
// `Bytes.random` ensures enough bytes so this call will not fail
194
- return try ! string ( bits: bits, using: bytes)
219
+ return try ! string ( bits: bits, charSet : charSet , using: bytes)
195
220
}
196
221
197
222
/// Generates a random string.
198
223
///
199
224
/// - parameter bits: Minimum bits of entropy.
200
- /// - parameter bytes: __Bytes__ used to generate characters.
225
+ /// - parameter bytes: `Bytes` used to generate characters.
201
226
///
202
227
/// - throws: `.tooFewBytes` if there are an insufficient number of bytes to generate the string.
203
228
///
204
229
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
205
230
/// for the character set in use. The entropy returned is the smallest such multiple larger
206
231
/// than `bits`.
207
232
public func string( bits: Float , using bytes: [ UInt8 ] ) throws -> String {
233
+ return try string ( bits: bits, charSet: self . charSet, using: bytes)
234
+ }
235
+
236
+ /// Generates a random string.
237
+ ///
238
+ /// - parameter bits: Minimum bits of entropy.
239
+ /// - parameter charSet: The `CharSet` to use
240
+ /// - parameter bytes: `Bytes` used to generate characters.
241
+ ///
242
+ /// - throws: `.tooFewBytes` if there are an insufficient number of bytes to generate the string.
243
+ ///
244
+ /// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
245
+ /// for the character set in use. The entropy returned is the smallest such multiple larger
246
+ /// than `bits`.
247
+ public func string( bits: Float , charSet: CharSet , using bytes: [ UInt8 ] ) throws -> String {
208
248
guard !( bits < 0 ) else { throw EntropyStringError . negativeEntropy }
209
249
guard 0 < bytes. count else { return " " }
210
250
@@ -221,12 +261,12 @@ public class Random {
221
261
for chunk in 0 ..< chunks {
222
262
for slice in 0 ..< charSet. charsPerChunk {
223
263
let ndx = charSet. ndxFn ( bytes, chunk, slice)
224
- string. append ( char ( ndx) )
264
+ string. append ( char ( ndx, charSet ) )
225
265
}
226
266
}
227
267
for slice in 0 ..< partials {
228
268
let ndx = charSet. ndxFn ( bytes, chunks, slice)
229
- string. append ( char ( ndx) )
269
+ string. append ( char ( ndx, charSet ) )
230
270
}
231
271
return string
232
272
}
@@ -236,13 +276,23 @@ public class Random {
236
276
/// Gets a character from the current `CharSet` characters.
237
277
///
238
278
/// - parameter ndx: The index of the character
239
- /// - parameter chars: The characters string
240
279
///
241
280
/// - return: The character
242
281
private func char( _ ndx: CharSet . Ndx ) -> Character {
282
+ return char ( ndx, self . charSet)
283
+ }
284
+
285
+ /// Gets a character from the specified `CharSet` characters.
286
+ ///
287
+ /// - parameter ndx: The index of the character
288
+ /// - parameter chars: The characters string
289
+ ///
290
+ /// - return: The character
291
+ private func char( _ ndx: CharSet . Ndx , _ charSet: CharSet ) -> Character {
243
292
let chars = charSet. chars
244
293
guard Int ( ndx) < chars. characters. count else { fatalError ( " Index out of bounds " ) }
245
294
let charIndex = chars. index ( chars. startIndex, offsetBy: Int ( ndx) )
246
295
return chars [ charIndex]
247
296
}
297
+
248
298
}
0 commit comments