Skip to content

Commit 73a6393

Browse files
committed
Refactoring
Rename char sets Move CharSet
1 parent 5841b2f commit 73a6393

File tree

17 files changed

+645
-524
lines changed

17 files changed

+645
-524
lines changed

Entropy.playground/Pages/Basic Usage.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Basic Usage.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import EntropyString
66
//: Calculate bits of entropy to cover **1 million strings** with a repeat *risk* of
77
//: **1 in a billion** and generate a string using a set of 32 characters:
88
let bits = Entropy.bits(for: .ten06, risk: .ten09)
9-
var string = RandomString.entropy(of: bits, using: .base32)
9+
var string = RandomString.entropy(of: bits, using: .charSet32)
1010
print(string)
1111
//: * callout(string): 9Pp7MDDm7b9Dhb
1212
//:
1313
//: Generate a string of the same entropy using set of 16 characters (hexadecimal)
14-
string = RandomString.entropy(of: bits, using: .base16)
14+
string = RandomString.entropy(of: bits, using: .charSet16)
1515
print(string)
1616
//: * callout(string): d33fa62f572c4cc9c8
1717
//:

Entropy.playground/Pages/Character Bases.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Character Bases.xcplaygroundpage/Contents.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
//: ## Character Bases
33
//:
44
//: As we've seen in the previous sections, `EntropyString` provides default characters for each of
5-
//: the supported bases. Let's see what's under the hood.
5+
//: the supported character sets. Let's see what's under the hood.
66
import EntropyString
77

8-
print("Base 64: \(RandomString.characters(for: .base64))\n")
8+
print("Base 64: \(RandomString.characters(for: .charSet64))\n")
99
//: The call to `RandomString.characters(for:)` returns the characters used for any of the
10-
//: bases defined by the `RandomString.CharBase enum`. The following code reveals all the
11-
//: character bases.
12-
print("Base 32: \(RandomString.characters(for: .base32))\n")
13-
print("Base 16: \(RandomString.characters(for: .base16))\n")
14-
print("Base 8: \(RandomString.characters(for: .base8))\n")
15-
print("Base 4: \(RandomString.characters(for: .base4))\n")
16-
print("Base 2: \(RandomString.characters(for: .base2))\n")
17-
//: The default character bases were chosen as follows:
10+
//: character sets defined by the `RandomString.CharBase enum`. The following code reveals all the
11+
//: character sets.
12+
print("Base 32: \(RandomString.characters(for: .charSet32))\n")
13+
print("Base 16: \(RandomString.characters(for: .charSet16))\n")
14+
print("Base 8: \(RandomString.characters(for: .charSet8))\n")
15+
print("Base 4: \(RandomString.characters(for: .charSet4))\n")
16+
print("Base 2: \(RandomString.characters(for: .charSet2))\n")
17+
//: The default character sets were chosen as follows:
1818
//: - Base 64: **ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_**
1919
//: - The file system and URL safe char set from
2020
//: [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5).

Entropy.playground/Pages/Custom Bytes.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Custom Bytes.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import EntropyString
1010

