Make Sodium's initializer non-failable

This commit is contained in:
Zhu Shengqi 2017-09-12 22:35:35 +08:00
parent 789c4d5898
commit bf8ef237f5
6 changed files with 48 additions and 48 deletions

View File

@ -18,7 +18,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) { func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application // Insert code here to initialize your application
let sodium = Sodium()! let sodium = Sodium()
let aliceKeyPair = sodium.box.keyPair()! let aliceKeyPair = sodium.box.keyPair()!
let bobKeyPair = sodium.box.keyPair()! let bobKeyPair = sodium.box.keyPair()!
let message = "My Test Message".toData()! let message = "My Test Message".toData()!

View File

@ -15,7 +15,7 @@ class ViewController: UIViewController {
super.viewDidLoad() super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. // Do any additional setup after loading the view, typically from a nib.
let sodium = Sodium()! let sodium = Sodium()
let aliceKeyPair = sodium.box.keyPair()! let aliceKeyPair = sodium.box.keyPair()!
let bobKeyPair = sodium.box.keyPair()! let bobKeyPair = sodium.box.keyPair()!
let message = "My Test Message".toData()! let message = "My Test Message".toData()!

View File

@ -73,7 +73,7 @@ Anonymous Encryption (Sealed Boxes)
----------------------------------- -----------------------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let bobKeyPair = sodium.box.keyPair()! let bobKeyPair = sodium.box.keyPair()!
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
@ -100,7 +100,7 @@ Detached signatures
------------------- -------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let keyPair = sodium.sign.keyPair()! let keyPair = sodium.sign.keyPair()!
let signature = sodium.sign.signature(message: message, secretKey: keyPair.secretKey)! let signature = sodium.sign.signature(message: message, secretKey: keyPair.secretKey)!
@ -115,7 +115,7 @@ Attached signatures
------------------- -------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let keyPair = sodium.sign.keyPair()! let keyPair = sodium.sign.keyPair()!
let signedMessage = sodium.sign.sign(message: message, secretKey: keyPair.secretKey)! let signedMessage = sodium.sign.sign(message: message, secretKey: keyPair.secretKey)!
@ -128,7 +128,7 @@ Secret-key authenticated encryption
=================================== ===================================
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let secretKey = sodium.secretBox.key()! let secretKey = sodium.secretBox.key()!
let encrypted: Data = sodium.secretBox.seal(message: message, secretKey: secretKey)! let encrypted: Data = sodium.secretBox.seal(message: message, secretKey: secretKey)!
@ -144,7 +144,7 @@ Deterministic hashing
--------------------- ---------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let h = sodium.genericHash.hash(message: message) let h = sodium.genericHash.hash(message: message)
``` ```
@ -153,7 +153,7 @@ Keyed hashing
------------- -------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let key = "Secret key".data(using:.utf8)! let key = "Secret key".data(using:.utf8)!
let h = sodium.genericHash.hash(message: message, key: key) let h = sodium.genericHash.hash(message: message, key: key)
@ -163,7 +163,7 @@ Streaming
--------- ---------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message1 = "My Test ".data(using:.utf8)! let message1 = "My Test ".data(using:.utf8)!
let message2 = "Message".data(using:.utf8)! let message2 = "Message".data(using:.utf8)!
let key = "Secret key".data(using:.utf8)! let key = "Secret key".data(using:.utf8)!
@ -177,7 +177,7 @@ Short-output hashing (SipHash)
============================== ==============================
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let key = sodium.randomBytes.buf(length: sodium.shortHash.KeyBytes)! let key = sodium.randomBytes.buf(length: sodium.shortHash.KeyBytes)!
let h = sodium.shortHash.hash(message: message, key: key) let h = sodium.shortHash.hash(message: message, key: key)
@ -187,7 +187,7 @@ Random numbers generation
========================= =========================
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let randomData = sodium.randomBytes.buf(length: 1000)! let randomData = sodium.randomBytes.buf(length: 1000)!
let seed = "0123456789abcdef0123456789abcdef".data(using:.utf8)! let seed = "0123456789abcdef0123456789abcdef".data(using:.utf8)!
let stream = sodium.randomBytes.deterministic(length: 1000, seed: seed)! let stream = sodium.randomBytes.deterministic(length: 1000, seed: seed)!
@ -199,7 +199,7 @@ Password hashing
Using Argon2i: Using Argon2i:
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let password = "Correct Horse Battery Staple".data(using:.utf8)! let password = "Correct Horse Battery Staple".data(using:.utf8)!
let hashedStr = sodium.pwHash.str(passwd: password, let hashedStr = sodium.pwHash.str(passwd: password,
opsLimit: sodium.pwHash.OpsLimitInteractive, opsLimit: sodium.pwHash.OpsLimitInteractive,
@ -216,7 +216,7 @@ Key exchange
============ ============
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let aliceKeyPair = sodium.keyExchange.keyPair()! let aliceKeyPair = sodium.keyExchange.keyPair()!
let bobKeyPair = sodium.keyExchange.keyPair()! let bobKeyPair = sodium.keyExchange.keyPair()!
@ -242,7 +242,7 @@ computing and verifying a tag. Therefore, verifiers can also compute
tags for arbitrary messages. tags for arbitrary messages.
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let input = "test".data(using:.utf8)! let input = "test".data(using:.utf8)!
let key = sodium.auth.key()! let key = sodium.auth.key()!
let tag = sodium.auth.tag(message: input, secretKey: key)! let tag = sodium.auth.tag(message: input, secretKey: key)!
@ -258,7 +258,7 @@ context. Up to (2^64) - 1 subkeys can be generated for each context,
by incrementing the index. by incrementing the index.
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let secretKey = sodium.keyDerivation.keygen()! let secretKey = sodium.keyDerivation.keygen()!
let subKey1 = sodium.keyDerivation.derive(secretKey: secretKey, let subKey1 = sodium.keyDerivation.derive(secretKey: secretKey,
@ -276,7 +276,7 @@ Zeroing memory
-------------- --------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
var dataToZero = "Message".data(using:.utf8)! var dataToZero = "Message".data(using:.utf8)!
sodium.utils.zero(&dataToZero) sodium.utils.zero(&dataToZero)
``` ```
@ -285,7 +285,7 @@ Constant-time comparison
------------------------ ------------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let secret1 = "Secret key".data(using:.utf8)! let secret1 = "Secret key".data(using:.utf8)!
let secret2 = "Secret key".data(using:.utf8)! let secret2 = "Secret key".data(using:.utf8)!
let equality = sodium.utils.equals(secret1, secret2) let equality = sodium.utils.equals(secret1, secret2)
@ -295,7 +295,7 @@ Constant-time hexadecimal encoding
---------------------------------- ----------------------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let data = "Secret key".data(using:.utf8)! let data = "Secret key".data(using:.utf8)!
let hex = sodium.utils.bin2hex(data) let hex = sodium.utils.bin2hex(data)
``` ```
@ -304,7 +304,7 @@ Hexadecimal decoding
-------------------- --------------------
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let data1 = sodium.utils.hex2bin("deadbeef") let data1 = sodium.utils.hex2bin("deadbeef")
let data2 = sodium.utils.hex2bin("de:ad be:ef", ignore: " :") let data2 = sodium.utils.hex2bin("de:ad be:ef", ignore: " :")
``` ```
@ -334,7 +334,7 @@ In order to generate a deterministic stream out of a seed, the
need. need.
```swift ```swift
let sodium = Sodium()! let sodium = Sodium()
let input = "test".data(using:.utf8)! let input = "test".data(using:.utf8)!
let key = sodium.stream.key()! let key = sodium.stream.key()!
let (output, nonce) = sodium.stream.xor(input: input, secretKey: key)! let (output, nonce) = sodium.stream.xor(input: input, secretKey: key)!

View File

@ -21,15 +21,15 @@ public class Sodium {
public let keyExchange = KeyExchange() public let keyExchange = KeyExchange()
public let auth = Auth() public let auth = Auth()
public let stream = Stream() public let stream = Stream()
public let keyDerivation = KeyDerivation() public let keyDerivation = KeyDerivation()
public init?() { private static let once: Void = {
struct Once { if sodium_init() == -1 {
static var once : () = { fatalError("Failed to initialize libSodium")
if sodium_init() == -1 {
abort()
}
}()
} }
}()
public init() {
_ = Sodium.once
} }
} }

View File

@ -11,7 +11,7 @@ import Sodium
class ReadmeTests : XCTestCase { class ReadmeTests : XCTestCase {
func testAuthenticatedEncryption() { func testAuthenticatedEncryption() {
let sodium = Sodium()! let sodium = Sodium()
let aliceKeyPair = sodium.box.keyPair()! let aliceKeyPair = sodium.box.keyPair()!
let bobKeyPair = sodium.box.keyPair()! let bobKeyPair = sodium.box.keyPair()!
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
@ -30,7 +30,7 @@ class ReadmeTests : XCTestCase {
} }
func testAnonymousEncryptionSealedBoxes() { func testAnonymousEncryptionSealedBoxes() {
let sodium = Sodium()! let sodium = Sodium()
let bobKeyPair = sodium.box.keyPair()! let bobKeyPair = sodium.box.keyPair()!
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
@ -46,7 +46,7 @@ class ReadmeTests : XCTestCase {
} }
func testDetachedSignatures() { func testDetachedSignatures() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let keyPair = sodium.sign.keyPair()! let keyPair = sodium.sign.keyPair()!
let signature = sodium.sign.signature(message: message, secretKey: keyPair.secretKey)! let signature = sodium.sign.signature(message: message, secretKey: keyPair.secretKey)!
@ -58,7 +58,7 @@ class ReadmeTests : XCTestCase {
} }
func testAttachedSignatures() { func testAttachedSignatures() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let keyPair = sodium.sign.keyPair()! let keyPair = sodium.sign.keyPair()!
let signedMessage = sodium.sign.sign(message: message, secretKey: keyPair.secretKey)! let signedMessage = sodium.sign.sign(message: message, secretKey: keyPair.secretKey)!
@ -68,7 +68,7 @@ class ReadmeTests : XCTestCase {
} }
func testSecretKeyAuthenticatedEncryption() { func testSecretKeyAuthenticatedEncryption() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let secretKey = sodium.secretBox.key()! let secretKey = sodium.secretBox.key()!
let encrypted: Data = sodium.secretBox.seal(message: message, secretKey: secretKey)! let encrypted: Data = sodium.secretBox.seal(message: message, secretKey: secretKey)!
@ -78,7 +78,7 @@ class ReadmeTests : XCTestCase {
} }
func testDeterministicHashing() { func testDeterministicHashing() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let h = sodium.genericHash.hash(message: message) let h = sodium.genericHash.hash(message: message)
@ -86,7 +86,7 @@ class ReadmeTests : XCTestCase {
} }
func testKeyedHashing() { func testKeyedHashing() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let key = "Secret key".data(using:.utf8)! let key = "Secret key".data(using:.utf8)!
let h = sodium.genericHash.hash(message: message, key: key) let h = sodium.genericHash.hash(message: message, key: key)
@ -95,7 +95,7 @@ class ReadmeTests : XCTestCase {
} }
func testStreaming() { func testStreaming() {
let sodium = Sodium()! let sodium = Sodium()
let message1 = "My Test ".data(using:.utf8)! let message1 = "My Test ".data(using:.utf8)!
let message2 = "Message".data(using:.utf8)! let message2 = "Message".data(using:.utf8)!
let key = "Secret key".data(using:.utf8)! let key = "Secret key".data(using:.utf8)!
@ -108,7 +108,7 @@ class ReadmeTests : XCTestCase {
} }
func testShortOutputHashing() { func testShortOutputHashing() {
let sodium = Sodium()! let sodium = Sodium()
let message = "My Test Message".data(using:.utf8)! let message = "My Test Message".data(using:.utf8)!
let key = sodium.randomBytes.buf(length: sodium.shortHash.KeyBytes)! let key = sodium.randomBytes.buf(length: sodium.shortHash.KeyBytes)!
let h = sodium.shortHash.hash(message: message, key: key) let h = sodium.shortHash.hash(message: message, key: key)
@ -117,14 +117,14 @@ class ReadmeTests : XCTestCase {
} }
func testRandomNumberGeneration() { func testRandomNumberGeneration() {
let sodium = Sodium()! let sodium = Sodium()
let randomData = sodium.randomBytes.buf(length: 1000) let randomData = sodium.randomBytes.buf(length: 1000)
XCTAssertNotNil(randomData) XCTAssertNotNil(randomData)
} }
func testPasswordHashing() { func testPasswordHashing() {
let sodium = Sodium()! let sodium = Sodium()
let password = "Correct Horse Battery Staple".data(using:.utf8)! let password = "Correct Horse Battery Staple".data(using:.utf8)!
let hashedStr = sodium.pwHash.str(passwd: password, let hashedStr = sodium.pwHash.str(passwd: password,
opsLimit: sodium.pwHash.OpsLimitInteractive, opsLimit: sodium.pwHash.OpsLimitInteractive,
@ -138,13 +138,13 @@ class ReadmeTests : XCTestCase {
} }
func testZeroingMemory() { func testZeroingMemory() {
let sodium = Sodium()! let sodium = Sodium()
var dataToZero = "Message".data(using:.utf8)! var dataToZero = "Message".data(using:.utf8)!
sodium.utils.zero(&dataToZero) sodium.utils.zero(&dataToZero)
} }
func testConstantTimeComparison() { func testConstantTimeComparison() {
let sodium = Sodium()! let sodium = Sodium()
let secret1 = "Secret key".data(using:.utf8)! let secret1 = "Secret key".data(using:.utf8)!
let secret2 = "Secret key".data(using:.utf8)! let secret2 = "Secret key".data(using:.utf8)!
let equality = sodium.utils.equals(secret1, secret2) let equality = sodium.utils.equals(secret1, secret2)
@ -153,7 +153,7 @@ class ReadmeTests : XCTestCase {
} }
func testConstantTimeHexdecimalEncoding() { func testConstantTimeHexdecimalEncoding() {
let sodium = Sodium()! let sodium = Sodium()
let data = "Secret key".data(using:.utf8)! let data = "Secret key".data(using:.utf8)!
let hex = sodium.utils.bin2hex(data) let hex = sodium.utils.bin2hex(data)
@ -161,7 +161,7 @@ class ReadmeTests : XCTestCase {
} }
func testHexDecimalDecoding() { func testHexDecimalDecoding() {
let sodium = Sodium()! let sodium = Sodium()
let data1 = sodium.utils.hex2bin("deadbeef") let data1 = sodium.utils.hex2bin("deadbeef")
let data2 = sodium.utils.hex2bin("de:ad be:ef", ignore: " :") let data2 = sodium.utils.hex2bin("de:ad be:ef", ignore: " :")
@ -170,7 +170,7 @@ class ReadmeTests : XCTestCase {
} }
func testStream() { func testStream() {
let sodium = Sodium()! let sodium = Sodium()
let input = "test".data(using:.utf8)! let input = "test".data(using:.utf8)!
let key = sodium.stream.key()! let key = sodium.stream.key()!
let (output, nonce) = sodium.stream.xor(input: input, secretKey: key)! let (output, nonce) = sodium.stream.xor(input: input, secretKey: key)!
@ -180,7 +180,7 @@ class ReadmeTests : XCTestCase {
} }
func testAuth() { func testAuth() {
let sodium = Sodium()! let sodium = Sodium()
let input = "test".data(using:.utf8)! let input = "test".data(using:.utf8)!
let key = sodium.auth.key()! let key = sodium.auth.key()!
let tag = sodium.auth.tag(message: input, secretKey: key)! let tag = sodium.auth.tag(message: input, secretKey: key)!
@ -190,7 +190,7 @@ class ReadmeTests : XCTestCase {
} }
func testKeyDerivation() { func testKeyDerivation() {
let sodium = Sodium()! let sodium = Sodium()
let secretKey = sodium.keyDerivation.key()! let secretKey = sodium.keyDerivation.key()!
let subKey1 = sodium.keyDerivation.derive(secretKey: secretKey, let subKey1 = sodium.keyDerivation.derive(secretKey: secretKey,

View File

@ -22,7 +22,7 @@ extension Data {
} }
class SodiumTests: XCTestCase { class SodiumTests: XCTestCase {
let sodium = Sodium()! let sodium = Sodium()
override func setUp() { override func setUp() {
super.setUp() super.setUp()