Browse Source

startIndex in place of [0] index

Marcin Krzyżanowski 10 years ago
parent
commit
28463e1e88
3 changed files with 7 additions and 12 deletions
  1. 0 5
      CryptoSwift/BitExtension.swift
  2. 6 6
      CryptoSwift/CipherBlockMode.swift
  3. 1 1
      CryptoSwift/Generics.swift

+ 0 - 5
CryptoSwift/BitExtension.swift

@@ -7,7 +7,6 @@
 //
 
 extension Bit {
-
     func inverted() -> Bit {
         if (self == Bit.Zero) {
             return Bit.One
@@ -15,8 +14,4 @@ extension Bit {
 
         return Bit.Zero
     }
-
-    mutating func invert()  {
-        self = self.inverted()
-    }
 }

+ 6 - 6
CryptoSwift/CipherBlockMode.swift

@@ -83,7 +83,7 @@ private struct CBCMode: BlockMode {
         }
         
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
         var prevCiphertext = iv // for the first time prevCiphertext = iv
         for plaintext in blocks {
             if let encrypted = cipherOperation(block: xor(prevCiphertext, b: plaintext)) {
@@ -101,7 +101,7 @@ private struct CBCMode: BlockMode {
         }
 
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
         var prevCiphertext = iv // for the first time prevCiphertext = iv
         for ciphertext in blocks {
             if let decrypted = cipherOperation(block: ciphertext) { // decrypt
@@ -126,7 +126,7 @@ private struct CFBMode: BlockMode {
         }
         
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
 
         var lastCiphertext = iv
         for plaintext in blocks {
@@ -152,7 +152,7 @@ private struct ECBMode: BlockMode {
     
     func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8] {
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
         for plaintext in blocks {
             if let encrypted = cipherOperation(block: plaintext) {
                 out.appendContentsOf(encrypted)
@@ -191,7 +191,7 @@ private struct CTRMode: BlockMode {
 
         var counter:UInt = 0
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
         for plaintext in blocks {
             let nonce = buildNonce(iv, counter: counter++)
             if let encrypted = cipherOperation(block: nonce) {
@@ -208,7 +208,7 @@ private struct CTRMode: BlockMode {
         
         var counter:UInt = 0
         var out:[UInt8] = [UInt8]()
-        out.reserveCapacity(blocks.count * blocks[0].count)
+        out.reserveCapacity(blocks.count * blocks[blocks.startIndex].count)
         for plaintext in blocks {
             let nonce = buildNonce(iv, counter: counter++)
             if let encrypted = cipherOperation(block: nonce) {

+ 1 - 1
CryptoSwift/Generics.swift

@@ -44,7 +44,7 @@ func integerWithBytes<T: IntegerType where T:ByteConvertible, T: BitshiftOperati
     }
     
     if sizeof(T) == 1 {
-        return T(truncatingBitPattern: UInt64(bytes[0]))
+        return T(truncatingBitPattern: UInt64(bytes.first!))
     }
     
     var result: T = 0