Selaa lähdekoodia

Replace RandomBytesSequence with Swift.RandomNumberGenerator

Marcin Krzyzanowski 5 vuotta sitten
vanhempi
commit
8556175efc

+ 0 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -103,7 +103,6 @@
 		75EC52AA1EE8B83D0048EB3B /* Poly1305.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526A1EE8B6CA0048EB3B /* Poly1305.swift */; };
 		75EC52AB1EE8B83D0048EB3B /* Rabbit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526B1EE8B6CA0048EB3B /* Rabbit.swift */; };
 		75EC52AC1EE8B83D0048EB3B /* Cryptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526C1EE8B6CA0048EB3B /* Cryptor.swift */; };
-		75EC52AD1EE8B83D0048EB3B /* RandomBytesSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526D1EE8B6CA0048EB3B /* RandomBytesSequence.swift */; };
 		75EC52AE1EE8B83D0048EB3B /* SecureBytes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526E1EE8B6CA0048EB3B /* SecureBytes.swift */; };
 		75EC52AF1EE8B83D0048EB3B /* SHA1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC526F1EE8B6CA0048EB3B /* SHA1.swift */; };
 		75EC52B01EE8B83D0048EB3B /* SHA2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75EC52701EE8B6CA0048EB3B /* SHA2.swift */; };
@@ -348,7 +347,6 @@
 		75EC526A1EE8B6CA0048EB3B /* Poly1305.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Poly1305.swift; sourceTree = "<group>"; };
 		75EC526B1EE8B6CA0048EB3B /* Rabbit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Rabbit.swift; sourceTree = "<group>"; };
 		75EC526C1EE8B6CA0048EB3B /* Cryptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cryptor.swift; sourceTree = "<group>"; };
-		75EC526D1EE8B6CA0048EB3B /* RandomBytesSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RandomBytesSequence.swift; sourceTree = "<group>"; };
 		75EC526E1EE8B6CA0048EB3B /* SecureBytes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureBytes.swift; sourceTree = "<group>"; };
 		75EC526F1EE8B6CA0048EB3B /* SHA1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SHA1.swift; sourceTree = "<group>"; };
 		75EC52701EE8B6CA0048EB3B /* SHA2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SHA2.swift; sourceTree = "<group>"; };
@@ -594,7 +592,6 @@
 				75EC526A1EE8B6CA0048EB3B /* Poly1305.swift */,
 				75EC526B1EE8B6CA0048EB3B /* Rabbit.swift */,
 				75EC526C1EE8B6CA0048EB3B /* Cryptor.swift */,
-				75EC526D1EE8B6CA0048EB3B /* RandomBytesSequence.swift */,
 				75EC526E1EE8B6CA0048EB3B /* SecureBytes.swift */,
 				81F279DC2181F58300449EDA /* Scrypt.swift */,
 				75EC526F1EE8B6CA0048EB3B /* SHA1.swift */,
@@ -965,7 +962,6 @@
 				75EC52A31EE8B8290048EB3B /* NoPadding.swift in Sources */,
 				81F279DD2181F58300449EDA /* Scrypt.swift in Sources */,
 				75EC52931EE8B81A0048EB3B /* Digest.swift in Sources */,
-				75EC52AD1EE8B83D0048EB3B /* RandomBytesSequence.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 5 - 7
Sources/CryptoSwift/Cryptors.swift

@@ -33,12 +33,10 @@ public protocol Cryptors: class {
 }
 
 extension Cryptors {
-    public static func randomIV(_ blockSize: Int) -> Array<UInt8> {
-        var randomIV: Array<UInt8> = Array<UInt8>()
-        randomIV.reserveCapacity(blockSize)
-        for randomByte in RandomBytesSequence(size: blockSize) {
-            randomIV.append(randomByte)
-        }
-        return randomIV
+  /// Generate array of random values.
+  /// Convenience helper that uses `Swift.RandomNumberGenerator`.
+  /// - Parameter count: Length of array
+    public static func randomIV(_ count: Int) -> Array<UInt8> {
+      (0..<count).map({ _ in .random(in: 0...UInt8.max) })
     }
 }

+ 0 - 50
Sources/CryptoSwift/RandomBytesSequence.swift

@@ -1,50 +0,0 @@
-//
-//  CryptoSwift
-//
-//  Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
-//  This software is provided 'as-is', without any express or implied warranty.
-//
-//  In no event will the authors be held liable for any damages arising from the use of this software.
-//
-//  Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-//
-//  - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
-//  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-//  - This notice may not be removed or altered from any source or binary distribution.
-//
-
-#if canImport(Darwin)
-    import Darwin
-#else
-    import Glibc
-#endif
-
-struct RandomBytesSequence: Sequence {
-    let size: Int
-
-    func makeIterator() -> AnyIterator<UInt8> {
-        var count = 0
-        return AnyIterator<UInt8>.init { () -> UInt8? in
-            guard count < self.size else {
-                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
-        }
-    }
-}