Forráskód Böngészése

Add Array based convenient methods to Updatable and Cipher

Marcin Krzyżanowski 8 éve
szülő
commit
0058adee3a

+ 20 - 0
Sources/CryptoSwift/Updatable.swift

@@ -73,3 +73,23 @@ extension Updatable {
         try self.finish(withBytes: [], output: output)
     }
 }
+
+extension Updatable {
+
+    mutating public func update(withBytes bytes: Array<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
+        return try update(withBytes: bytes.slice, isLast: isLast)
+    }
+
+    mutating public func update(withBytes bytes: Array<UInt8>, isLast: Bool = false, output: (_ bytes: Array<UInt8>) -> Void) throws {
+        return try update(withBytes: bytes.slice, isLast: isLast, output: output)
+    }
+
+    mutating public func finish(withBytes bytes: Array<UInt8>) throws -> Array<UInt8> {
+        return try finish(withBytes: bytes.slice)
+    }
+
+    mutating public func finish(withBytes bytes: Array<UInt8>, output: (_ bytes: Array<UInt8>) -> Void) throws {
+        return try finish(withBytes: bytes.slice, output: output)
+    }
+
+}

+ 2 - 2
Tests/CryptoSwiftTests/Access.swift

@@ -217,8 +217,8 @@ class Access: XCTestCase {
     func testRabbit() {
         do {
             let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
-            let enc = rabbit.encrypt([1, 2, 3])
-            let _ = rabbit.decrypt(enc)
+            let enc = try rabbit.encrypt([1, 2, 3])
+            let _ = try rabbit.decrypt(enc)
 
             XCTAssertThrowsError(try Rabbit(key: "123"))
 

+ 1 - 1
Tests/CryptoSwiftTests/DigestTests.swift

@@ -128,7 +128,7 @@ final class DigestTests: XCTestCase {
             var hash = SHA1()
             let _ = try hash.update(withBytes: [0x31, 0x32])
             let _ = try hash.update(withBytes: [0x33])
-            let _ = try hash.update(withBytes: Array<UInt8>.init(repeating: 0x33, count: 64))
+            let _ = try hash.update(withBytes: Array<UInt8>(repeating: 0x33, count: 64))
             XCTAssertEqual(try hash.finish().toHexString(), "0e659367eff83a6b868a35b96ac305b270025e86", "Failed")
         } catch {
             XCTFail()

+ 4 - 4
Tests/CryptoSwiftTests/RabbitTests.swift

@@ -64,9 +64,9 @@ class RabbitTests: XCTestCase {
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (key, expectedCipher) in cases {
             let rabbit = try! Rabbit(key: key)
-            let cipherText = rabbit.encrypt(plainText)
+            let cipherText = try! rabbit.encrypt(plainText)
             XCTAssertEqual(cipherText, expectedCipher)
-            XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
+            XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
         }
     }
 
@@ -103,9 +103,9 @@ class RabbitTests: XCTestCase {
         let plainText = Array<UInt8>(repeating: 0, count: 48)
         for (iv, expectedCipher) in cases {
             let rabbit = try! Rabbit(key: key, iv: iv)
-            let cipherText = rabbit.encrypt(plainText)
+            let cipherText = try! rabbit.encrypt(plainText)
             XCTAssertEqual(cipherText, expectedCipher)
-            XCTAssertEqual(rabbit.decrypt(cipherText), plainText)
+            XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
         }
     }
 }