Skip to content

Commit bc798ae

Browse files
committed
Doc updates
1 parent dbf2fa6 commit bc798ae

File tree

8 files changed

+29
-44
lines changed

8 files changed

+29
-44
lines changed

EntropyString.playground/Pages/Custom Bytes.xcplaygroundpage/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ catch {
2929
//: * callout(error): tooFewBytes
3030
//:
3131
//: Note how the number of bytes needed is dependent on the number of characters in our set.
32-
//: In using a string to represent entropy, we can only generate entropy in multiples of the
33-
//: bits of entropy per character used. So in the example above, to get at least 32 bits of
34-
//: entropy using a character set of 32 characters (5 bits per char), we'll need enough bytes
35-
//: to cover 35 bits, not 32, so a `tooFewBytes` error is thrown.
32+
//: In using a string to represent entropy, we can only have multiples of the bits of entropy per
33+
//: character used. So in the example above, to get at least 32 bits of entropy using a character
34+
//: set of 32 characters (5 bits per char), we'll need enough bytes to cover 35 bits, not 32, so
35+
//: a `tooFewBytes` error is thrown.
3636
//:
3737
//: [TOC](Table%20of%20Contents)

EntropyString.playground/Pages/Custom Characters.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ print("flips: \(flips)\n")
2424
//: used in the class `RandomString` (i.e., there is no `RandomString.use(_,for:)` function).
2525
//:
2626
//: As another example, we saw in [Character Sets](Character%20Sets) the default characters for
27-
//: charSet 16 are **01234567890abcdef**. Suppose you like uppercase hexadecimal letters instead.
27+
//: charSet 16 are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
2828
try! randomString.use("0123456789ABCDEF", for: .charSet16)
2929
let hex = randomString.entropy(of: 48, using: .charSet16)
3030
print("hex: \(hex)\n")

EntropyString.playground/Pages/Efficiency.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//: (`log2(32)`) of entropy into **string**. The resulting string has an information carrying capacity of
3434
//: 80 bits. So creating each **string** requires a *total* of 512 bits of randomness while only actually
3535
//: *carrying* 80 bits of that entropy forward in the string itself. That means 432 bits (84% of the total!)
36-
//: of the generated randomness is simply wasted away.
36+
//: of the generated randomness is simply wasted.
3737
//:
3838
//: Compare that to the `EntropyString` scheme. For the example above, slicing off 5 bits at a time
3939
//: requires a total of 80 bits (10 bytes). Creating the same strings as above, `EntropyString` uses 80

EntropyString.playground/Pages/More Examples.xcplaygroundpage/Contents.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ print("String: \(string)\n")
1717
//:
1818
//: We're using the same __bits__ calculation since we haven't changed the number of IDs or the
1919
//: accepted risk of probabilistic uniqueness. But this time we use 32 characters and our resulting
20-
//: ID only requires 10 characters (and can carry 50 bits of entropy, which as when we used 16
21-
//: characters, is more than the required 45.51).
20+
//: ID only requires 10 characters (and can carry 50 bits of entropy).
2221
//:
2322
//: Now let's suppose we need to ensure the names of a handful of items are unique. Let's say 30
2423
//: items. And let's decide we can live with a 1 in 100,000 probability of collision (we're just
@@ -58,7 +57,7 @@ print("String: \(string)\n")
5857
//: our OWASP requirement covered! 😌
5958
//:
6059
//: Also note that we covered our need using strings that are only 22 characters in length. So long
61-
//: to using GUID strings which only carry 122 bits of entropy (for the commonly used version 4
62-
//: anyway) and use string representations (hex and dashes) that are 36 characters in length.
60+
//: to using GUID strings which only carry 122 bits of entropy (commonly used version 4) and use string
61+
//: representations that are 36 characters long (hex with dashes).
6362
//:
6463
//: [TOC](Table%20of%20Contents) | [Next](@next)

EntropyString.playground/Pages/Real Need.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
//:
4545
//: *I need 10,000 random hexadecimal IDs with less than 1 in a million chance of any repeats*.
4646
//:
47+
//: Not only is this statement more specific, there is no mention of string length. The developer needs
48+
//: probabilistic uniqueness, and strings are to be used to capture randomness for this purpose. As such,
49+
//: the length of the string is simply a by-product of the encoding used to represent the required
50+
//: uniqueness as a string.
51+
//:
4752
//: How do you address this need using a library designed to generate strings of specified length?
4853
//: Well, you don't directly, because that library was designed to answer the originally stated need,
4954
//: not the real need we've uncovered. We need a library that deals with probabilistic uniqueness

EntropyString.playground/contents.xcplayground

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='6.0' target-platform='ios' display-mode='rendered'>
2+
<playground version='6.0' target-platform='ios' display-mode='raw'>
33
<pages>
44
<page name='Table of Contents'/>
55
<page name='TLDR'/>

EntropyString.podspec

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = "EntropyString"
5-
s.version = "1.0.0"
5+
s.version = "1.1.0"
66
s.summary = "Efficiently generate cryptographically strong random strings of specified entropy from various character sets."
77

88
s.description = <<-DESC
@@ -12,18 +12,8 @@ Efficiently generate cryptographically strong and secure random strings of speci
1212
s.homepage = "https://github.com/#{s.name}/#{s.name}-Swift"
1313
s.license = { :type => "MIT", :file => "LICENSE" }
1414
s.authors = { "knoxen" => "paul@knoxen.com", "dingo sky" => "paul@dingosky.com" }
15-
# s.social_media_url = "http://twitter.com/knoxen"
15+
s.social_media_url = "http://twitter.com/knoxen"
1616

17-
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
18-
#
19-
# If this Pod runs only on iOS or OS X, then specify the platform and
20-
# the deployment target. You can optionally include the target after the platform.
21-
#
22-
23-
# s.platform = :ios
24-
# s.platform = :ios, "5.0"
25-
26-
# When using multiple platforms
2717
s.ios.deployment_target = "9.0"
2818
# s.osx.deployment_target = "10.7"
2919
# s.watchos.deployment_target = "2.0"

README.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
## EntropyString for Swift
1+
## EntropyString for Swift
22

3-
<p align="center">
4-
<a href="https://github.com/Carthage/Carthage"><img
5-
src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat"
6-
alt="Carthage"></a>
7-
<a href="https://cocoapods.org/pods/EntropyString"><img src="https://img.shields.io/cocoapods/v/EntropyString.svg" alt="CocoaPods - EntropyString"></a>
8-
</p>
3+
[![Build Status](https://travis-ci.org/EntropyString/EntropyString-Swift.svg?branch=master)](https://travis-ci.org/EntropyString/EntropyString-Swift) &nbsp; <a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage"></a> &nbsp; <a href="https://cocoapods.org/pods/EntropyString"><img src="https://img.shields.io/cocoapods/v/EntropyString.svg" alt="CocoaPods - EntropyString"></a>
94

105
EntropyString provides easy creation of randomly generated strings of specific entropy using various character sets.
116

127
## <a name="TOC"></a>
13-
- [Build Status](#BuildStatus)
148
- [Installation](#Installation)
159
- [TL;DR](#TLDR)
1610
- [Overview](#Overview)
@@ -22,9 +16,6 @@ EntropyString provides easy creation of randomly generated strings of specific e
2216
- [Secure Bytes](#SecureBytes)
2317
- [Custom Bytes](#CustomBytes)
2418

25-
## <a name="BuildStatus"></a>Build Status
26-
[![Build Status](https://travis-ci.org/EntropyString/EntropyString-Swift.svg?branch=master)](https://travis-ci.org/EntropyString/EntropyString-Swift)
27-
2819
[TOC](#TOC)
2920

3021
## <a name="Installation"></a>Installation
@@ -129,7 +120,7 @@ The remainer of this README is included in the project as a Swift playground for
129120

130121
```swift
131122
let randomString = RandomString()
132-
try! randomString.use("1234567890ABCDEF", for: .charSet16)
123+
try! randomString.use("0123456789ABCDEF", for: .charSet16)
133124
string = randomString.entropy(of: bits, using: .charSet16)
134125
```
135126

@@ -216,9 +207,9 @@ So now we've gotten to the developer's real need:
216207

217208
*I need 10,000 random hexadecimal IDs with less than 1 in a million chance of any repeats*.
218209

219-
Not only is this statement more specific, note there is no mention of string length. The developer needs probabilistic uniqueness, and strings are to be used to capture randomness for this purpose. As such, the length of the string is simply a by-product of the encoding used to represent the required uniqueness as a string.
210+
Not only is this statement more specific, there is no mention of string length. The developer needs probabilistic uniqueness, and strings are to be used to capture randomness for this purpose. As such, the length of the string is simply a by-product of the encoding used to represent the required uniqueness as a string.
220211

221-
So how do you address this need using a library designed to generate strings of specified length? Well, you don't directly, because that library was designed to answer the originally stated need, not the real need we've uncovered. We need a library that deals with probabilistic uniqueness of a total number of some strings. And that's exactly what `EntropyString` does.
212+
How do you address this need using a library designed to generate strings of specified length? Well, you don't directly, because that library was designed to answer the originally stated need, not the real need we've uncovered. We need a library that deals with probabilistic uniqueness of a total number of some strings. And that's exactly what `EntropyString` does.
222213

223214
Let's use `EntropyString` to help this developer by generating 5 IDs:
224215

@@ -270,7 +261,7 @@ We'll start with using 32 characters. What 32 characters, you ask? Well, the [Ch
270261

271262
> String: PmgMJrdp9h
272263
273-
We're using the same __bits__ calculation since we haven't changed the number of IDs or the accepted risk of probabilistic uniqueness. But this time we use 32 characters and our resulting ID only requires 10 characters (and can carry 50 bits of entropy, which as when we used 16 characters, is more than the required 45.51).
264+
We're using the same __bits__ calculation since we haven't changed the number of IDs or the accepted risk of probabilistic uniqueness. But this time we use 32 characters and our resulting ID only requires 10 characters (and can carry 50 bits of entropy).
274265

275266
Now let's suppose we need to ensure the names of a handful of items are unique. Let's say 30 items. And let's decide we can live with a 1 in 100,000 probability of collision (we're just futzing with some code ideas). Using hex characters:
276267

@@ -314,7 +305,7 @@ Finally, let say we're generating session IDs. We're not interested in uniquenes
314305
315306
Using 64 characters, our string length is 22 characters. That's actually 132 bits, so we've got our OWASP requirement covered! 😌
316307

317-
Also note that we covered our need using strings that are only 22 characters in length. So long to using GUID strings which only carry 122 bits of entropy (for the commonly used version 4 anyway) and use string representations (hex and dashes) that are 36 characters in length.
308+
Also note that we covered our need using strings that are only 22 characters in length. So long to using GUID strings which only carry 122 bits of entropy (commonly used version 4) and use string representations that are 36 characters long (hex with dashes).
318309

319310
[TOC](#TOC)
320311

@@ -388,7 +379,7 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. You want t
388379
389380
Note that setting custom characters in the above code requires using an *instance* of `RandomString`, wheras in the previous sections we used *class* functions for all calls. The function signatures are the same in each case, but you can't change the static character sets used in the class `RandomString` (i.e., there is no `RandomString.use(_,for:)` function).
390381

391-
As another example, we saw in [Character Sets](#CharacterSets) the default characters for CharSet 16 are **01234567890abcdef**. Suppose you like uppercase hexadecimal letters instead.
382+
As another example, we saw in [Character Sets](#CharacterSets) the default characters for CharSet 16 are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
392383

393384
```swift
394385
try! randomString.use("0123456789ABCDEF", for: .charSet16)
@@ -450,7 +441,7 @@ The `EntropyString` scheme is also efficient with regard to the amount of random
450441
}
451442
```
452443

453-
In the code above, `arc4random_uniform` generates 32 bits of randomness per call, returned as an `UInt32`. The returned value is used to create an **index**. Suppose we're creating strings with **len=16** and **charCount=32**. Each **char** consumes 32 bits of randomness (`UInt32`) while only injecting 5 bits (`log2(32)`) of entropy into **string**. The resulting string has an information carrying capacity of 80 bits. So creating each **string** requires a *total* of 512 bits of randomness while only actually *carrying* 80 bits of that entropy forward in the string itself. That means 432 bits (84% of the total) of the generated randomness is simply wasted away.
444+
In the code above, `arc4random_uniform` generates 32 bits of randomness per call, returned as an `UInt32`. The returned value is used to create an **index**. Suppose we're creating strings with **len=16** and **charCount=32**. Each **char** consumes 32 bits of randomness (`UInt32`) while only injecting 5 bits (`log2(32)`) of entropy into **string**. The resulting string has an information carrying capacity of 80 bits. So creating each **string** requires a *total* of 512 bits of randomness while only actually *carrying* 80 bits of that entropy forward in the string itself. That means 432 bits (84% of the total) of the generated randomness is simply wasted.
454445

455446
Compare that to the `EntropyString` scheme. For the example above, slicing off 5 bits at a time requires a total of 80 bits (10 bytes). Creating the same strings as above, `EntropyString` uses 80 bits of randomness per string with no wasted bits. In general, the `EntropyString` scheme can waste up to 7 bits per string, but that's the worst case scenario and that's *per string*, not *per character*!
456447

@@ -464,7 +455,7 @@ As described in [Efficiency](#Efficiency), `EntropyString` uses an underlying ar
464455

465456
`EntropyString` automatically generates the necessary number of bytes needed to create a random string. On Apple OSes, `EntropyString` uses either `SecRandomCopyBytes` or `arc4random_buf`, both of which are cryptographically secure random number generators. `SecRandomCopyBytes` is the stronger of the two, but can fail if the system entropy pool lacks sufficient randomness. Rather than propagate that failure, if `SecRandomCopyBytes` fails `EntropyString` falls back and uses `arc4random_buf` to generate the bytes. Though not as strong, `arc4random_buf` does not fail.
466457

467-
You may, of course, want feedback as to when or if `SecRandomCopyBytes` fails. `RandomString.entropy(of:using:secRand)` provides an additional `inout` parameter that acts as a flag should a `SecRandomCopyBtyes` call fail.
458+
You may, of course, want feedback as to when or if `SecRandomCopyBytes` fails. `RandomString.entropy(of:using:secRand)` provides an additional `inout` parameter that acts as a flag should a `SecRandomCopyBytes` call fail.
468459

469460
On Linux OSes, `EntropyString` always uses `arc4random_buf`. The `secRand` parameter in the `RandomString.entropy(of:using:secRand)` is ignored.
470461

@@ -494,7 +485,7 @@ Rather than have `EntropyString` generate bytes automatically, you can provide y
494485

495486
## <a name="CustomBytes"></a>Custom Bytes
496487

497-
As described in [Secure Bytes](#SecureBytes), `EntropyString` automatically generates random bytes using either `SecRandomCopyBuf` or `arc4random_buf`. These functions are fine, but you may have a need to provide your own btyes, say for deterministic testing or to use a specialized byte genterator. The `RandomString.entropy(of:using:bytes)` function allows passing in your own bytes to create a string.
488+
As described in [Secure Bytes](#SecureBytes), `EntropyString` automatically generates random bytes using either `SecRandomCopyBuf` or `arc4random_buf`. These functions are fine, but you may have a need to provide your own bytes, say for deterministic testing or to use a specialized byte genterator. The `RandomString.entropy(of:using:bytes)` function allows passing in your own bytes to create a string.
498489

499490
Suppose we want a string capable of 30 bits of entropy using 32 characters. We pass in 4 bytes (to cover the 30 bits):
500491

@@ -521,6 +512,6 @@ The __bytes__ provided can come from any source. However, the number of bytes mu
521512

522513
> error: tooFewBytes
523514
524-
Note how the number of bytes needed is dependent on the number of characters in our set. In using a string to represent entropy, we can only generate entropy in multiples of the bits of entropy per character used. So in the example above, to get at least 32 bits of entropy using a character set of 32 characters (5 bits per char), we'll need enough bytes to cover 35 bits, not 32, so a `tooFewBytes` error is thrown.
515+
Note how the number of bytes needed is dependent on the number of characters in our set. In using a string to represent entropy, we can only have multiples of the bits of entropy per character used. So in the example above, to get at least 32 bits of entropy using a character set of 32 characters (5 bits per char), we'll need enough bytes to cover 35 bits, not 32, so a `tooFewBytes` error is thrown.
525516

526517
[TOC](#TOC)

0 commit comments

Comments
 (0)