Damian Kołakowski 10 ani în urmă
părinte
comite
3a2dab3818

+ 111 - 0
Sources/Swifter/RSA.swift

@@ -0,0 +1,111 @@
+//
+//  RSA.swift
+//  Swifter
+//
+//  Copyright © 2016 Damian Kołakowski. All rights reserved.
+//
+
+#if os(Linux)
+    import Glibc
+#else
+    import Foundation
+#endif
+
+
+public struct RSA {
+    
+    //
+    // RSA
+    //
+    // https://en.wikipedia.org/wiki/RSA_(cryptosystem)
+    //
+    
+    public struct Config {
+        public var n, e, d : Int
+    }
+    
+    public static func encrypt(_ s: Int, _ config: Config) -> Int {
+        return powmod(s, config.e, config.n);
+    }
+    
+    public static func decrypt(_ c: Int, _ config: Config) -> Int {
+        return powmod(c, config.d, config.n);
+    }
+
+    public static func config(_ p: Int, _ q: Int) -> Config {
+        let n = p * q
+        let phin = (p - 1) * (q - 1)
+        let e = coprimes(phin)[1]
+        let d = inverse(e, phin)
+        return Config(n: n, e: e, d: d)
+    }
+
+    public static func inverse(_ a: Int, _ n: Int) -> Int {
+        for i in 1..<n {
+            if (a * i) % n == 1 {
+                return i
+            }
+        }
+        return -1
+    }
+    
+    public static func gcd(_ a: Int, _ b: Int) -> Int {
+        var r = b, pr = a
+        while r > 0 {
+            let t = r
+            r = pr % r
+            pr = t
+        }
+        return pr;
+    }
+
+    public static func coprimes(_ a: Int) -> [Int] {
+        var r = [Int]()
+        for i in 1..<a {
+            if gcd(i, a) == 1 { r.append(i) }
+        }
+        return r
+    }
+    
+    public static func powmod(_ a: Int, _ b: Int, _ n: Int) -> Int {
+        let amod = a % n
+        var r = 1
+        for _ in 0..<b {
+            r = (r * amod) % n
+        }
+        return r
+    }
+    
+    public static func divisors(_ n: Int) -> [Int] {
+        var r = [Int]()
+        for i in 0...n {
+            if n % i == 0 { r.append(i) }
+        }
+        return r
+    }
+    
+    public static func factorization(_ n: Int) -> [Int] {
+        var nn = n
+        var r = [Int]()
+        while nn > 1 {
+            for d in 2...nn {
+                if nn % d == 0 {
+                    r.append(d)
+                    nn = nn / d
+                    break
+                }
+            }
+        }
+        return r
+    }
+    
+    public static func phi(_ n: Int) -> Int {
+        var r = 1, p = 1
+        factorization(n).forEach { f in
+            r *= (f != p) ? (f - 1) : f
+            p = f
+        }
+        return r
+    }
+}
+

+ 14 - 0
XCode/Swifter.xcodeproj/project.pbxproj

@@ -88,6 +88,11 @@
 		7C5F78F41D54C99200C514AA /* RC4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78F11D54C99200C514AA /* RC4.swift */; };
 		7C5F78F91D54D24B00C514AA /* SwifterTestsRC4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78F51D54D21600C514AA /* SwifterTestsRC4.swift */; };
 		7C5F78FA1D54D24B00C514AA /* SwifterTestsRC4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78F51D54D21600C514AA /* SwifterTestsRC4.swift */; };
+		7C5F78FC1D5520B000C514AA /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78FB1D5520B000C514AA /* RSA.swift */; };
+		7C5F78FD1D5520B000C514AA /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78FB1D5520B000C514AA /* RSA.swift */; };
+		7C5F78FE1D5520B000C514AA /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F78FB1D5520B000C514AA /* RSA.swift */; };
+		7C5F79041D55F44500C514AA /* SwifterTestsRSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F79031D55F44500C514AA /* SwifterTestsRSA.swift */; };
+		7C5F79051D55F44500C514AA /* SwifterTestsRSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C5F79031D55F44500C514AA /* SwifterTestsRSA.swift */; };
 		7C6B57EB1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C6B57EA1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift */; };
 		7C6B57EC1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C6B57EA1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift */; };
 		7C71C5B01A1D52F800682BF0 /* login.html in CopyFiles */ = {isa = PBXBuildFile; fileRef = 98630C061A1C9A9D00478D08 /* login.html */; };
