Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2923a29

Browse files
committed
Allow CharSet as parameter
Override default CharSet per call
1 parent 2cbf79f commit 2923a29

File tree

1 file changed

+69
-19
lines changed

1 file changed

+69
-19
lines changed

‎Sources/Random.swift

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,25 @@ public class Random {
3232

3333
// MARK: - Public Initializers
3434
//
35-
35+
/// Create a `Random` instance
36+
///
37+
/// - parameter charSet: The default `CharSet`
3638
public init(_ charSet: CharSet) {
3739
self.charSet = charSet
3840
}
3941

42+
/// Create a `Random` instance with default `CharSet` set to `.charSet32`
43+
///
4044
convenience public init() {
4145
self.init(CharSet.charSet32)
4246
}
4347

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
4454
convenience public init(_ chars: String) throws {
4555
let charSet = try CharSet(chars)
4656
self.init(charSet)
@@ -51,11 +61,20 @@ public class Random {
5161
public var chars: String {
5262
return charSet.chars
5363
}
54-
64+
65+
/// Sets the default `CharSet` for generating random strings
66+
///
67+
/// - paramter charSet: The `CharSet` to use
5568
public func use(_ charSet: CharSet) {
5669
self.charSet = charSet
5770
}
5871

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
5978
public func use(_ chars: String) throws {
6079
let charSet = try CharSet(chars)
6180
self.charSet = charSet
@@ -90,6 +109,7 @@ public class Random {
90109

91110
/// Generates a medium ID
92111
///
112+
/// - parameter charSet: The `CharSet` to use
93113
///
94114
/// - return: A string with a one in a billion chance of repeat in a million strings.
95115
public func mediumID(_ charSet: CharSet) -> String {
@@ -99,10 +119,8 @@ public class Random {
99119

100120
/// Generates a large ID
101121
///
102-
103122
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
104123
public func largeID() -> String {
105-
106124
return largeID(self.charSet)
107125
}
108126

@@ -150,7 +168,7 @@ public class Random {
150168
return string(bits: 256, charSet: charSet, secRand: &secRand)
151169
}
152170

153-
/// Generates a random session ID.
171+
/// Generates a random string.
154172
///
155173
/// - parameter bits: Minimum bits of entropy.
156174
///
@@ -162,18 +180,27 @@ public class Random {
162180
return string(bits: bits, secRand: &secRand)
163181
}
164182

165-
/// Generates a random session ID.
183+
/// Generates a random string.
166184
///
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)
172198
}
173199

174200
/// Generates a random string.
175201
///
176202
/// - parameter bits: Minimum bits of entropy.
203+
/// - parameter charSet: The `CharSet` to use
177204
/// - parameter secRand: If _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
178205
/// generate the random bytes used to generate the random characters for the returned string;
179206
/// otherwise use `arc4random_buf` to generate random bytes.
@@ -184,27 +211,40 @@ public class Random {
184211
///
185212
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
186213
/// `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 {
188215
guard 0 < bits else { return "" }
189-
190216
// `Bytes.random` sets `secRand`
191217
let bytes = Bytes.random(bits, charSet, &secRand)
192-
193218
// `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)
195220
}
196221

197222
/// Generates a random string.
198223
///
199224
/// - parameter bits: Minimum bits of entropy.
200-
/// - parameter bytes: __Bytes__ used to generate characters.
225+
/// - parameter bytes: `Bytes` used to generate characters.
201226
///
202227
/// - throws: `.tooFewBytes` if there are an insufficient number of bytes to generate the string.
203228
///
204229
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
205230
/// for the character set in use. The entropy returned is the smallest such multiple larger
206231
/// than `bits`.
207232
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 {
208248
guard !(bits < 0) else { throw EntropyStringError.negativeEntropy }
209249
guard 0 < bytes.count else { return "" }
210250

@@ -221,12 +261,12 @@ public class Random {
221261
for chunk in 0 ..< chunks {
222262
for slice in 0 ..< charSet.charsPerChunk {
223263
let ndx = charSet.ndxFn(bytes, chunk, slice)
224-
string.append(char(ndx))
264+
string.append(char(ndx, charSet))
225265
}
226266
}
227267
for slice in 0 ..< partials {
228268
let ndx = charSet.ndxFn(bytes, chunks, slice)
229-
string.append(char(ndx))
269+
string.append(char(ndx, charSet))
230270
}
231271
return string
232272
}
@@ -236,13 +276,23 @@ public class Random {
236276
/// Gets a character from the current `CharSet` characters.
237277
///
238278
/// - parameter ndx: The index of the character
239-
/// - parameter chars: The characters string
240279
///
241280
/// - return: The character
242281
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 {
243292
let chars = charSet.chars
244293
guard Int(ndx) < chars.characters.count else { fatalError("Index out of bounds") }
245294
let charIndex = chars.index(chars.startIndex, offsetBy: Int(ndx))
246295
return chars[charIndex]
247296
}
297+
248298
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /