Selaa lähdekoodia

Add RandomBytesSequence for random bytes

Marcin Krzyżanowski 8 vuotta sitten
vanhempi
commit
d3f8c7234a

+ 8 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -12,6 +12,8 @@
 		750D3ACB1D0EADCA00999299 /* ZeroPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750D3ACA1D0EADCA00999299 /* ZeroPadding.swift */; };
 		750D3ACB1D0EADCA00999299 /* ZeroPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750D3ACA1D0EADCA00999299 /* ZeroPadding.swift */; };
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		753881EB1CB06E390089101D /* NoPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753881EA1CB06E390089101D /* NoPadding.swift */; };
 		753881EB1CB06E390089101D /* NoPadding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753881EA1CB06E390089101D /* NoPadding.swift */; };
+		753B32FF1DAB711200D06422 /* RandomBytesSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753B32FE1DAB711200D06422 /* RandomBytesSequence.swift */; };
+		753B33011DAB84D600D06422 /* RandomBytesSequenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 753B33001DAB84D600D06422 /* RandomBytesSequenceTests.swift */; };
 		75482EA41CB310B7001F66A5 /* PBKDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EA31CB310B7001F66A5 /* PBKDF.swift */; };
 		75482EA41CB310B7001F66A5 /* PBKDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EA31CB310B7001F66A5 /* PBKDF.swift */; };
 		75482EA71CB329C1001F66A5 /* PBKDF2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EA61CB329C1001F66A5 /* PBKDF2.swift */; };
 		75482EA71CB329C1001F66A5 /* PBKDF2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EA61CB329C1001F66A5 /* PBKDF2.swift */; };
 		75482EAC1CB329DF001F66A5 /* PKCS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EAB1CB329DF001F66A5 /* PKCS5.swift */; };
 		75482EAC1CB329DF001F66A5 /* PKCS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75482EAB1CB329DF001F66A5 /* PKCS5.swift */; };
@@ -41,7 +43,6 @@
 		757BC9801C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */; };
 		757BC9801C1CA5790093AAA9 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */; };
 		757BC9841C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
 		757BC9841C1CA5790093AAA9 /* Int+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */; };
 		757BC9881C1CA5790093AAA9 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92D1C1CA5790093AAA9 /* MD5.swift */; };
 		757BC9881C1CA5790093AAA9 /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92D1C1CA5790093AAA9 /* MD5.swift */; };
-		757BC98C1C1CA5790093AAA9 /* Multiplatform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92E1C1CA5790093AAA9 /* Multiplatform.swift */; };
 		757BC9901C1CA5790093AAA9 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92F1C1CA5790093AAA9 /* Operators.swift */; };
 		757BC9901C1CA5790093AAA9 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC92F1C1CA5790093AAA9 /* Operators.swift */; };
 		757BC9941C1CA5790093AAA9 /* Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9301C1CA5790093AAA9 /* Padding.swift */; };
 		757BC9941C1CA5790093AAA9 /* Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9301C1CA5790093AAA9 /* Padding.swift */; };
 		757BC99C1C1CA5790093AAA9 /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9321C1CA5790093AAA9 /* PKCS7.swift */; };
 		757BC99C1C1CA5790093AAA9 /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757BC9321C1CA5790093AAA9 /* PKCS7.swift */; };
@@ -155,6 +156,8 @@
 		750D3ACA1D0EADCA00999299 /* ZeroPadding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ZeroPadding.swift; path = Sources/CryptoSwift/ZeroPadding.swift; sourceTree = SOURCE_ROOT; };
 		750D3ACA1D0EADCA00999299 /* ZeroPadding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ZeroPadding.swift; path = Sources/CryptoSwift/ZeroPadding.swift; sourceTree = SOURCE_ROOT; };
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
 		753881EA1CB06E390089101D /* NoPadding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NoPadding.swift; path = Sources/CryptoSwift/NoPadding.swift; sourceTree = SOURCE_ROOT; };
 		753881EA1CB06E390089101D /* NoPadding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NoPadding.swift; path = Sources/CryptoSwift/NoPadding.swift; sourceTree = SOURCE_ROOT; };
+		753B32FE1DAB711200D06422 /* RandomBytesSequence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RandomBytesSequence.swift; path = Sources/CryptoSwift/RandomBytesSequence.swift; sourceTree = SOURCE_ROOT; };
+		753B33001DAB84D600D06422 /* RandomBytesSequenceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RandomBytesSequenceTests.swift; sourceTree = "<group>"; };
 		75482EA31CB310B7001F66A5 /* PBKDF.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBKDF.swift; sourceTree = "<group>"; };
 		75482EA31CB310B7001F66A5 /* PBKDF.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBKDF.swift; sourceTree = "<group>"; };
 		75482EA61CB329C1001F66A5 /* PBKDF2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PBKDF2.swift; path = Sources/CryptoSwift/PKCS5/PBKDF2.swift; sourceTree = SOURCE_ROOT; };
 		75482EA61CB329C1001F66A5 /* PBKDF2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PBKDF2.swift; path = Sources/CryptoSwift/PKCS5/PBKDF2.swift; sourceTree = SOURCE_ROOT; };
 		75482EAB1CB329DF001F66A5 /* PKCS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PKCS5.swift; path = Sources/CryptoSwift/PKCS5/PKCS5.swift; sourceTree = SOURCE_ROOT; };
 		75482EAB1CB329DF001F66A5 /* PKCS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PKCS5.swift; path = Sources/CryptoSwift/PKCS5/PKCS5.swift; sourceTree = SOURCE_ROOT; };
@@ -190,7 +193,6 @@
 		757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntegerConvertible.swift; path = Sources/CryptoSwift/IntegerConvertible.swift; sourceTree = SOURCE_ROOT; };
 		757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = IntegerConvertible.swift; path = Sources/CryptoSwift/IntegerConvertible.swift; sourceTree = SOURCE_ROOT; };
 		757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Int+Extension.swift"; path = "Sources/CryptoSwift/Int+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		757BC92C1C1CA5790093AAA9 /* Int+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Int+Extension.swift"; path = "Sources/CryptoSwift/Int+Extension.swift"; sourceTree = SOURCE_ROOT; };
 		757BC92D1C1CA5790093AAA9 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MD5.swift; path = Sources/CryptoSwift/MD5.swift; sourceTree = SOURCE_ROOT; };
 		757BC92D1C1CA5790093AAA9 /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MD5.swift; path = Sources/CryptoSwift/MD5.swift; sourceTree = SOURCE_ROOT; };
-		757BC92E1C1CA5790093AAA9 /* Multiplatform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Multiplatform.swift; path = Sources/CryptoSwift/Multiplatform.swift; sourceTree = SOURCE_ROOT; };
 		757BC92F1C1CA5790093AAA9 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Operators.swift; path = Sources/CryptoSwift/Operators.swift; sourceTree = SOURCE_ROOT; };
 		757BC92F1C1CA5790093AAA9 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Operators.swift; path = Sources/CryptoSwift/Operators.swift; sourceTree = SOURCE_ROOT; };
 		757BC9301C1CA5790093AAA9 /* Padding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Padding.swift; path = Sources/CryptoSwift/Padding.swift; sourceTree = SOURCE_ROOT; };
 		757BC9301C1CA5790093AAA9 /* Padding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Padding.swift; path = Sources/CryptoSwift/Padding.swift; sourceTree = SOURCE_ROOT; };
 		757BC9321C1CA5790093AAA9 /* PKCS7.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PKCS7.swift; path = Sources/CryptoSwift/PKCS7.swift; sourceTree = SOURCE_ROOT; };
 		757BC9321C1CA5790093AAA9 /* PKCS7.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PKCS7.swift; path = Sources/CryptoSwift/PKCS7.swift; sourceTree = SOURCE_ROOT; };
@@ -296,6 +298,7 @@
 				80545D121CA9FECD00474A99 /* Bit.swift */,
 				80545D121CA9FECD00474A99 /* Bit.swift */,
 				757F440D1CC1822A002B1F85 /* SecureBytes.swift */,
 				757F440D1CC1822A002B1F85 /* SecureBytes.swift */,
 				757BC91F1C1CA5790093AAA9 /* BytesSequence.swift */,
 				757BC91F1C1CA5790093AAA9 /* BytesSequence.swift */,
