瀏覽代碼

Update bit shifting API to Swift 3

Marcin Krzyżanowski 9 年之前
父節點
當前提交
7617c2aef1

+ 3 - 3
CryptoSwiftTests/AESTests.swift

@@ -236,12 +236,12 @@ final class AESTests: XCTestCase {
         let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
         
         measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let keyData     = NSData.withBytes(bytes: key)
+            let keyData     = NSData.with(bytes: key)
             let keyBytes    = UnsafePointer<Void>(keyData.bytes)
-            let ivData      = NSData.withBytes(bytes: iv)
+            let ivData      = NSData.with(bytes: iv)
             let ivBytes     = UnsafePointer<Void>(ivData.bytes)
             
-            let data = NSData.withBytes(bytes: message)
+            let data = NSData.with(bytes: message)
             let dataLength    = data.length
             let dataBytes     = UnsafePointer<Void>(data.bytes)
             

+ 1 - 1
CryptoSwiftTests/ChaCha20Tests.swift

@@ -56,7 +56,7 @@ final class ChaCha20Tests: XCTestCase {
                 let messageData = NSData(bytes: message, length: message.count);
                 let encrypted2 = try! messageData.encrypt(cipher: ChaCha20(key: keys[idx], iv: ivs[idx])!)
                 XCTAssertNotNil(encrypted2, "")
-                XCTAssertEqual(NSData.withBytes(bytes: encrypted), encrypted2, "ChaCha20 extension failed")
+                XCTAssertEqual(NSData.with(bytes: encrypted), encrypted2, "ChaCha20 extension failed")
             } catch CipherError.Encrypt {
                 XCTAssert(false, "Encryption failed")
             } catch CipherError.Decrypt {

+ 2 - 2
CryptoSwiftTests/ExtensionsTest.swift

@@ -31,13 +31,13 @@ final class ExtensionsTest: XCTestCase {
     func testIntExtension() {
         let i1:Int = 1024
         let i1Array = i1.bytes(totalBytes: 32 / 8) // 32 bit
-        let i1recovered = Int.withBytes(bytes: i1Array)
+        let i1recovered = Int.with(bytes: i1Array)
         
         XCTAssertEqual(i1, i1recovered, "Bytes conversion failed")
         
         let i2:Int = 1024
         let i2Array = i2.bytes(totalBytes: 160 / 8) // 160 bit
-        let i2recovered = Int.withBytes(bytes: i2Array)
+        let i2recovered = Int.with(bytes: i2Array)
         
         XCTAssertEqual(i2, i2recovered, "Bytes conversion failed")
     }

+ 2 - 2
CryptoSwiftTests/Poly1305Tests.swift

@@ -28,8 +28,8 @@ final class Poly1305Tests: XCTestCase {
         XCTAssertEqual(mac, expectedMac, "Invalid authentication result")
         
         // extensions
-        let msgData = NSData.withBytes(bytes: msg)
+        let msgData = NSData.with(bytes: msg)
         let mac2 = try! msgData.authenticate(authenticator: Authenticator.Poly1305(key: key))
-        XCTAssertEqual(mac2, NSData.withBytes(bytes: expectedMac), "Invalid authentication result")
+        XCTAssertEqual(mac2, NSData.with(bytes: expectedMac), "Invalid authentication result")
     }
 }

+ 2 - 2
Sources/CryptoSwift/AES.swift

@@ -286,7 +286,7 @@ extension AES {
             var arr = Array<UInt32>()
             for idx in stride(from: expanded.startIndex, to: expanded.endIndex, by: 4) {
                 let four = Array(expanded[idx..<idx.advanced(by: 4)].reversed())
-                let num = UInt32.withBytes(bytes: four)
+                let num = UInt32.with(bytes: four)
                 arr.append(num)
             }
 
@@ -326,7 +326,7 @@ extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(word: rotateLeft(v: UInt32.withBytes(bytes: tmp), 8).bytes(totalBytes: sizeof(UInt32)))
+                tmp = subWord(word: rotateLeft(v: UInt32.with(bytes: tmp), 8).bytes(totalBytes: sizeof(UInt32)))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(word: tmp)

+ 1 - 1
Sources/CryptoSwift/BlockMode/CTR.swift

@@ -46,6 +46,6 @@ private func buildNonce(iv: Array<UInt8>, counter: UInt64) -> Array<UInt8> {
     let noncePartLen = AES.blockSize / 2
     let noncePrefix = Array(iv[0..<noncePartLen])
     let nonceSuffix = Array(iv[noncePartLen..<iv.count])
-    let c = UInt64.withBytes(bytes: nonceSuffix) + counter
+    let c = UInt64.with(bytes: nonceSuffix) + counter
     return noncePrefix + arrayOfBytes(value: c)
 }

+ 13 - 13
Sources/CryptoSwift/Foundation/NSData+Extension.swift

@@ -32,57 +32,57 @@ extension NSData {
     
     @nonobjc public func md5() -> NSData {
         let result = Hash.md5(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func sha1() -> NSData? {
         let result = Hash.sha1(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func sha224() -> NSData? {
         let result = Hash.sha224(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func sha256() -> NSData? {
         let result = Hash.sha256(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func sha384() -> NSData? {
         let result = Hash.sha384(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func sha512() -> NSData? {
         let result = Hash.sha512(self.arrayOfBytes()).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func crc32(seed: UInt32? = nil, reflect : Bool = true) -> NSData? {
         let result = Hash.crc32(self.arrayOfBytes(), seed: seed, reflect: reflect).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func crc16(seed: UInt16? = nil) -> NSData? {
         let result = Hash.crc16(self.arrayOfBytes(), seed: seed).calculate()
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 
     public func encrypt(cipher: Cipher) throws -> NSData {
         let encrypted = try cipher.encrypt(bytes: self.arrayOfBytes())
-        return NSData.withBytes(bytes: encrypted)
+        return NSData.with(bytes: encrypted)
     }
 
     public func decrypt(cipher: Cipher) throws -> NSData {
         let decrypted = try cipher.decrypt(bytes: self.arrayOfBytes())
-        return NSData.withBytes(bytes: decrypted)
+        return NSData.with(bytes: decrypted)
     }
     
     public func authenticate(authenticator: Authenticator) throws -> NSData {
         let result = try authenticator.authenticate(message: self.arrayOfBytes())
-        return NSData.withBytes(bytes: result)
+        return NSData.with(bytes: result)
     }
 }
 
@@ -100,10 +100,10 @@ extension NSData {
     }
 
     public convenience init(bytes: Array<UInt8>) {
-        self.init(data: NSData.withBytes(bytes: bytes))
+        self.init(data: NSData.with(bytes: bytes))
     }
     
-    class public func withBytes(bytes: Array<UInt8>) -> NSData {
+    class public func with(bytes: Array<UInt8>) -> NSData {
         return NSData(bytes: bytes, length: bytes.count)
     }
 }

+ 22 - 22
Sources/CryptoSwift/Generics.swift

@@ -6,7 +6,7 @@
 //  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
 //
 
-/** Protocol and extensions for integerFromBitsArray. Bit hakish for me, but I can't do it in any other way */
+/** Protocol and extensions for integerFrom(bits:). Bit hakish for me, but I can't do it in any other way */
 protocol Initiable  {
     init(_ v: Int)
     init(_ v: UInt)
@@ -20,7 +20,7 @@ extension UInt32:Initiable {}
 extension UInt64:Initiable {}
 
 /** build bit pattern from array of bits */
-func integerFromBitsArray<T: UnsignedInteger>(bits: [Bit]) -> T
+func integerFrom<T: UnsignedInteger>(bits: [Bit]) -> T
 {
     var bitPattern:T = 0
     for (idx,b) in bits.enumerated() {
@@ -34,7 +34,7 @@ func integerFromBitsArray<T: UnsignedInteger>(bits: [Bit]) -> T
 
 /// Initialize integer from array of bytes.
 /// This method may be slow
-func integerWithBytes<T:Integer where T:ByteConvertible, T: BitshiftOperationsType>(bytes: Array<UInt8>) -> T {
+func integerWith<T:Integer where T:ByteConvertible, T: BitshiftOperationsType>(bytes: Array<UInt8>) -> T {
     var bytes = bytes.reversed() as Array<UInt8> //FIXME: check it this is equivalent of Array(...)
     if bytes.count < sizeof(T) {
         let paddingCount = sizeof(T) - bytes.count
@@ -91,7 +91,7 @@ func << <T:UnsignedInteger>(lhs: T, rhs: Int) -> UInt {
 
 // Generic function itself
 // FIXME: this generic function is not as generic as I would. It crashes for smaller types
-func shiftLeft<T: SignedInteger where T: Initiable>(value: T, count: Int) -> T {
+func shiftLeft<T: SignedInteger where T: Initiable>(value: T, by count: Int) -> T {
     if (value == 0) {
         return 0;
     }
@@ -115,39 +115,39 @@ func shiftLeft<T: SignedInteger where T: Initiable>(value: T, count: Int) -> T {
 }
 
 // for any f*** other Integer type - this part is so non-Generic
-func shiftLeft(value: UInt, count: Int) -> UInt {
-    return UInt(shiftLeft(value: Int(value), count: count)) //FIXME: count:
+func shiftLeft(value: UInt, by count: Int) -> UInt {
+    return UInt(shiftLeft(value: Int(value), by: count))
 }
 
-func shiftLeft(value: UInt8, count: Int) -> UInt8 {
-    return UInt8(shiftLeft(value: UInt(value), count: count))
+func shiftLeft(value: UInt8, by count: Int) -> UInt8 {
+    return UInt8(shiftLeft(value: UInt(value), by: count))
 }
 
-func shiftLeft(value: UInt16, count: Int) -> UInt16 {
-    return UInt16(shiftLeft(value: UInt(value), count: count))
+func shiftLeft(value: UInt16, by count: Int) -> UInt16 {
+    return UInt16(shiftLeft(value: UInt(value), by: count))
 }
 
-func shiftLeft(value: UInt32, count: Int) -> UInt32 {
-    return UInt32(shiftLeft(value: UInt(value), count: count))
+func shiftLeft(value: UInt32, by count: Int) -> UInt32 {
+    return UInt32(shiftLeft(value: UInt(value), by: count))
 }
 
-func shiftLeft(value: UInt64, count: Int) -> UInt64 {
-    return UInt64(shiftLeft(value: UInt(value), count: count))
+func shiftLeft(value: UInt64, by count: Int) -> UInt64 {
+    return UInt64(shiftLeft(value: UInt(value), by: count))
 }
 
-func shiftLeft(value: Int8, count: Int) -> Int8 {
-    return Int8(shiftLeft(value: Int(value), count: count))
+func shiftLeft(value: Int8, by count: Int) -> Int8 {
+    return Int8(shiftLeft(value: Int(value), by: count))
 }
 
-func shiftLeft(value: Int16, count: Int) -> Int16 {
-    return Int16(shiftLeft(value: Int(value), count: count))
+func shiftLeft(value: Int16, by count: Int) -> Int16 {
+    return Int16(shiftLeft(value: Int(value), by: count))
 }
 
-func shiftLeft(value: Int32, count: Int) -> Int32 {
-    return Int32(shiftLeft(value: Int(value), count: count))
+func shiftLeft(value: Int32, by count: Int) -> Int32 {
+    return Int32(shiftLeft(value: Int(value), by: count))
 }
 
-func shiftLeft(value: Int64, count: Int) -> Int64 {
-    return Int64(shiftLeft(value: Int(value), count: count))
+func shiftLeft(value: Int64, by count: Int) -> Int64 {
+    return Int64(shiftLeft(value: Int(value), by: count))
 }
 

+ 6 - 6
Sources/CryptoSwift/IntExtension.swift

@@ -24,7 +24,7 @@
 /* array of bits */
 extension Int {
     init(bits: [Bit]) {
-        self.init(bitPattern: integerFromBitsArray(bits: bits) as UInt)
+        self.init(bitPattern: integerFrom(bits: bits) as UInt)
     }
 }
 
@@ -35,13 +35,13 @@ extension Int {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: ArraySlice<UInt8>) -> Int {
-        return Int.withBytes(bytes: Array(bytes))
+    public static func with(bytes: ArraySlice<UInt8>) -> Int {
+        return Int.with(bytes: Array(bytes))
     }
 
     /** Int with array bytes (little-endian) */
-    public static func withBytes(bytes: Array<UInt8>) -> Int {
-        return integerWithBytes(bytes: bytes)
+    public static func with(bytes: Array<UInt8>) -> Int {
+        return integerWith(bytes: bytes)
     }
 }
 
@@ -52,7 +52,7 @@ extension Int {
     
     /** Shift bits to the left. All bits are shifted (including sign bit) */
     private mutating func shiftLeft(by count: Int) {
-        self = CryptoSwift.shiftLeft(value: self, count: count) //FIXME: count:
+        self = CryptoSwift.shiftLeft(value: self, by: count) //FIXME: count:
     }
     
     /** Shift bits to the right. All bits are shifted (including sign bit) */

+ 4 - 4
Sources/CryptoSwift/Rabbit.swift

@@ -100,10 +100,10 @@ final public class Rabbit: BlockCipher {
     private func setupIV(iv: Array<UInt8>) {
         // 63...56 55...48 47...40 39...32 31...24 23...16 15...8 7...0 IV bits
         //    0       1       2       3       4       5       6     7   IV bytes in array
-        let iv0: UInt32 = integerWithBytes(bytes: [iv[4], iv[5], iv[6], iv[7]])
-        let iv1: UInt32 = integerWithBytes(bytes: [iv[0], iv[1], iv[4], iv[5]])
-        let iv2: UInt32 = integerWithBytes(bytes: [iv[0], iv[1], iv[2], iv[3]])
-        let iv3: UInt32 = integerWithBytes(bytes: [iv[2], iv[3], iv[6], iv[7]])
+        let iv0: UInt32 = integerWith(bytes: [iv[4], iv[5], iv[6], iv[7]])
+        let iv1: UInt32 = integerWith(bytes: [iv[0], iv[1], iv[4], iv[5]])
+        let iv2: UInt32 = integerWith(bytes: [iv[0], iv[1], iv[2], iv[3]])
+        let iv3: UInt32 = integerWith(bytes: [iv[2], iv[3], iv[6], iv[7]])
         
         // Modify the counter state as function of the IV
         c[0] = c[0] ^ iv0

+ 4 - 4
Sources/CryptoSwift/UInt32Extension.swift

@@ -22,13 +22,13 @@ extension UInt32 {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: ArraySlice<UInt8>) -> UInt32 {
-        return UInt32.withBytes(bytes: Array(bytes))
+    public static func with(bytes: ArraySlice<UInt8>) -> UInt32 {
+        return UInt32.with(bytes: Array(bytes))
     }
 
     /** Int with array bytes (little-endian) */
-    public static func withBytes(bytes: Array<UInt8>) -> UInt32 {
-        return integerWithBytes(bytes: bytes)
+    public static func with(bytes: Array<UInt8>) -> UInt32 {
+        return integerWith(bytes: bytes)
     }
 }
 

+ 4 - 4
Sources/CryptoSwift/UInt64Extension.swift

@@ -12,12 +12,12 @@ extension UInt64 {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
-    public static func withBytes(bytes: ArraySlice<UInt8>) -> UInt64 {
-        return UInt64.withBytes(bytes: Array(bytes))
+    public static func with(bytes: ArraySlice<UInt8>) -> UInt64 {
+        return UInt64.with(bytes: Array(bytes))
     }
 
     /** Int with array bytes (little-endian) */
-    public static func withBytes(bytes: Array<UInt8>) -> UInt64 {
-        return integerWithBytes(bytes: bytes)
+    public static func with(bytes: Array<UInt8>) -> UInt64 {
+        return integerWith(bytes: bytes)
     }
 }

+ 1 - 1
Sources/CryptoSwift/UInt8Extension.swift

@@ -48,7 +48,7 @@ extension UInt8 {
 extension UInt8 {
 
     init(bits: [Bit]) {
-        self.init(integerFromBitsArray(bits: bits) as UInt8)
+        self.init(integerFrom(bits: bits) as UInt8)
     }
     
     /** array of bits */