Skip to content

Commit a144ba5

Browse files
committed
Resolve some magic numbers
1 parent be9746d commit a144ba5

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

EntropyString.podspec

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
Pod::Spec.new do |s|
44
s.name = "EntropyString"
55
s.version = "0.5.1"
6-
s.summary = "Efficiently generate secure, random strings of specified entropy from various character sets."
6+
s.summary = "Efficiently generate cryptographically strong random strings of specified entropy from various character sets."
77

8-
# This description is used to generate tags and improve search results.
9-
# * Think: What does it do? Why did you write it? What is the focus?
10-
# * Try to keep it short, snappy and to the point.
11-
# * Write the description between the DESC delimiters below.
12-
# * Finally, don't worry about the indent, CocoaPods strips it!
138
s.description = <<-DESC
14-
Efficiently generate secure, randmo strings of specified entropy from various character sets for use when probabilisticly unique string identifiers are needed. Entropy is specified by total number of strings and acceptable risk of a repeat.
9+
Efficiently generate cryptographically strong and secure random strings of specified entropy from various character sets for use when probabilisticly unique string identifiers are needed. Entropy is calculated from a total number of strings and acceptable risk of a repeat.
1510
DESC
1611

1712
s.homepage = "https://github.com/#{s.name}/#{s.name}-Swift"
1813
s.license = { :type => "MIT", :file => "LICENSE" }
1914
s.authors = { "knoxen" => "paul@knoxen.com", "dingo sky" => "paul@dingosky.com" }
20-
# s.authors = { "dingo sky" => "paul@dingosky.com" }
2115
# s.social_media_url = "http://twitter.com/knoxen"
2216

2317
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
@@ -35,18 +29,9 @@ Efficiently generate secure, randmo strings of specified entropy from various ch
3529
# s.watchos.deployment_target = "2.0"
3630
# s.tvos.deployment_target = "9.0"
3731

38-
39-
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
40-
#
41-
# Specify the location from where the source should be retrieved.
42-
# Supports git, hg, bzr, svn and HTTP.
43-
#
44-
4532
s.source = { :git => "https://github.com/EntropyString/EntropyString-Swift.git", :tag => "#{s.version}" }
4633

4734
s.source_files = "Sources/**/*.swift"
4835
# s.public_header_files = "#{s.name}/{s.name}.h"
4936

50-
# s.dependency "JSONKit", "~> 1.4"
51-
5237
end

Sources/RandomString.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class RandomString {
201201
string.append(char(ndx, from: chars))
202202
}
203203
}
204-
for slice in 0..<partials {
204+
for slice in 0 ..< partials {
205205
let ndx = ndxFn(bytes, Int(chunks), Int(slice))
206206
string.append(char(ndx, from: chars))
207207
}
@@ -304,7 +304,7 @@ public class RandomString {
304304
///
305305
/// - return: The index into the `charSet64` characters.
306306
private func ndx64(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
307-
guard slice < 4 else { fatalError("Invalid slice for charSet64 chars") }
307+
guard slice < CharSet.charSet64.charsPerChunk else { fatalError("Invalid slice for charSet64 chars") }
308308
return ndxGen(bytes, chunk, slice, 6)
309309
}
310310

@@ -320,7 +320,7 @@ public class RandomString {
320320
///
321321
/// - return: The index into the `charSet32` characters.
322322
private func ndx32(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
323-
guard slice < 8 else { fatalError("Invalid slice for charSet32 chars") }
323+
guard slice < CharSet.charSet32.charsPerChunk else { fatalError("Invalid slice for charSet32 chars") }
324324
return ndxGen(bytes, chunk, slice, 5)
325325
}
326326

@@ -336,7 +336,7 @@ public class RandomString {
336336
///
337337
/// - return: The index into the `charSet16` characters.
338338
private func ndx16(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
339-
guard slice < 2 else { fatalError("Invalid slice for charSet16 chars") }
339+
guard slice < CharSet.charSet16.charsPerChunk else { fatalError("Invalid slice for charSet16 chars") }
340340
return (bytes[chunk]<<UInt8(4*slice))>>4
341341
}
342342

@@ -352,7 +352,7 @@ public class RandomString {
352352
///
353353
/// - return: The index into the `charSet8` characters.
354354
private func ndx8(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
355-
guard slice < 8 else { fatalError("Invalid slice for charSet8 chars") }
355+
guard slice < CharSet.charSet8.charsPerChunk else { fatalError("Invalid slice for charSet8 chars") }
356356
return ndxGen(bytes, chunk, slice, 3)
357357
}
358358

@@ -368,7 +368,7 @@ public class RandomString {
368368
///
369369
/// - return: The index into the `charSet4` characters.
370370
private func ndx4(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
371-
guard slice < 4 else { fatalError("Invalid slice for charSet4 chars") }
371+
guard slice < CharSet.charSet4.charsPerChunk else { fatalError("Invalid slice for charSet4 chars") }
372372
return (bytes[chunk]<<UInt8(2*slice))>>6
373373
}
374374

@@ -384,10 +384,21 @@ public class RandomString {
384384
///
385385
/// - return: The index into the `charSet2` characters.
386386
private func ndx2(_ bytes: Bytes, _ chunk: Int, _ slice: Int) -> UInt8 {
387-
guard slice < 8 else { fatalError("Invalid slice for charSet2 chars") }
387+
guard slice < CharSet.charSet2.charsPerChunk else { fatalError("Invalid slice for charSet2 chars") }
388388
return (bytes[chunk]<<UInt8(slice))>>7
389389
}
390390

391+
/// Determines index into general CharSet characters.
392+
///
393+
/// Each `slice` of bits is used to create a single character. A `chunk` is the number of
394+
/// __Bytes__ required for a exact multiple of `slice`s. This function indexes into the _chunk_
395+
/// chunk of __Bytes__ to get the _slice_ of bits for generating a character.
396+
///
397+
/// - parameter bytes: The __Bytes__ used for character generation
398+
/// - parameter chunk: The _chunk_ into the __Bytes__.
399+
/// - parameter slice: The _slice_ of the _chunk_.
400+
///
401+
/// - return: The index into the `charSet2` characters.
391402
private func ndxGen(_ bytes: Bytes, _ chunk: Int, _ slice: Int, _ bitsPerSlice: Int) -> UInt8 {
392403
var ndx: UInt8 = 0
393404

0 commit comments

Comments
 (0)