Bladeren bron

Pure swift integerWithBytes

Marcin Krzyżanowski 10 jaren geleden
bovenliggende
commit
32ad55e3d7

+ 5 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -58,6 +58,7 @@
 		75B0A5701AB1A1BB000BD8D2 /* PKCS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */; };
 		75B0A5701AB1A1BB000BD8D2 /* PKCS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */; };
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
 		75B601EB197D6A6C0009B53D /* CryptoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 754BE45519693E190098E6F3 /* CryptoSwift.framework */; };
 		75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */; };
 		75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */; };
+		75BE4EB11B1E4A9F007A2B57 /* IntegerConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */; };
 		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
 		75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2319B60C08007CB2A4 /* Operators.swift */; };
 		75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */; };
 		75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */; };
 		75D94E2819B60DDE007CB2A4 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */; };
 		75D94E2819B60DDE007CB2A4 /* UInt64Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */; };
@@ -168,6 +169,7 @@
 		75A74B261A1FF6B2004419F1 /* AES.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AES.swift; sourceTree = "<group>"; };
 		75A74B261A1FF6B2004419F1 /* AES.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AES.swift; sourceTree = "<group>"; };
 		75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKCS5.swift; sourceTree = "<group>"; };
 		75B0A56F1AB1A1BB000BD8D2 /* PKCS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PKCS5.swift; sourceTree = "<group>"; };
 		75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CipherBlockMode.swift; sourceTree = "<group>"; };
 		75BC3AE21A4E412000ADF343 /* CipherBlockMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CipherBlockMode.swift; sourceTree = "<group>"; };
+		75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntegerConvertible.swift; sourceTree = "<group>"; };
 		75D94E2319B60C08007CB2A4 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
 		75D94E2319B60C08007CB2A4 /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
 		75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt32Extension.swift; sourceTree = "<group>"; };
 		75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt32Extension.swift; sourceTree = "<group>"; };
 		75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt64Extension.swift; sourceTree = "<group>"; };
 		75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt64Extension.swift; sourceTree = "<group>"; };
@@ -253,6 +255,7 @@
 				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
 				758C764019B61AE500653BC6 /* Generics.swift */,
 				758C764019B61AE500653BC6 /* Generics.swift */,
+				75BE4EB01B1E4A9F007A2B57 /* IntegerConvertible.swift */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 			);
 			);
 			path = CryptoSwift;
 			path = CryptoSwift;
@@ -432,6 +435,7 @@
 				7552614E1993051E000D2B20 /* Hash.swift in Sources */,
 				7552614E1993051E000D2B20 /* Hash.swift in Sources */,
 				75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */,
 				75BC3AE31A4E412000ADF343 /* CipherBlockMode.swift in Sources */,
 				7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */,
 				7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */,
+				75BE4EB11B1E4A9F007A2B57 /* IntegerConvertible.swift in Sources */,
 				7563B2E819B14D4300B152CD /* Cipher.swift in Sources */,
 				7563B2E819B14D4300B152CD /* Cipher.swift in Sources */,
 				752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */,
 				752E087B199FF27C005B0EA0 /* SHA1.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
 				75EB380119ABDD710002375A /* ChaCha20.swift in Sources */,