+				753B32FE1DAB711200D06422 /* RandomBytesSequence.swift */,
 				755655C61D080E3F00F004E7 /* Cryptors.swift */,
 				755655C61D080E3F00F004E7 /* Cryptors.swift */,
 				75DB81A71CDC06B100ED181A /* Updatable.swift */,
 				75DB81A71CDC06B100ED181A /* Updatable.swift */,
 				75558FCB1D4BA93C00CF6C18 /* RandomAccessCryptor.swift */,
 				75558FCB1D4BA93C00CF6C18 /* RandomAccessCryptor.swift */,
@@ -305,7 +308,6 @@
 				75F818241D9527DB00A78C92 /* Digest */,
 				75F818241D9527DB00A78C92 /* Digest */,
 				757BC9291C1CA5790093AAA9 /* HMAC.swift */,
 				757BC9291C1CA5790093AAA9 /* HMAC.swift */,
 				757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */,
 				757BC92B1C1CA5790093AAA9 /* IntegerConvertible.swift */,
-				757BC92E1C1CA5790093AAA9 /* Multiplatform.swift */,
 				757BC92F1C1CA5790093AAA9 /* Operators.swift */,
 				757BC92F1C1CA5790093AAA9 /* Operators.swift */,
 				757BC9301C1CA5790093AAA9 /* Padding.swift */,
 				757BC9301C1CA5790093AAA9 /* Padding.swift */,
 				75482EA51CB3299E001F66A5 /* PKCS5 */,
 				75482EA51CB3299E001F66A5 /* PKCS5 */,
@@ -351,6 +353,7 @@
 				755FB1D9199E347D00475437 /* ExtensionsTest.swift */,
 				755FB1D9199E347D00475437 /* ExtensionsTest.swift */,
 				757DA2521A4ED0A4002BA3EF /* PaddingTests.swift */,
 				757DA2521A4ED0A4002BA3EF /* PaddingTests.swift */,
 				75482EA31CB310B7001F66A5 /* PBKDF.swift */,
 				75482EA31CB310B7001F66A5 /* PBKDF.swift */,
+				753B33001DAB84D600D06422 /* RandomBytesSequenceTests.swift */,
 				75C2E76C1D55F097003D2BCA /* Access.swift */,
 				75C2E76C1D55F097003D2BCA /* Access.swift */,
 				756BFDCA1A82B87300B9D9A4 /* Bridging.h */,
 				756BFDCA1A82B87300B9D9A4 /* Bridging.h */,
 				754BE46519693E190098E6F3 /* Supporting Files */,
 				754BE46519693E190098E6F3 /* Supporting Files */,
@@ -552,6 +555,7 @@
 				75DB81A31CDBFDC700ED181A /* BlockModeWorker.swift in Sources */,
 				75DB81A31CDBFDC700ED181A /* BlockModeWorker.swift in Sources */,
 				757BC9161C1CA56A0093AAA9 /* Utils+Foundation.swift in Sources */,
 				757BC9161C1CA56A0093AAA9 /* Utils+Foundation.swift in Sources */,
 				7574E5FE1CD02C8B00E96346 /* CSArrayType+Foundation.swift in Sources */,
 				7574E5FE1CD02C8B00E96346 /* CSArrayType+Foundation.swift in Sources */,
+				753B32FF1DAB711200D06422 /* RandomBytesSequence.swift in Sources */,
 				757BC9481C1CA5790093AAA9 /* Authenticator.swift in Sources */,
 				757BC9481C1CA5790093AAA9 /* Authenticator.swift in Sources */,
 				757BC9C01C1CA5790093AAA9 /* Utils.swift in Sources */,
 				757BC9C01C1CA5790093AAA9 /* Utils.swift in Sources */,
 				75CB93491C8F60700087740D /* ECB.swift in Sources */,
 				75CB93491C8F60700087740D /* ECB.swift in Sources */,
@@ -597,7 +601,6 @@
 				75C2E7681D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
 				75C2E7681D55EFA1003D2BCA /* UInt16+Extension.swift in Sources */,
 				757BC9B81C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC9B81C1CA5790093AAA9 /* UInt32+Extension.swift in Sources */,
 				757BC8FE1C1CA56A0093AAA9 /* AES+Foundation.swift in Sources */,
 				757BC8FE1C1CA56A0093AAA9 /* AES+Foundation.swift in Sources */,
