@@ -37,21 +37,21 @@ public enum EntropyStringError: Error {
37
37
public class Entropy {
38
38
static let bitsPerByte : UInt8 = 8
39
39
40
- public private( set) var charSet : CharSet
40
+ public private( set) var charset : CharSet
41
41
42
42
// MARK: - Public Initializers
43
43
//
44
44
/// Create a `Entropy` instance
45
45
///
46
- /// - parameter charSet : The default `CharSet`
47
- public init ( _ charSet : CharSet ) {
48
- self . charSet = charSet
46
+ /// - parameter charset : The default `CharSet`
47
+ public init ( _ charset : CharSet ) {
48
+ self . charset = charset
49
49
}
50
50
51
- /// Create a `Entropy` instance with default `CharSet` set to `.charSet32 `
51
+ /// Create a `Entropy` instance with default `CharSet` set to `.charset32 `
52
52
///
53
53
convenience public init ( ) {
54
- self . init ( CharSet . charSet32 )
54
+ self . init ( CharSet . charset32 )
55
55
}
56
56
57
57
/// Create a `Entropy` instance
@@ -61,8 +61,8 @@ public class Entropy {
61
61
/// - throws: `.invalidCharCount` if String length not a multiple of 2
62
62
/// - throws: `.charsNotUnique` if any character repeats
63
63
convenience public init ( _ chars: String ) throws {
64
- let charSet = try CharSet ( chars)
65
- self . init ( charSet )
64
+ let charset = try CharSet ( chars)
65
+ self . init ( charset )
66
66
}
67
67
68
68
// MARK: - Public Static
@@ -82,9 +82,9 @@ public class Entropy {
82
82
//
83
83
/// Sets the default `CharSet` for generating random strings
84
84
///
85
- /// - paramter charSet : The `CharSet` to use
86
- public func use( _ charSet : CharSet ) {
87
- self . charSet = charSet
85
+ /// - paramter charset : The `CharSet` to use
86
+ public func use( _ charset : CharSet ) {
87
+ self . charset = charset
88
88
}
89
89
90
90
/// Sets the default `CharSet` to use
@@ -94,14 +94,14 @@ public class Entropy {
94
94
/// - throws: `.invalidCharCount` if String length not a multiple of 2
95
95
/// - throws: `.charsNotUnique` if any character repeats
96
96
public func use( _ chars: String ) throws {
97
- let charSet = try CharSet ( chars)
98
- self . charSet = charSet
97
+ let charset = try CharSet ( chars)
98
+ self . charset = charset
99
99
}
100
100
101
101
/// The characters of the default `CharSet`
102
- @available ( * , deprecated, message: " use charSet .chars instead " )
102
+ @available ( * , deprecated, message: " use charset .chars instead " )
103
103
public var chars : String {
104
- return charSet . chars
104
+ return charset . chars
105
105
}
106
106
107
107
// MARK: - Public API
@@ -115,80 +115,80 @@ public class Entropy {
115
115
116
116
/// Generates a small ID
117
117
///
118
- /// - parameter charSet : The `CharSet` to use
118
+ /// - parameter charset : The `CharSet` to use
119
119
///
120
120
/// - return: A string with a one in a million chance of repeat in 30 strings.
121
- public func smallID( _ charSet : CharSet ) -> String {
121
+ public func smallID( _ charset : CharSet ) -> String {
122
122
var secRand = true
123
- return string ( bits: 29 , charSet : charSet , secRand: & secRand)
123
+ return string ( bits: 29 , charset : charset , secRand: & secRand)
124
124
}
125
125
126
126
/// Generates a medium ID
127
127
///
128
128
/// - return: A string with a one in a billion chance of repeat in a million strings.
129
129
public func mediumID( ) -> String {
130
- return mediumID ( self . charSet )
130
+ return mediumID ( self . charset )
131
131
}
132
132
133
133
/// Generates a medium ID
134
134
///
135
- /// - parameter charSet : The `CharSet` to use
135
+ /// - parameter charset : The `CharSet` to use
136
136
///
137
137
/// - return: A string with a one in a billion chance of repeat in a million strings.
138
- public func mediumID( _ charSet : CharSet ) -> String {
138
+ public func mediumID( _ charset : CharSet ) -> String {
139
139
var secRand = true
140
- return string ( bits: 69 , charSet : charSet , secRand: & secRand)
140
+ return string ( bits: 69 , charset : charset , secRand: & secRand)
141
141
}
142
142
143
143
/// Generates a large ID
144
144
///
145
145
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
146
146
public func largeID( ) -> String {
147
- return largeID ( self . charSet )
147
+ return largeID ( self . charset )
148
148
}
149
149
150
150
/// Generates a large ID
151
151
///
152
- /// - parameter charSet : The `CharSet` to use
152
+ /// - parameter charset : The `CharSet` to use
153
153
///
154
154
/// - return: A string with a one in a trillion chance of repeat in a billion strings.
155
- public func largeID( _ charSet : CharSet ) -> String {
155
+ public func largeID( _ charset : CharSet ) -> String {
156
156
var secRand = true
157
- return string ( bits: 99 , charSet : charSet , secRand: & secRand)
157
+ return string ( bits: 99 , charset : charset , secRand: & secRand)
158
158
}
159
159
160
160
/// Generates a 128 bit random session ID.
161
161
///
162
162
/// - return: A string suitable for a OWASP recommended session ID.
163
163
public func sessionID( ) -> String {
164
- return sessionID ( self . charSet )
164
+ return sessionID ( self . charset )
165
165
}
166
166
167
167
/// Generates a 128 bit random session ID.
168
168
///
169
- /// - parameter charSet : The `CharSet` to use
169
+ /// - parameter charset : The `CharSet` to use
170
170
///
171
171
/// - return: A string suitable for a OWASP recommended session ID.
172
- public func sessionID( _ charSet : CharSet ) -> String {
172
+ public func sessionID( _ charset : CharSet ) -> String {
173
173
var secRand = true
174
- return string ( bits: 128 , charSet : charSet , secRand: & secRand)
174
+ return string ( bits: 128 , charset : charset , secRand: & secRand)
175
175
}
176
176
177
177
/// Generates a 256 bit random token
178
178
///
179
179
/// - return: A 256 bit string
180
180
public func token( ) -> String {
181
- return token ( self . charSet )
181
+ return token ( self . charset )
182
182
}
183
183
184
184
/// Generates a 256 bit random token
185
185
///
186
- /// - parameter charSet : The `CharSet` to use
186
+ /// - parameter charset : The `CharSet` to use
187
187
///
188
188
/// - return: A 256 bit string
189
- public func token( _ charSet : CharSet ) -> String {
189
+ public func token( _ charset : CharSet ) -> String {
190
190
var secRand = true
191
- return string ( bits: 256 , charSet : charSet , secRand: & secRand)
191
+ return string ( bits: 256 , charset : charset , secRand: & secRand)
192
192
}
193
193
194
194
/// Generates a random string.
@@ -210,9 +210,9 @@ public class Entropy {
210
210
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
211
211
/// for the character set in use. The entropy returned is the smallest such multiple larger
212
212
/// than `bits`.
213
- public func string( bits: Float , charSet : CharSet ) -> String {
213
+ public func string( bits: Float , charset : CharSet ) -> String {
214
214
var secRand = true
215
- return string ( bits: bits, charSet : charSet , secRand: & secRand)
215
+ return string ( bits: bits, charset : charset , secRand: & secRand)
216
216
}
217
217
218
218
/// Generates a random string.
@@ -229,13 +229,13 @@ public class Entropy {
229
229
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
230
230
/// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
231
231
public func string( bits: Float , secRand: inout Bool ) -> String {
232
- return string ( bits: bits, charSet : self . charSet , secRand: & secRand)
232
+ return string ( bits: bits, charset : self . charset , secRand: & secRand)
233
233
}
234
234
235
235
/// Generates a random string.
236
236
///
237
237
/// - parameter bits: Minimum bits of entropy.
238
- /// - parameter charSet : The `CharSet` to use
238
+ /// - parameter charset : The `CharSet` to use
239
239
/// - parameter secRand: If _secRand_ is `true`, attempt to use `SecRandomCopyBytes` to
240
240
/// generate the random bytes used to generate the random characters for the returned string;
241
241
/// otherwise use `arc4random_buf` to generate random bytes.
@@ -246,12 +246,12 @@ public class Entropy {
246
246
///
247
247
/// If _secRand_ is passed in as `true`, the value of _secRand_ on return indicates whether
248
248
/// `SecRandomCopyBytes` (`true`) or `arc4random_buf` (`false`) was used.
249
- public func string( bits: Float , charSet : CharSet , secRand: inout Bool ) -> String {
249
+ public func string( bits: Float , charset : CharSet , secRand: inout Bool ) -> String {
250
250
guard 0 < bits else { return " " }
251
251
// `Bytes.random` sets `secRand`
252
- let bytes = Bytes . random ( bits, charSet , & secRand)
252
+ let bytes = Bytes . random ( bits, charset , & secRand)
253
253
// `Bytes.random` ensures enough bytes so this call will not fail
254
- return try ! string ( bits: bits, charSet : charSet , using: bytes)
254
+ return try ! string ( bits: bits, charset : charset , using: bytes)
255
255
}
256
256
257
257
/// Generates a random string.
@@ -265,43 +265,43 @@ public class Entropy {
265
265
/// for the character set in use. The entropy returned is the smallest such multiple larger
266
266
/// than `bits`.
267
267
public func string( bits: Float , using bytes: [ UInt8 ] ) throws -> String {
268
- return try string ( bits: bits, charSet : self . charSet , using: bytes)
268
+ return try string ( bits: bits, charset : self . charset , using: bytes)
269
269
}
270
270
271
271
/// Generates a random string.
272
272
///
273
273
/// - parameter bits: Minimum bits of entropy.
274
- /// - parameter charSet : The `CharSet` to use
274
+ /// - parameter charset : The `CharSet` to use
275
275
/// - parameter bytes: `Bytes` used to generate characters.
276
276
///
277
277
/// - throws: `.tooFewBytes` if there are an insufficient number of bytes to generate the string.
278
278
///
279
279
/// - return: A string. The returned string's entropy is a multiple of the _entropy per char_
280
280
/// for the character set in use. The entropy returned is the smallest such multiple larger
281
281
/// than `bits`.
282
- public func string( bits: Float , charSet : CharSet , using bytes: [ UInt8 ] ) throws -> String {
282
+ public func string( bits: Float , charset : CharSet , using bytes: [ UInt8 ] ) throws -> String {
283
283
guard !( bits < 0 ) else { throw EntropyStringError . negativeEntropy }
284
284
guard 0 < bytes. count else { return " " }
285
285
286
- let count : Int = Int ( ceil ( bits / Float( charSet . bitsPerChar) ) )
286
+ let count : Int = Int ( ceil ( bits / Float( charset . bitsPerChar) ) )
287
287
guard 0 < count else { return " " }
288
288
289
- let needed = Int ( ceil ( Float ( charSet . bitsPerChar) / 8 * Float( count) ) )
289
+ let needed = Int ( ceil ( Float ( charset . bitsPerChar) / 8 * Float( count) ) )
290
290
guard needed <= bytes. count else { throw EntropyStringError . tooFewBytes }
291
291
292
- let chunks = count / Int( charSet . charsPerChunk)
293
- let partials = UInt8 ( count % Int( charSet . charsPerChunk) )
292
+ let chunks = count / Int( charset . charsPerChunk)
293
+ let partials = UInt8 ( count % Int( charset . charsPerChunk) )
294
294
295
295
var string = " "
296
296
for chunk in 0 ..< chunks {
297
- for slice in 0 ..< charSet . charsPerChunk {
298
- let ndx = charSet . ndxFn ( bytes, chunk, slice)
299
- string. append ( char ( ndx, charSet ) )
297
+ for slice in 0 ..< charset . charsPerChunk {
298
+ let ndx = charset . ndxFn ( bytes, chunk, slice)
299
+ string. append ( char ( ndx, charset ) )
300
300
}
301
301
}
302
302
for slice in 0 ..< partials {
303
- let ndx = charSet . ndxFn ( bytes, chunks, slice)
304
- string. append ( char ( ndx, charSet ) )
303
+ let ndx = charset . ndxFn ( bytes, chunks, slice)
304
+ string. append ( char ( ndx, charset ) )
305
305
}
306
306
return string
307
307
}
@@ -314,7 +314,7 @@ public class Entropy {
314
314
///
315
315
/// - return: The character
316
316
private func char( _ ndx: CharSet . Ndx ) -> Character {
317
- return char ( ndx, self . charSet )
317
+ return char ( ndx, self . charset )
318
318
}
319
319
320
320
/// Gets a character from the specified `CharSet` characters.
@@ -323,8 +323,8 @@ public class Entropy {
323
323
/// - parameter chars: The characters string
324
324
///
325
325
/// - return: The character
326
- private func char( _ ndx: CharSet . Ndx , _ charSet : CharSet ) -> Character {
327
- let chars = charSet . chars
326
+ private func char( _ ndx: CharSet . Ndx , _ charset : CharSet ) -> Character {
327
+ let chars = charset . chars
328
328
guard Int ( ndx) < chars. count else { fatalError ( " Index out of bounds " ) }
329
329
let charIndex = chars. index ( chars. startIndex, offsetBy: Int ( ndx) )
330
330
return chars [ charIndex]
0 commit comments