@@ -720,6 +724,7 @@
 				3F8849E41B0647D6006AB604 /* Release */,
 				3F8849E41B0647D6006AB604 /* Release */,
 			);
 			);
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		};
 		754BE44F19693E190098E6F3 /* Build configuration list for PBXProject "CryptoSwift" */ = {
 		754BE44F19693E190098E6F3 /* Build configuration list for PBXProject "CryptoSwift" */ = {
 			isa = XCConfigurationList;
 			isa = XCConfigurationList;

+ 2 - 2
CryptoSwift.xcodeproj/project.xcworkspace/xcshareddata/CryptoSwift.xccheckout

@@ -7,14 +7,14 @@
 	<key>IDESourceControlProjectIdentifier</key>
 	<key>IDESourceControlProjectIdentifier</key>
 	<string>7266B6DF-1CD3-4383-821A-42E5BE4580E8</string>
 	<string>7266B6DF-1CD3-4383-821A-42E5BE4580E8</string>
 	<key>IDESourceControlProjectName</key>
 	<key>IDESourceControlProjectName</key>
-	<string>CryptoSwift</string>
+	<string>project</string>
 	<key>IDESourceControlProjectOriginsDictionary</key>
 	<key>IDESourceControlProjectOriginsDictionary</key>
 	<dict>
 	<dict>
 		<key>C3D8ED1CB28D809ADCE2C0DE74935E8A502ACD53</key>
 		<key>C3D8ED1CB28D809ADCE2C0DE74935E8A502ACD53</key>
 		<string>github.com:krzyzanowskim/CryptoSwift.git</string>
 		<string>github.com:krzyzanowskim/CryptoSwift.git</string>
 	</dict>
 	</dict>
 	<key>IDESourceControlProjectPath</key>
 	<key>IDESourceControlProjectPath</key>
-	<string>CryptoSwift.xcodeproj</string>
+	<string>CryptoSwift.xcodeproj/project.xcworkspace</string>
 	<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
 	<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
 	<dict>
 	<dict>
 		<key>C3D8ED1CB28D809ADCE2C0DE74935E8A502ACD53</key>
 		<key>C3D8ED1CB28D809ADCE2C0DE74935E8A502ACD53</key>

+ 1 - 1
CryptoSwift.xcodeproj/xcshareddata/xcschemes/CryptoSwift.xcscheme

@@ -40,7 +40,7 @@
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
       shouldUseLaunchSchemeArgsEnv = "YES"
       shouldUseLaunchSchemeArgsEnv = "YES"
-      buildConfiguration = "Debug">
+      buildConfiguration = "Release">
       <Testables>
       <Testables>
          <TestableReference
          <TestableReference
             skipped = "NO">
             skipped = "NO">

+ 16 - 15
CryptoSwift/Generics.swift

@@ -35,24 +35,25 @@ func integerFromBitsArray<T: UnsignedIntegerType>(bits: [Bit]) -> T
 }
 }
 
 
 /// Initialize integer from array of bytes.
 /// Initialize integer from array of bytes.
-/// I found this method slow
-@availability(*, deprecated=0.8, message="Deprecated but replacement is not yet available.")
-func integerWithBytes<T: IntegerType>(bytes: [UInt8]) -> T {
-    var totalBytes = Swift.min(bytes.count, sizeof(T))
-    // get slice of Int
-    var start = Swift.max(bytes.count - sizeof(T),0)
-    var intarr = [UInt8](bytes[start..<(start + totalBytes)])
+/// This method may be slow
+func integerWithBytes<T: IntegerType where T:ByteConvertible, T: BitshiftOperationsType>(bytes: [UInt8]) -> T {
+    var bytes = bytes.reverse()
+    if bytes.count < sizeof(T) {
+        let paddingCount = sizeof(T) - bytes.count
+        if (paddingCount > 0) {
+            bytes += [UInt8](count: paddingCount, repeatedValue: 0)
+        }
+    }
     
     
-    // pad size if necessary
-    while (intarr.count < sizeof(T)) {
-        intarr.insert(0 as UInt8, atIndex: 0)
+    if sizeof(T) == 1 {
+        return T(truncatingBitPattern: UInt64(bytes[0]))
     }
     }
-    intarr = intarr.reverse()
     
     
-    var i:T = 0
-    var data = NSData(bytes: intarr, length: intarr.count)
-    data.getBytes(&i, length: sizeofValue(i));
-    return i
+    var result: T = 0
+    for byte in bytes.reverse() {
+        result = result << 8 | T(byte)
+    }
+    return result
 }
 }
 
 
 /// Array of bytes, little-endian representation. Don't use if not necessary.
 /// Array of bytes, little-endian representation. Don't use if not necessary.

+ 40 - 0
CryptoSwift/IntegerConvertible.swift

@@ -0,0 +1,40 @@
+//
+//  IntegerConvertible.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/06/15.
+//  Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+protocol BitshiftOperationsType {
+    func <<(lhs: Self, rhs: Self) -> Self
+    func >>(lhs: Self, rhs: Self) -> Self
+    func <<=(inout lhs: Self, rhs: Self)
+    func >>=(inout lhs: Self, rhs: Self)
+}
+
+protocol ByteConvertible {
+    init(_ value: UInt8)
+    init(truncatingBitPattern: UInt64)
+}
+
+extension Int    : BitshiftOperationsType, ByteConvertible { }
+extension Int8   : BitshiftOperationsType, ByteConvertible { }
+extension Int16  : BitshiftOperationsType, ByteConvertible { }
+extension Int32  : BitshiftOperationsType, ByteConvertible { }
+extension Int64  : BitshiftOperationsType, ByteConvertible {
+    init(truncatingBitPattern value: UInt64) {
+        self = Int64(bitPattern: value)
+    }
+}
+extension UInt   : BitshiftOperationsType, ByteConvertible { }
+extension UInt8  : BitshiftOperationsType, ByteConvertible { }
+extension UInt16 : BitshiftOperationsType, ByteConvertible { }
+extension UInt32 : BitshiftOperationsType, ByteConvertible { }
+extension UInt64 : BitshiftOperationsType, ByteConvertible {
+    init(truncatingBitPattern value: UInt64) {
+        self = value
+    }
+}