Pārlūkot izejas kodu

update readme and stuff

Marcin Krzyżanowski 11 gadi atpakaļ
vecāks
revīzija
1b9469cd36

+ 7 - 3
CryptoSwift/ChaCha20.swift

@@ -8,7 +8,7 @@
 
 import Foundation
 
-internal class ChaCha20 {
+class ChaCha20 {
     private let stateSize = 16
     private let blockSize = 16 * 4
     private var context:Context = Context()
@@ -17,7 +17,7 @@ internal class ChaCha20 {
         var input:[UInt32] = [UInt32](count: 16, repeatedValue: 0)
     }
     
-    internal init(key:NSData, iv:NSData) {
+    init(key:NSData, iv:NSData) {
         context = keySetup(iv: iv, key: key)
     }
 
@@ -28,11 +28,15 @@ internal class ChaCha20 {
         }
     }
     
-    internal func encrypt(message:NSData) -> NSData {
+    func encrypt(message:NSData) -> NSData {
         let output = encryptBytes(message.arrayOfBytes())
         return NSData(bytes: output, length: output.count)
     }
     
+    func decrypt(message:NSData) -> NSData {
+        return encrypt(message)
+    }
+    
     private func wordToByte(input:[UInt32] /* 64 */) -> [Byte]? /* 16 */ {
         if (input.count != stateSize) {
             return nil;

+ 9 - 0
CryptoSwift/Cipher.swift

@@ -18,4 +18,13 @@ public enum Cipher {
                 return chacha.encrypt(message)
         }
     }
+    
+    public func decrypt(message: NSData) -> NSData {
+        switch (self) {
+        case .ChaCha20(let key, let iv):
+            var chacha = CryptoSwift.ChaCha20(key: key, iv: iv);
+            return chacha.decrypt(message)
+        }
+    }
+
 }

+ 1 - 1
CryptoSwift/Info.plist

@@ -15,7 +15,7 @@
 	<key>CFBundlePackageType</key>
 	<string>FMWK</string>
 	<key>CFBundleShortVersionString</key>
-	<string>0.1</string>
+	<string>0.2</string>
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<key>CFBundleVersion</key>

+ 8 - 2
CryptoSwiftTests/CipherTests.swift

@@ -53,8 +53,14 @@ class CipherTests: XCTestCase {
             let message = [Byte](count: (countElements(expectedHex) / 2), repeatedValue: 0)
             let messageData = NSData(bytes: message, length: message.count);
             
-            let ch = Cipher.ChaCha20(key: keyData, iv: ivData)
-            let encrypted = ch.encrypt(messageData)
+            let setup = (key: keyData, iv: ivData)
+            let encrypted = Cipher.ChaCha20(setup).encrypt(messageData)
+            let decrypted = Cipher.ChaCha20(setup).decrypt(encrypted)
+            XCTAssertEqual(messageData, decrypted, "ChaCha20 decryption failed");
+            
+            if (encrypted.isEqual(decrypted)) {
+                print("FAIL!")
+            }
             
             XCTAssertEqual(encrypted.hexString, expectedHex, "ChaCha20 failed");
         }

+ 27 - 12
README.md

@@ -9,23 +9,22 @@ Good mood
 - Easy to use
 - Convenience extensions
 
-####what implemented?
+###What implemented?
 
 #### Hash
-- MD5
-- SHA1
-- SHA224
-- SHA256
-- SHA384
-- SHA512
-- CRC32 (well, kind of hash)
+- [MD5](http://tools.ietf.org/html/rfc1321)
+- [SHA1](http://tools.ietf.org/html/rfc3174)
+- [SHA224](http://tools.ietf.org/html/rfc6234)
+- [SHA256](http://tools.ietf.org/html/rfc6234)
+- [SHA384](http://tools.ietf.org/html/rfc6234)
+- [SHA512](http://tools.ietf.org/html/rfc6234)
+- [CRC32](http://en.wikipedia.org/wiki/Cyclic_redundancy_check) (well, kind of hash)
 
 #####Cipher
-- ChaCha20
+- [ChaCha20](http://cr.yp.to/chacha/chacha-20080128.pdf)
 
-####[Why?](https://github.com/krzyzanowskim/CryptoSwift/issues/5)
-
-[Because I can](https://github.com/krzyzanowskim/CryptoSwift/issues/5#issuecomment-53379391)
+###Why
+[Why?](https://github.com/krzyzanowskim/CryptoSwift/issues/5) [Because I can](https://github.com/krzyzanowskim/CryptoSwift/issues/5#issuecomment-53379391).
 
 ##Usage
 
@@ -60,6 +59,22 @@ Hashing a String and printing result
         println(hash)
     }
     
+Working with Cipher
+
+	// convenience setup tuplet
+	let setup = (key: keyData, iv: ivData)
+	
+	// encrypt
+	let encrypted:NSData = Cipher.ChaCha20(setup).encrypt(dataToEncrypt)
+	
+	// decrypt
+	let decrypted:NSData = Cipher.ChaCha20(setup).decrypt(encrypted)
+	
+	// check
+	if (encrypted.isEqual(decrypted)) {
+		print("Decryption failed!")
+	}
+    
 ##Contact
 Marcin Krzyżanowski [@krzyzanowskim](http://twitter.com/krzyzanowskim)