Marcin Krzyżanowski 8 years ago
parent
commit
a6d3f38fdc

+ 2 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -172,6 +172,7 @@
 		755655C61D080E3F00F004E7 /* Cryptors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Cryptors.swift; path = Sources/CryptoSwift/Cryptors.swift; sourceTree = SOURCE_ROOT; };
 		755D27BC1D06DE6400C41692 /* CryptoSwift.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = CryptoSwift.playground; sourceTree = "<group>"; };
 		755FB1D9199E347D00475437 /* ExtensionsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionsTest.swift; sourceTree = "<group>"; };
+		75668C801E2191FF001186BA /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
 		756B575E1D5131A4001C0C72 /* Collection+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Collection+Extension.swift"; path = "Sources/CryptoSwift/Collection+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		756BFDCA1A82B87300B9D9A4 /* Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bridging.h; sourceTree = "<group>"; };
 		7574E5FD1CD02C8B00E96346 /* CSArrayType+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSArrayType+Foundation.swift"; sourceTree = "<group>"; };
@@ -337,6 +338,7 @@
 		754BE45819693E190098E6F3 /* Supporting Files */ = {
 			isa = PBXGroup;
 			children = (
+				75668C801E2191FF001186BA /* README.md */,
 				54EE1C621C2199B2008DD2E5 /* CryptoSwift.h */,
 				757BC92A1C1CA5790093AAA9 /* Info.plist */,
 			);

+ 7 - 9
README.md

@@ -189,7 +189,7 @@ let hex   = bytes.toHexString()            // "010203"
 
 Build bytes out of `String`
 ```swift
-let bytes = "string".utf8.map({$0})
+let bytes = Array("string".utf8)
 ```
 
 Also... check out helpers that work with **Base64** encoded data:
@@ -257,10 +257,8 @@ try HMAC(key: key, variant: .sha256).authenticate(bytes)
 #####Password-Based Key Derivation Functions
 
 ```swift
-let password: Array<UInt8> = "s33krit".utf8.map {$0}
-let salt: Array<UInt8> = "nacllcan".utf8.map {$0}
-
-try PKCS5.PBKDF1(password: password, salt: salt, variant: .sha1, iterations: 4096).calculate()
+let password: Array<UInt8> = Array("s33krit".utf8)
+let salt: Array<UInt8> = Array("nacllcan".utf8)
 
 try PKCS5.PBKDF2(password: password, salt: salt, iterations: 4096, variant: .sha256).calculate()
 ```
@@ -313,7 +311,7 @@ try AES(key: [1,2,3,...,32], iv: [1,2,3,...,16], blockMode: .CBC, padding: PKCS7
 ```swift
 do {
     let aes = try AES(key: "passwordpassword", iv: "drowssapdrowssap") // aes128
-    let ciphertext = try aes.encrypt("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8.map({$0}))
+    let ciphertext = try aes.encrypt(Array("Nullam quis risus eget urna mollis ornare vel eu leo.".utf8))
 } catch { }
 ```
 
@@ -327,9 +325,9 @@ do {
 
     var ciphertext = Array<UInt8>()
     // aggregate partial results
-    ciphertext += try encryptor.update(withBytes: "Nullam quis risus ".utf8.map({$0}))
-    ciphertext += try encryptor.update(withBytes: "eget urna mollis ".utf8.map({$0}))
-    ciphertext += try encryptor.update(withBytes: "ornare vel eu leo.".utf8.map({$0}))
+    ciphertext += try encryptor.update(withBytes: Array("Nullam quis risus ".utf8))
+    ciphertext += try encryptor.update(withBytes: Array("eget urna mollis ".utf8))
+    ciphertext += try encryptor.update(withBytes: Array("ornare vel eu leo.".utf8))
     // finish at the end
     ciphertext += try encryptor.finish()
 

+ 1 - 15
Sources/CryptoSwift/ChaCha20.swift

@@ -206,7 +206,7 @@ public final class ChaCha20: BlockCipher {
 
         var block = Array<UInt8>(repeating: 0, count: ChaCha20.blockSize)
         var bytes = bytes //TODO: check bytes[bytes.indices]
-        var out = Array<UInt8>.init(reserveCapacity: bytes.count)
+        var out = Array<UInt8>(reserveCapacity: bytes.count)
 
         while bytes.count >= ChaCha20.blockSize {
             self.core(block: &block, counter: counter, key: key)
@@ -327,17 +327,3 @@ extension ChaCha20: Cryptors {
         return Decryptor(chacha: self)
     }
 }
-
-// MARK: Helpers
-
-/// Change array to number. It's here because arrayOfBytes is too slow
-//TODO: check if it should replace arrayOfBytes
-//private func wordNumber<T: Collection>(_ bytes: T) -> UInt32 where T.Iterator.Element == UInt8, T.IndexDistance == Int {
-//    var value: UInt32 = 0
-//    for i: UInt32 in 0 ..< 4 {
-//        let j = bytes.index(bytes.startIndex, offsetBy: Int(i))
-//        value = value | UInt32(bytes[j]) << (8 * i)
-//    }
-//
-//    return value
-//}

+ 4 - 4
Tests/CryptoSwiftTests/AESTests.swift

@@ -97,12 +97,12 @@ final class AESTests: XCTestCase {
         do {
             var ciphertext = Array<UInt8>()
             let plaintext = "Today Apple launched the open source Swift community, as well as amazing new tools and resources."
-            let aes = try AES(key: "passwordpassword".utf8.map({ $0 }), iv: "drowssapdrowssap".utf8.map({ $0 }))
+            let aes = try AES(key: Array("passwordpassword".utf8), iv: Array("drowssapdrowssap".utf8))
             var encryptor = aes.makeEncryptor()
 
-            ciphertext += try encryptor.update(withBytes: plaintext.utf8.map({ $0 }))
+            ciphertext += try encryptor.update(withBytes: Array(plaintext.utf8))
             ciphertext += try encryptor.finish()
-            XCTAssertEqual(try aes.encrypt(plaintext.utf8.map({ $0 })), ciphertext, "encryption failed")
+            XCTAssertEqual(try aes.encrypt(Array(plaintext.utf8)), ciphertext, "encryption failed")
         } catch {
             XCTAssert(false, "\(error)")
         }
@@ -219,7 +219,7 @@ final class AESTests: XCTestCase {
         var plaintext: Array<UInt8> = Array<UInt8>(repeating: 0, count: 6000)
 
         for i in 0 ..< plaintext.count / 6 {
-            let s = String(format: "%05d", i).utf8.map { $0 }
+            let s = Array(String(format: "%05d", i).utf8)
             plaintext[i * 6 + 0] = s[0]
             plaintext[i * 6 + 1] = s[1]
             plaintext[i * 6 + 2] = s[2]

+ 2 - 2
Tests/CryptoSwiftTests/PBKDF.swift

@@ -46,8 +46,8 @@ class PBKDF: XCTestCase {
     }
 
     func testPBKDF2Length() {
-        let password: Array<UInt8> = "s33krit".utf8.map { $0 }
-        let salt: Array<UInt8> = "nacl".utf8.map { $0 }
+        let password: Array<UInt8> = Array("s33krit".utf8)
+        let salt: Array<UInt8> = Array("nacl".utf8)
         let value = try! PKCS5.PBKDF2(password: password, salt: salt, iterations: 2, keyLength: 8, variant: .sha1).calculate()
         XCTAssert(value.toHexString() == "a53cf3df485e5cd9", "PBKDF2 fail")
     }