@@ -218,6 +223,8 @@
 		7C5F78EE1D54BB5600C514AA /* SwifterTestsAES128.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsAES128.swift; sourceTree = "<group>"; };
 		7C5F78F11D54C99200C514AA /* RC4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RC4.swift; sourceTree = "<group>"; };
 		7C5F78F51D54D21600C514AA /* SwifterTestsRC4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsRC4.swift; sourceTree = "<group>"; };
+		7C5F78FB1D5520B000C514AA /* RSA.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RSA.swift; sourceTree = "<group>"; };
+		7C5F79031D55F44500C514AA /* SwifterTestsRSA.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsRSA.swift; sourceTree = "<group>"; };
 		7C6B57EA1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsHttpRouter.swift; sourceTree = "<group>"; };
 		7C7488771C1DA07300CBCD77 /* file.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = file.html; sourceTree = "<group>"; };
 		7C839B6E19422CFF003A6950 /* SwifterSampleiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwifterSampleiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -383,6 +390,7 @@
 				7C2C85901D50D83D00B32145 /* JSON.swift */,
 				7C1145981D527545000DB965 /* AES128.swift */,
 				7C5F78F11D54C99200C514AA /* RC4.swift */,
+				7C5F78FB1D5520B000C514AA /* RSA.swift */,
 			);
 			path = Swifter;
 			sourceTree = "<group>";
@@ -481,6 +489,7 @@
 				7CB923CF1D510D6400899E2A /* SwifterTestsJSON.swift */,
 				7C5F78F51D54D21600C514AA /* SwifterTestsRC4.swift */,
 				7C5F78EE1D54BB5600C514AA /* SwifterTestsAES128.swift */,
+				7C5F79031D55F44500C514AA /* SwifterTestsRSA.swift */,
 				7CCD875D1C66099B0068099B /* SwifteriOSTests */,
 			);
 			path = SwifterTestsCommon;
@@ -818,6 +827,7 @@
 			files = (
 				7C5F78F21D54C99200C514AA /* RC4.swift in Sources */,
 				7C31962B1CC2C68F00DF5406 /* Socket.swift in Sources */,
+				7C5F78FC1D5520B000C514AA /* RSA.swift in Sources */,
 				7C3196191CC2C68F00DF5406 /* HttpResponse.swift in Sources */,
 				7C3196131CC2C68F00DF5406 /* HttpParser.swift in Sources */,
 				7C31963A1CC2C68F00DF5406 /* String+SHA1.swift in Sources */,
@@ -848,6 +858,7 @@
 			files = (
 				7C5F78F31D54C99200C514AA /* RC4.swift in Sources */,
 				7C31962C1CC2C68F00DF5406 /* Socket.swift in Sources */,
+				7C5F78FD1D5520B000C514AA /* RSA.swift in Sources */,
 				7C31961A1CC2C68F00DF5406 /* HttpResponse.swift in Sources */,
 				7C3196141CC2C68F00DF5406 /* HttpParser.swift in Sources */,
 				7C31963B1CC2C68F00DF5406 /* String+SHA1.swift in Sources */,
@@ -904,6 +915,7 @@
 			files = (
 				7C5F78F41D54C99200C514AA /* RC4.swift in Sources */,
 				7C31962D1CC2C68F00DF5406 /* Socket.swift in Sources */,
+				7C5F78FE1D5520B000C514AA /* RSA.swift in Sources */,
 				7C31961B1CC2C68F00DF5406 /* HttpResponse.swift in Sources */,
 				7C3196151CC2C68F00DF5406 /* HttpParser.swift in Sources */,
 				7C31963C1CC2C68F00DF5406 /* String+SHA1.swift in Sources */,
@@ -934,6 +946,7 @@
 			files = (
 				7C5F78EF1D54BB5600C514AA /* SwifterTestsAES128.swift in Sources */,
 				7CCD87701C660B250068099B /* SwifterTestsHttpParser.swift in Sources */,
+				7C5F79041D55F44500C514AA /* SwifterTestsRSA.swift in Sources */,
 				7C6B57EB1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift in Sources */,
 				7C5915221C92A99300D884BC /* SwifterTestsReflection.swift in Sources */,
 				7C13B57C1C7B069500556443 /* SwifterTestsSQLite.swift in Sources */,
@@ -950,6 +963,7 @@
 			files = (
 				7C5F78F01D54BB5600C514AA /* SwifterTestsAES128.swift in Sources */,
 				7CCD87841C660ED60068099B /* SwifterTestsHttpParser.swift in Sources */,
+				7C5F79051D55F44500C514AA /* SwifterTestsRSA.swift in Sources */,
 				7C6B57EC1CA6C3AA0042655C /* SwifterTestsHttpRouter.swift in Sources */,
 				7C5915231C92A99300D884BC /* SwifterTestsReflection.swift in Sources */,
 				7C13B57D1C7B069500556443 /* SwifterTestsSQLite.swift in Sources */,

+ 21 - 0
XCode/SwifterTestsCommon/SwifterTestsRSA.swift

@@ -0,0 +1,21 @@
+//
+//  SwiferTestsRSA.swift
+//  Swifter
+//
+//  Copyright © 2016 Damian Kołakowski. All rights reserved.
+//
+
+import XCTest
+import Swifter
+
+class SwifterTestsRSA: XCTestCase {
+    
+    func testRSA() {
+        
+        let config = RSA.config(7, 11)
+        
+        XCTAssertEqual(RSA.encrypt(6, config), 41)
+        
+        XCTAssertEqual(RSA.decrypt(RSA.encrypt(6, config), config), 6)
+    }
+}