-				757BC98C1C1CA5790093AAA9 /* Multiplatform.swift in Sources */,
 				757BC93C1C1CA5790093AAA9 /* AES.swift in Sources */,
 				757BC93C1C1CA5790093AAA9 /* AES.swift in Sources */,
 				757BC9541C1CA5790093AAA9 /* ChaCha20.swift in Sources */,
 				757BC9541C1CA5790093AAA9 /* ChaCha20.swift in Sources */,
 				757BC9941C1CA5790093AAA9 /* Padding.swift in Sources */,
 				757BC9941C1CA5790093AAA9 /* Padding.swift in Sources */,
@@ -618,6 +621,7 @@
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
 				757DA2571A4ED47B002BA3EF /* Helpers.swift in Sources */,
 				757DA2571A4ED47B002BA3EF /* Helpers.swift in Sources */,
+				753B33011DAB84D600D06422 /* RandomBytesSequenceTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* DigestTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* DigestTests.swift in Sources */,
 				E3FD2D531D6B81CE00A9F35F /* Error+Extension.swift in Sources */,
 				E3FD2D531D6B81CE00A9F35F /* Error+Extension.swift in Sources */,
 				757DA2591A4ED4D7002BA3EF /* ChaCha20Tests.swift in Sources */,
 				757DA2591A4ED4D7002BA3EF /* ChaCha20Tests.swift in Sources */,

+ 4 - 4
Sources/CryptoSwift/Cryptors.swift

@@ -24,15 +24,15 @@ public protocol Cryptors: class {
     func makeDecryptor() -> DecryptorType
     func makeDecryptor() -> DecryptorType
 
 
     /// Generate array of random bytes. Helper function.
     /// Generate array of random bytes. Helper function.
-    @available(*, deprecated: 0.6.0, message: "Use system random generator")
     static func randomIV(_ blockSize:Int) -> Array<UInt8>
     static func randomIV(_ blockSize:Int) -> Array<UInt8>
 }
 }
 
 
 extension Cryptors {
 extension Cryptors {
     static public func randomIV(_ blockSize:Int) -> Array<UInt8> {
     static public func randomIV(_ blockSize:Int) -> Array<UInt8> {
-        var randomIV:Array<UInt8> = Array<UInt8>();
-        for _ in 0..<blockSize {
-            randomIV.append(UInt8(truncatingBitPattern: cs_arc4random_uniform(256)));
+        var randomIV:Array<UInt8> = Array<UInt8>()
+        randomIV.reserveCapacity(blockSize)
+        for randomByte in RandomBytesSequence(size: blockSize) {
+            randomIV.append(randomByte)
         }
         }
         return randomIV
         return randomIV
     }
     }

+ 0 - 23
Sources/CryptoSwift/Multiplatform.swift

@@ -1,23 +0,0 @@
-//
-//  Multiplatform.swift
-//  CryptoSwift
-//
-//  Created by Marcin Krzyzanowski on 03/12/15.
-//  Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
-//
-
-#if os(Linux) || os(Android) || os(FreeBSD)
-    import Glibc
-    import SwiftShims
-#else
-    import Darwin
-#endif
-
-@available(*, deprecated: 0.6.1, message: "Please don't use it. Use random generator suitable for the platform.")
-func cs_arc4random_uniform(_ upperBound: UInt32) -> UInt32 {
-    #if os(Linux) || os(Android) || os(FreeBSD)
-        return _swift_stdlib_cxx11_mt19937_uniform(upperBound)
-    #else
-        return arc4random_uniform(upperBound)
-    #endif
-}

+ 43 - 0
Sources/CryptoSwift/RandomBytesSequence.swift

@@ -0,0 +1,43 @@
+//
+//  RandomBytesSequence.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 10/10/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+#if os(Linux) || os(Android) || os(FreeBSD)
+    import Glibc
+#else
+    import Darwin
+#endif
+
+struct RandomBytesSequence: Sequence {
+    let size: Int
+
+    func makeIterator() -> AnyIterator<UInt8> {
+        var count = 0
+        return AnyIterator<UInt8>.init({ () -> UInt8? in
+            if count >= self.size {
+                return nil
+            }
+            count = count + 1
+
+            #if os(Linux) || os(Android) || os(FreeBSD)
+                let fd = open("/dev/urandom", O_RDONLY)
+                if fd <= 0 {
+                    return nil
+                }
+
+                var value: UInt8 = 0
+                let result = read(fd, &value, MemoryLayout<UInt8>.size)
+                precondition(result == 1)
+
+                close(fd)
+                return value
+            #else
+                return UInt8(arc4random_uniform(UInt32(UInt8.max) + 1))
+            #endif
+        })
+    }
+}

+ 7 - 1
Tests/CryptoSwiftTests/Access.swift

@@ -20,6 +20,11 @@ class Access: XCTestCase {
         let _ = Checksum.crc16([1,2,3])
         let _ = Checksum.crc16([1,2,3])
     }
     }
 
 
+    func testRandomIV() {
+        let _ = AES.randomIV(AES.blockSize)
+        let _ = ChaCha20.randomIV(ChaCha20.blockSize)
+    }
+
     func testDigest() {
     func testDigest() {
         let _ = Digest.md5([1,2,3])
         let _ = Digest.md5([1,2,3])
         let _ = Digest.sha1([1,2,3])
         let _ = Digest.sha1([1,2,3])
@@ -248,6 +253,7 @@ class Access: XCTestCase {
         ("testAES", testAES),
         ("testAES", testAES),
         ("testRabbit", testRabbit),
         ("testRabbit", testRabbit),
         ("testChaCha20", testChaCha20),
         ("testChaCha20", testChaCha20),
-        ("testUpdatable", testUpdatable)
+        ("testUpdatable", testUpdatable),
+        ("testRandomIV", testRandomIV)
     ]
     ]
 }
 }