1111
let bytes: RandomString.Bytes = [250, 200, 150, 100]
12-
let string = try! RandomString.entropy(of: 30, using: .base32, bytes: bytes)
12+
let string = try! RandomString.entropy(of: 30, using: .charSet32, bytes: bytes)
1313
print("String: \(string)\n")
1414
//: * callout(string): Th7fjL
1515
//:
@@ -18,7 +18,7 @@ print("String: \(string)\n")
1818
//: `RandomString.entropy(of:using:bytes)` throws `RandomString.RandomError.tooFewBytes` if
1919
//: the string cannot be formed from the passed bytes.
2020
do {
21-
try RandomString.entropy(of: 32, using: .base32, bytes: bytes)
21+
try RandomString.entropy(of: 32, using: .charSet32, bytes: bytes)
2222
}
2323
catch {
2424
print(error)

Entropy.playground/Pages/Custom Characters.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Custom Characters.xcplaygroundpage/Contents.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77
import EntropyString
88

99
let randomString = RandomString()
10-
var flips = randomString.entropy(of: 10, using: .base2)
10+
var flips = randomString.entropy(of: 10, using: .charSet2)
1111
print("flips: \(flips)\n")
1212
//: * callout(flips): 0101001110
1313
//:
1414
//: The resulting string of __0__'s and __1__'s doesn't look quite right. You want to use the
1515
//: characters __H__ and __T__ instead.
16-
try! randomString.use("HT", for: .base2)
17-
flips = randomString.entropy(of: 10, using: .base2)
16+
try! randomString.use("HT", for: .charSet2)
17+
flips = randomString.entropy(of: 10, using: .charSet2)
1818
print("flips: \(flips)\n")
1919
//: * callout(flips): HTTTHHTTHH
2020
//:
2121
//: Note that setting custom characters in the above code requires using an *instance* of
2222
//: `RandomString`, wheras in the previous sections we used *class* functions for all calls. The
23-
//: function signatures are the same in each case, but you can't change the static character bases
23+
//: function signatures are the same in each case, but you can't change the static character sets
2424
//: used in the class `RandomString` (i.e., there is no `RandomString.use(_,for:)` function).
2525
//:
2626
//: As another example, we saw in [Character Bases](Character%20Bases) the default characters for
27-
//: base 16 are **01234567890abcdef**. Suppose you like uppercase hexadecimal letters instead.
28-
try! randomString.use("0123456789ABCDEF", for: .base16)
29-
let hex = randomString.entropy(of: 48, using: .base16)
27+
//: charSet 16 are **01234567890abcdef**. Suppose you like uppercase hexadecimal letters instead.
28+
try! randomString.use("0123456789ABCDEF", for: .charSet16)
29+
let hex = randomString.entropy(of: 48, using: .charSet16)
3030
print("hex: \(hex)\n")
3131
//: * callout(hex): 4D20D9AA862C
3232
//:
3333
//: Or suppose you want a random password with numbers, lowercase letters and special characters.
34-
try! randomString.use("1234567890abcdefghijklmnopqrstuvwxyz-=[];,./~!@#$%^&*()_+{}|:<>?", for: .base64)
35-
let password = randomString.entropy(of: 64, using: .base64)
34+
try! randomString.use("1234567890abcdefghijklmnopqrstuvwxyz-=[];,./~!@#$%^&*()_+{}|:<>?", for: .charSet64)
35+
let password = randomString.entropy(of: 64, using: .charSet64)
3636
print("password: \(password)")
3737
//: * callout(password): }4?0x*$o_=w
3838
//:
3939
//: Note that `randomString.use(_,for:)` can throw an `Error`. The throw is actually a
4040
//: `RandomStringError` and will occur if the number of characters doesn't match the number
41-
//: required for the base or if the characters are not all unique. The section on [Unique
41+
//: required for the character set or if the characters are not all unique. The section on [Unique
4242
//: Characters](Unique%20Characters) discusses these errors further.
4343
//:
4444
//: [TOC](Table%20of%20Contents) | [Next](@next)

Entropy.playground/Pages/Efficiency.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Efficiency.xcplaygroundpage/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
//:
44
//: To efficiently create random strings, `EntropyString` generates the necessary number of
55
//: bytes needed for each the string and uses those bytes in a bit shifting scheme to index into
6-
//: a character base. For example, consider generating strings from the `.base32` character
7-
//: base. There are __32__ characters in the base, so an index into an array of those characters
8-
//: would be in the range `[0,31]`. Generating a random string of `.base32` characters is thus
6+
//: a character set. For example, consider generating strings from the `.charSet32` character
7+
//: set. There are __32__ characters in the set, so an index into an array of those characters
8+
//: would be in the range `[0,31]`. Generating a random string of `.charSet32` characters is thus
99
//: reduced to generating a random sequence of indices in the range `[0,31]`.
1010
//:
1111
//: To generate the indices, `EntropyString` slices just enough bits from the array of bytes to create
1212
//: each index. In the example at hand, 5 bits are needed to create an index in the range
1313
//: `[0,31]`. `EntropyString` processes the byte array 5 bits at a time to create the indices. The first
1414
//: index comes from the first 5 bits of the first byte, the second index comes from the last 3 bits of
1515
//: the first byte combined with the first 2 bits of the second byte, and so on as the byte array is
16-
//: systematically sliced to form indices into the character base. And since bit shifting and addition
16+
//: systematically sliced to form indices into the character set. And since bit shifting and addition
1717
//: of byte values is really efficient, this scheme is quite fast.
1818
//:
1919
//: The `EntropyString` scheme is also efficient with regard to the amount of randomness used. Consider

Entropy.playground/Pages/More Examples.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/More Examples.xcplaygroundpage/Contents.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import EntropyString
1212

1313
var bits = Entropy.bits(total: 10000, risk: .ten06)
14-
var string = RandomString.entropy(of: bits, using: .base32)
14+
var string = RandomString.entropy(of: bits, using: .charSet32)
1515
print("String: \(string)\n")
1616
//: * callout(string): PmgMJrdp9h
1717
//:
@@ -24,24 +24,24 @@ print("String: \(string)\n")
2424
//: items. And let's decide we can live with a 1 in 100,000 probability of collision (we're just
2525
//: futzing with some code ideas). Using hex characters we get:
2626
bits = Entropy.bits(total: 30, risk: .ten05)
27-
string = RandomString.entropy(of: bits, using: .base16)
27+
string = RandomString.entropy(of: bits, using: .charSet16)
2828
print("String: \(string)\n")
2929
//: * callout(string): 766923a
3030
//:
31-
//: Using base 4 characters we get:
32-
string = RandomString.entropy(of: bits, using: .base4)
31+
//: Using 4 characters we get:
32+
string = RandomString.entropy(of: bits, using: .charSet4)
3333
print("String: \(string)\n")
3434
//: * callout(string): GCGTCGGGTTTTA
3535
//:
36-
//: Okay, we probably wouldn't use base 4 (and what's up with those characters?), but you get the
36+
//: Okay, we probably wouldn't use 4 characters (and what's up with those characters?), but you get the
3737
//: idea.
3838
//:
3939
//: Suppose we have a more extreme need. We want less than a 1 in a trillion chance that 10
4040
//: billion strings of 32 characters repeat. Let's see, our risk (trillion) is 10 to the 12th and
4141
//: our total (10 billion) is 10 to the 10th, so:
4242
//:
4343
bits = Entropy.bits(total: .ten10, risk: .ten12)
44-
string = RandomString.entropy(of: bits, using: .base32)
44+
string = RandomString.entropy(of: bits, using: .charSet32)
4545
print("String: \(string)\n")
4646
//: * callout(string): F78PmfGRNfJrhHGTqpt6Hn
4747
//:
@@ -50,7 +50,7 @@ print("String: \(string)\n")
5050
//: this case, we're using entropy as a measure of unpredictability of the IDs. Rather than calculate
5151
//: our entropy, we declare it needs to be 128 bits (since we read on some web site that session IDs
5252
//: should be 128 bits).
53-
string = RandomString.entropy(of: 128, using: .base64)
53+
string = RandomString.entropy(of: 128, using: .charSet64)
5454
print("String: \(string)\n")
5555
//: * callout(string): b0Gnh6H5cKCjWrCLwKoeuN
5656
//:

Entropy.playground/Pages/Overview.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Overview.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//: ## Overview
44
//:
55
//: `EntropyString` provides easy creation of randomly generated strings of specific entropy using
6-
//: various character bases. Such strings are needed when generating, for example, random IDs and
6+
//: various character sets. Such strings are needed when generating, for example, random IDs and
77
//: you don't want the overkill of a GUID, or for ensuring that some number of items have unique
88
//: names.
99
//:

Entropy.playground/Pages/Real Need.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Real Need.xcplaygroundpage/Contents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import EntropyString
5555
let bits = Entropy.bits(total: 10000, risk: .ten06)
5656
var strings = [String]()
5757
for i in 0 ..< 5 {
58-
let string = RandomString.entropy(of: bits, using: .base16)
58+
let string = RandomString.entropy(of: bits, using: .charSet16)
5959
strings.append(string)
6060
}
6161
print("Strings: \(strings)")
@@ -72,10 +72,10 @@ print("Strings: \(strings)")
7272
//: result, but if you did you'd see it's about **45.51**. Then inside a loop we used
7373
//:
7474
//: ```swift
75-
//: let string = RandomString.entropy(of: bits, using: .base16)
75+
//: let string = RandomString.entropy(of: bits, using: .charSet16)
7676
//: ```
7777
//:
78-
//: to actually generate random strings using hexadecimal (base16) characters. Looking at the IDs, we can
78+
//: to actually generate random strings using hexadecimal (charSet16) characters. Looking at the IDs, we can
7979
//: see each is 12 characters long. Again, the string length is a by-product of the characters used to
8080
//: represent the entropy we needed. And it seems the developer didn't really need 16 characters after all.
8181
//:

Entropy.playground/Pages/Secure Bytes.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Secure Bytes.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import EntropyString
2121

2222
var secure = true
23-
RandomString.entropy(of: 20, using: .base32, secure: &secure)
23+
RandomString.entropy(of: 20, using: .charSet32, secure: &secure)
2424
print("secure: \(secure)")
2525
//: * callout(secure): true
2626
//:
@@ -30,7 +30,7 @@ print("secure: \(secure)")
3030
//: You can also pass in __secure__ as `false`, in which case the `entropy` call will not
3131
//: attempt to use `SecRandomCopyBytes` and will use `arc4random_buf` instead.
3232
secure = false
33-
RandomString.entropy(of: 20, using: .base32, secure: &secure)
33+
RandomString.entropy(of: 20, using: .charSet32, secure: &secure)
3434
//: Rather than have `EntropyString` generate bytes automatically, you can provide your own [Custom
3535
//: Bytes](Custom%20Bytes) to create a string, which is the next topic.
3636
//:

Entropy.playground/Pages/Table of Contents.xcplaygroundpage/Contents.swift renamed to EntropyString.playground/Pages/Table of Contents.xcplaygroundpage/Contents.swift

File renamed without changes.

0 commit comments

Comments
 (0)