Browse Source

UInt16 extension (I need generic for bits shifting)

Marcin Krzyżanowski 11 years ago
parent
commit
ca570ea7bd

+ 5 - 1
CryptoSwift.xcodeproj/project.pbxproj

@@ -25,6 +25,7 @@
 		7563B2E819B14D4300B152CD /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7563B2E719B14D4300B152CD /* Cipher.swift */; };
 		7563B2E819B14D4300B152CD /* Cipher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7563B2E719B14D4300B152CD /* Cipher.swift */; };
 		757EF7F519AAA82400586276 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757EF7F419AAA82400586276 /* CRC.swift */; };
 		757EF7F519AAA82400586276 /* CRC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 757EF7F419AAA82400586276 /* CRC.swift */; };
 		758C764119B61AE500653BC6 /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758C764019B61AE500653BC6 /* Generics.swift */; };
 		758C764119B61AE500653BC6 /* Generics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758C764019B61AE500653BC6 /* Generics.swift */; };
+		758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758C764219B61DE900653BC6 /* UInt16Extension.swift */; };
 		758F3F761992E57D0014BBDA /* Playground in Resources */ = {isa = PBXBuildFile; fileRef = 758F3F751992E57D0014BBDA /* Playground */; };
 		758F3F761992E57D0014BBDA /* Playground in Resources */ = {isa = PBXBuildFile; fileRef = 758F3F751992E57D0014BBDA /* Playground */; };
 		758F3F781992F6CE0014BBDA /* ByteExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F3F771992F6CE0014BBDA /* ByteExtension.swift */; };
 		758F3F781992F6CE0014BBDA /* ByteExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 758F3F771992F6CE0014BBDA /* ByteExtension.swift */; };
 		7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* StringExtension.swift */; };
 		7599C9C6199EA28700A3988B /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7599C9C5199EA28700A3988B /* StringExtension.swift */; };
@@ -123,6 +124,7 @@
 		7563B2E719B14D4300B152CD /* Cipher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cipher.swift; sourceTree = "<group>"; };
 		7563B2E719B14D4300B152CD /* Cipher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Cipher.swift; sourceTree = "<group>"; };
 		757EF7F419AAA82400586276 /* CRC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CRC.swift; sourceTree = "<group>"; };
 		757EF7F419AAA82400586276 /* CRC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CRC.swift; sourceTree = "<group>"; };
 		758C764019B61AE500653BC6 /* Generics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Generics.swift; sourceTree = "<group>"; };
 		758C764019B61AE500653BC6 /* Generics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Generics.swift; sourceTree = "<group>"; };
+		758C764219B61DE900653BC6 /* UInt16Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UInt16Extension.swift; sourceTree = "<group>"; };
 		758F3F751992E57D0014BBDA /* Playground */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Playground; path = CryptoSwift/Playground; sourceTree = "<group>"; };
 		758F3F751992E57D0014BBDA /* Playground */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Playground; path = CryptoSwift/Playground; sourceTree = "<group>"; };
 		758F3F771992F6CE0014BBDA /* ByteExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteExtension.swift; sourceTree = "<group>"; };
 		758F3F771992F6CE0014BBDA /* ByteExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ByteExtension.swift; sourceTree = "<group>"; };
 		7599C9C5199EA28700A3988B /* StringExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = "<group>"; };
 		7599C9C5199EA28700A3988B /* StringExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = "<group>"; };
@@ -197,12 +199,13 @@
 				759D481019B517BC005FF7FC /* BitExtension.swift */,
 				759D481019B517BC005FF7FC /* BitExtension.swift */,
 				758F3F771992F6CE0014BBDA /* ByteExtension.swift */,
 				758F3F771992F6CE0014BBDA /* ByteExtension.swift */,
 				7547195019931802002FA5F1 /* IntExtension.swift */,
 				7547195019931802002FA5F1 /* IntExtension.swift */,
+				758C764219B61DE900653BC6 /* UInt16Extension.swift */,
 				75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */,
 				75D94E2519B60C4F007CB2A4 /* UInt32Extension.swift */,
 				75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */,
 				75D94E2719B60DDE007CB2A4 /* UInt64Extension.swift */,
-				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				752DEF7619693EA000E17557 /* NSDataExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
 				754C8FEC19979F94005AD904 /* ArrayExtension.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
 				7599C9C5199EA28700A3988B /* StringExtension.swift */,
+				75D94E2319B60C08007CB2A4 /* Operators.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
 				75164E4819AD30AC00737F30 /* Utils.swift */,
 				758C764019B61AE500653BC6 /* Generics.swift */,
 				758C764019B61AE500653BC6 /* Generics.swift */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
 				754BE45819693E190098E6F3 /* Supporting Files */,
@@ -366,6 +369,7 @@
 				759D481119B517BC005FF7FC /* BitExtension.swift in Sources */,
 				759D481119B517BC005FF7FC /* BitExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
 				754C8FED19979F94005AD904 /* ArrayExtension.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
 				7547195119931802002FA5F1 /* IntExtension.swift in Sources */,
+				758C764319B61DE900653BC6 /* UInt16Extension.swift in Sources */,
 				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
 				75D94E2419B60C08007CB2A4 /* Operators.swift in Sources */,
 				757EF7F519AAA82400586276 /* CRC.swift in Sources */,
 				757EF7F519AAA82400586276 /* CRC.swift in Sources */,
 				75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */,
 				75D94E2619B60C4F007CB2A4 /* UInt32Extension.swift in Sources */,

+ 4 - 5
CryptoSwift/Poly1305.swift

@@ -59,12 +59,11 @@ public class Poly1305 {
             return false
             return false
         }
         }
         
         
-        var u:Byte = 0
+        var u:UInt16 = 0
         for i in 0..<h.count {
         for i in 0..<h.count {
-            u = u &+ h[i] &+ c[i]
-            h[0] = u
-            println(u)
-            u.shiftRight(8) // u = u >> 8
+            u = u &+ UInt16(h[i]) &+ UInt16(c[i])
+            h[0] = Byte(u)
+            u = u &>> 8 // u = u >> 8
         }
         }
         return true
         return true
     }
     }

+ 45 - 0
CryptoSwift/UInt16Extension.swift

@@ -0,0 +1,45 @@
+//
+//  UInt16Extension.swift
+//  CryptoSwift
+//
+//  Created by Marcin Krzyzanowski on 02/09/14.
+//  Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
+//
+
+import Foundation
+
+/** Shift bits */
+extension UInt16 {
+    /** Shift bits to the right. All bits are shifted (including sign bit) */
+    mutating func shiftRight(count: UInt16) -> UInt16 {
+        if (self == 0) {
+            return self;
+        }
+
+        var bitsCount = UInt16(sizeofValue(self) * 8)
+
+        if (count >= bitsCount) {
+            return 0
+        }
+
+        var maxBitsForValue = UInt16(floor(log2(Double(self) + 1)))
+        var shiftCount = Swift.min(count, maxBitsForValue - 1)
+        var shiftedValue:UInt16 = 0;
+        
+        for bitIdx in 0..<bitsCount {
+            var byte = 1 << bitIdx
+            if ((self & byte) == byte) {
+                shiftedValue = shiftedValue | (byte >> shiftCount)
+            }
+        }
+        self = shiftedValue
+        return self
+    }
+}
+
+/** shift right and assign with bits truncation */
+func &>> (lhs: UInt16, rhs: UInt16) -> UInt16 {
+    var l = lhs;
+    l.shiftRight(rhs)
+    return l
+}