+ 11 - 1
Tests/CryptoSwiftTests/DigestTests.swift

@@ -59,7 +59,16 @@ final class DigestTests: XCTestCase {
         XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
         XCTAssertEqual("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".sha1(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1", "SHA1 calculation failed")
         XCTAssertEqual("".sha1(), "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
         XCTAssertEqual("".sha1(), "da39a3ee5e6b4b0d3255bfef95601890afd80709", "SHA1 calculation failed")
     }
     }
-    
+
+    func testSHA1Performance() {
+//        let _ = "".sha1()
+        self.measure {
+            for _ in 0..<10_000 {
+                let _ = "".sha1()
+            }
+        }
+    }
+
     func testSHA224() {
     func testSHA224() {
         let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
         let data:Data = Data(bytes: UnsafePointer<UInt8>([0x31, 0x32, 0x33] as Array<UInt8>), count: 3)
         XCTAssertEqual(data.sha224().toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
         XCTAssertEqual(data.sha224().toHexString(), "78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f", "SHA224 calculation failed");
@@ -149,6 +158,7 @@ final class DigestTests: XCTestCase {
         ("testMD5Updates", testMD5Updates),
         ("testMD5Updates", testMD5Updates),
         ("testMD5PerformanceSwift", testMD5PerformanceSwift),
         ("testMD5PerformanceSwift", testMD5PerformanceSwift),
         ("testSHA1", testSHA1),
         ("testSHA1", testSHA1),
+        ("testSHA1Performance", testSHA1Performance),
         ("testSHA224", testSHA224),
         ("testSHA224", testSHA224),
         ("testSHA256", testSHA256),
         ("testSHA256", testSHA256),
         ("testSHA384", testSHA384),
         ("testSHA384", testSHA384),

+ 20 - 0
Tests/CryptoSwiftTests/RandomBytesSequenceTests.swift

@@ -0,0 +1,20 @@
+//
+//  RandomBytesSequenceTests.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 10/10/16.
+//  Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
+//
+
+import XCTest
+@testable import CryptoSwift
+
+class RandomBytesSequenceTests: XCTestCase {
+    func testSequence() {
+        XCTAssertNil(RandomBytesSequence(size: 0).makeIterator().next())
+
+        for value in RandomBytesSequence(size: 100) {
+            XCTAssertTrue(value <= UInt8.max)
+        }
+    }
+}

+ 1 - 0
Tests/LinuxMain.swift

@@ -11,5 +11,6 @@ XCTMain([
     testCase(ExtensionsTest.allTests),
     testCase(ExtensionsTest.allTests),
     testCase(PaddingTests.allTests),
     testCase(PaddingTests.allTests),
     testCase(PBKDF.allTests),
     testCase(PBKDF.allTests),
+    testCase(RandomBytesSequenceTests.allTests),
     testCase(Access.allTests)
     testCase(Access.allTests)
 ])
 ])